10 Most Important C Programs for NEB +2 Students

This is my final project report for practical class 12

Table Content

S.N.

Title of Lab Work

Click to Jump

1

Program to get three inputs from the user and then print the greatest number among them.

3

2

Program to check whether the given word is palindrome or not.

5

3

Program to print Fibonacci series up to the 10th term starting from 1, 1, 2,… 10th term using a user-defined function.

7

4

Program to count the vowels and consonants in a given word using a function (passing arguments).

9

5

Program to enter 20 employees’ names, ages, and salaries using a structure and print them.

11

6

Program to input 300 students’ details (Name, Age, Gender, GPA) and print the list of students with GPA greater than 2.5 and less than 3.00.

14

7

Program to get the sum of 3 different numbers using pointers.

17

8

Program to swap two integers using pointers.

19

9

Program to read the content from the file named “content.txt” and add additional content of students (name, section, roll number) in it.

21

10

Program to enter names and addresses of the students and store them in a data file “student.dat”.

23

 

OBJECTIVES

  • To introduce students to the basic knowledge of programming fundamentals of C language.
  • To impart writing skill of C programming to the students and solving problems.
  • To impart the concepts like looping, array, functions, pointers, file, structure.

 COURSE OUTCOME

 After completing this lab course you will be able to:

  • Understand the logic for a given problem.
  • Write the algorithm of a given problem.
  • Draw a flow chart of a given problem.
  • Recognize and understand the syntax and construction.
  • Gain experience of procedural language programming.
  • Know the steps involved in compiling, linking and debugging C code.
  • Understand using header files.
  • Learn the methods of iteration or looping and branching.
  • Make use of different data-structures like arrays, pointers, structures and files.
  • Understand how to access and use library functions.
  • Understand function declaration and definition.
  • Understand proper use of user defined functions.
  • Write programs to print output on the screen as well as in the files.
  • Apply all the concepts that have been covered in the theory course.
  • Know the alternative ways of providing solutions to a given problem.

 

 

Program to get three inputs from the user and then print the greatest number among them

Objective(s):

To find the greatest number among three inputs provided by the user.

Program:

Program to get three inputs from the user and then print the greatest number among them.

Algorithm:

  1. Start
  2. Declare three variables (num1, num2, num3) to store the user inputs.
  3. Display a message prompting the user to enter three numbers.
  4. Read the three numbers from the user and store them in num1, num2, and num3.
  5. Compare num1 with num2 and num3 to find the greatest number.
  6. Display the greatest number.
  7. End

 

Code:

   #include <stdio.h>

   int main() {

       int num1, num2, num3;

       printf(“Enter three numbers: “);

       scanf(“%d %d %d”, &num1, &num2, &num3);

       if (num1 >= num2 && num1 >= num3)

           printf(“Greatest number: %d\n”, num1);

       else if (num2 >= num1 && num2 >= num3)

           printf(“Greatest number: %d\n”, num2);

       else

           printf(“Greatest number: %d\n”, num3);

       return 0;

   }

Output:

   Enter three numbers: 5 10 3

   Greatest number: 10

Flowchart:


 

 

Program to check whether the given word is palindrome or not

Objective(s):

To determine whether a given word is a palindrome or not.

Program:

Program to check whether the given word is palindrome or not.

Algorithm:

  1. Start
  2. Declare a character array to store the input word.
  3. Display a message prompting the user to enter a word.
  4. Read the input word from the user and store it in the character array.
  5. Check if the word is a palindrome.
  6. Display the result.
  7. End

Code:

   #include <stdio.h>

   #include <string.h>

   int main() {

       char word[100];

       int i, length;

       int isPalindrome = 1;

       printf(“Enter a word: “);

       scanf(“%s”, word);

       length = strlen(word);

       for (i = 0; i < length / 2; i++) {

           if (word[i] != word[length – i – 1]) {

               isPalindrome = 0;

               break;

           }

       }

       if (isPalindrome)

           printf(“%s is a palindrome.\n”, word);

       else

           printf(“%s is not a palindrome.\n”, word);

       return 0;

   }

Output:

   Enter a word: radar

   radar is a palindrome.

Flowchart:

Program to print Fibonacci series up to the 10th term using a user-defined function

Objective(s):

To generate and print the Fibonacci series up to the 10th term.

Program:

Program to print Fibonacci series up to the 10th term using a user-defined function.

 

Algorithm:

  1. Start
  2. Define a function to calculate the nth term of the Fibonacci series.
  3. Iterate from 1 to 10 and print the Fibonacci series terms using the defined function.
  4. End

Code:

   #include <stdio.h>

   int fibonacci(int n) {

       if (n <= 1)

           return n;

       return fibonacci(n – 1) + fibonacci(n – 2);

   }

   int main() {

       int i;

       printf(“Fibonacci series up to 10th term:\n”);

       for (i = 0; i < 10; i++)

           printf(“%d “, fibonacci(i));

       printf(“\n”);

       return 0;

   }

Output:

   Fibonacci series up to 10th term:

   0 1 1 2 3 5 8 13 21 34

Flowchart:

 

 

 

 

Program to count the vowels and consonants in a given word using a function

Objective(s):

To count the number of vowels and consonants in a given word.

Program:

Program to count the vowels and consonants in a given word using a function.

 

Algorithm:

  1. Start
  2. Declare variables to store the counts of vowels and consonants.
  3. Define a function to check whether a character is a vowel.
  4. Iterate through each character in the word and increment the respective count.
  5. Display the counts of vowels and consonants.
  6. End

Code:

   #include <stdio.h>

   int isVowel(char c) {

       return (c == ‘a’ || c == ‘e’ || c == ‘i’ || c == ‘o’ || c == ‘u’ || c == ‘A’ || c == ‘E’ || c == ‘I’ || c == ‘O’ || c == ‘U’);

   }

   int main() {

       char word[100];

       int vowels = 0, consonants = 0, i;

       printf(“Enter a word: “);

       scanf(“%s”, word);

       for (i = 0; word[i] != ‘\0’; i++) {

           if (isVowel(word[i]))

               vowels++;

           else

               consonants++;

       }

       printf(“Vowels: %d\nConsonants: %d\n”, vowels, consonants);

       return 0;

   }

Output:

   Enter a word: Hello

   Vowels: 2

   Consonants: 3

Flowchart:


 

Program to enter 20 employee's name, age, and salary using a structure and print them

Objective(s):

To store and display the details of 20 employees using a structure.

Program:

Program to enter 20 employee’s name, age, and salary using a structure and print them.

 

 

Algorithm:

  1. Start
  2. Define a structure to store employee details (name, age, salary).
  3. Declare an array of structures to store details of 20 employees.
  4. Read details of each employee and store them in the array of structures.
  5. Display the details of all 20 employees.
  6. End

 

Code:

   #include <stdio.h>

   struct Employee {

       char name[50];

       int age;

       float salary;

   };

   int main() {

       struct Employee employees[20];

       int i;

       for (i = 0; i < 20; i++) {

           printf(“Enter details of Employee %d:\n”, i + 1);

           printf(“Name: “);

           scanf(“%s”, employees[i].name);

           printf(“Age

: “);

           scanf(“%d”, &employees[i].age);

           printf(“Salary: “);

           scanf(“%f”, &employees[i].salary);

       }

       printf(“\nEmployee Details:\n”);

       for (i = 0; i < 20; i++) {

           printf(“Employee %d:\n”, i + 1);

           printf(“Name: %s\n”, employees[i].name);

           printf(“Age: %d\n”, employees[i].age);

           printf(“Salary: %.2f\n”, employees[i].salary);

       }

       return 0;

   }

Output:

   Enter details of Employee 1:

   Name: John

   Age: 30

   Salary: 50000

    ……

   Enter details of Employee 20:

   Name: Alice

   Age: 25

   Salary: 45000

Flowchart:


 

Program to input 300 students' details (Name, Age, Gender, GPA) and print the list of students with GPA greater than 2.5 and less than 3.0

Objective(s):

To input and display student details based on GPA criteria.

Program:

Program to input 300 students’ details (Name, Age, Gender, GPA) and print the list of students with GPA greater than 2.5 and less than 3.0.

Algorithm:

  1. Start
  2. Define a structure to store student details (name, age, gender, GPA).
  3. Declare an array of structures to store details of 300 students.
  4. Read details of each student and store them in the array of structures.
  5. Iterate over the array and print details of students whose GPA is greater than 2.5 and less than 3.0.
  6. End

Code:

   #include <stdio.h>

   struct Student {

       char name[50];

       int age;

       char gender;

       float gpa;

   };

   int main() {

       struct Student students[300];

       int i;

       for (i = 0; i < 300; i++) {

           printf(“Enter details of Student %d:\n”, i + 1);

           printf(“Name: “);

           scanf(“%s”, students[i].name);

           printf(“Age: “);

           scanf(“%d”, &students[i].age);

           printf(“Gender (M/F): “);

           scanf(” %c”, &students[i].gender);

           printf(“GPA: “);

           scanf(“%f”, &students[i].gpa);

       }

       printf(“\nStudents with GPA between 2.5 and 3.0:\n”);

       for (i = 0; i < 300; i++) {

           if (students[i].gpa > 2.5 && students[i].gpa < 3.0) {

               printf(“Name: %s, Age: %d, Gender: %c, GPA: %.2f\n”, students[i].name, students[i].age, students[i].gender, students[i].gpa);

           }

       }

       return 0;

   }

Output:

Input:

Enter details of Student 1:

Name: John Doe

Age: 20

Gender (M/F): M

GPA: 2.8

Enter details of Student 300:

Name: Alex Johnson

Age: 22

Gender (M/F): M

GPA: 2.9

 

 

Flowchart: