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. | |
2 | Program to check whether the given word is palindrome or not. | |
3 | Program to print Fibonacci series up to the 10th term starting from 1, 1, 2,… 10th term using a user-defined function. | |
4 | Program to count the vowels and consonants in a given word using a function (passing arguments). | |
5 | Program to enter 20 employees’ names, ages, and salaries using a structure and print them. | |
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. | |
7 | Program to get the sum of 3 different numbers using pointers. | |
8 | Program to swap two integers using pointers. | |
9 | Program to read the content from the file named “content.txt” and add additional content of students (name, section, roll number) in it. | |
10 | Program to enter names and addresses of the students and store them in a data file “student.dat”. |
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:
- Start
- Declare three variables (num1, num2, num3) to store the user inputs.
- Display a message prompting the user to enter three numbers.
- Read the three numbers from the user and store them in num1, num2, and num3.
- Compare num1 with num2 and num3 to find the greatest number.
- Display the greatest number.
- 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:
- Start
- Declare a character array to store the input word.
- Display a message prompting the user to enter a word.
- Read the input word from the user and store it in the character array.
- Check if the word is a palindrome.
- Display the result.
- 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:
- Start
- Define a function to calculate the nth term of the Fibonacci series.
- Iterate from 1 to 10 and print the Fibonacci series terms using the defined function.
- 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:
- Start
- Declare variables to store the counts of vowels and consonants.
- Define a function to check whether a character is a vowel.
- Iterate through each character in the word and increment the respective count.
- Display the counts of vowels and consonants.
- 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:
- Start
- Define a structure to store employee details (name, age, salary).
- Declare an array of structures to store details of 20 employees.
- Read details of each employee and store them in the array of structures.
- Display the details of all 20 employees.
- 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:
- Start
- Define a structure to store student details (name, age, gender, GPA).
- Declare an array of structures to store details of 300 students.
- Read details of each student and store them in the array of structures.
- Iterate over the array and print details of students whose GPA is greater than 2.5 and less than 3.0.
- 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:
Program to get the sum of 3 different numbers using pointers
Objective(s):
To calculate the sum of three different numbers using pointers.
Program:
Program to get the sum of 3 different numbers using pointers.
Algorithm:
- Start
- Declare three variables (num1, num2, num3) and pointers (ptr1, ptr2, ptr3) to store the numbers and their addresses.
- Display a message prompting the user to enter three numbers.
- Read the three numbers from the user.
- Assign the addresses of the variables to the pointers.
- Calculate the sum using pointers.
- Display the sum.
- End
Code:
#include <stdio.h>
int main() {
int num1, num2, num3, sum;
int *ptr1, *ptr2, *ptr3;
ptr1 = &num1;
ptr2 = &num2;
ptr3 = &num3;
printf(“Enter three different numbers: “);
scanf(“%d %d %d”, &num1, &num2, &num3);
sum = *ptr1 + *ptr2 + *ptr3;
printf(“Sum: %d\n”, sum);
return 0;
}
Output: [Output will vary based on user input]
Flowchart:
Program to swap two integers using pointers
Objective(s):
To swap the values of two integers using pointers.
Program:
Program to swap two integers using pointers.
Algorithm:
- Start
- Declare two variables (num1, num2) and pointers (ptr1, ptr2) to store the integers and their addresses.
- Display a message prompting the user to enter two numbers.
- Read the two numbers from the user.
- Assign the addresses of the variables to the pointers.
- Swap the values using pointers.
- Display the swapped values.
- End
Code:
#include <stdio.h>
int main() {
int num1, num2, *ptr1, *ptr2, temp;
ptr1 = &num1;
ptr2 = &num2;
printf(“Enter two numbers: “);
scanf(“%d %d”, &num1, &num2);
temp = *ptr1;
*ptr1 = *ptr2;
*ptr2 = temp;
printf(“After swapping, num1 = %d, num2 = %d\n”, num1, num2);
return 0;
}
Output: [Output will vary based on user input]
Flowchart:
Program to read the content from the file named "content.txt" and add additional content of students (name, section, roll number) in it
Objective(s):
To read content from a file and append additional content to it.
Program:
Program to read the content from the file named “content.txt” and add additional content of students (name, section, roll number) in it.
Algorithm:
- Start
- Declare a file pointer (fptr) and open the file “content.txt” in append mode.
- Display a message prompting the user to enter student details (name, section, roll number).
- Read the student details from the user.
- Write the student details to the file.
- Close the file.
- End
Code:
#include <stdio.h>
int main() {
FILE *fptr;
char name[50], section;
int rollNumber;
fptr = fopen(“content.txt”, “a”);
if (fptr == NULL) {
printf(“Error opening file.\n”);
return 1;
}
printf(“Enter student details (Name Section RollNumber): “);
scanf(“%s %c %d”, name, §ion, &rollNumber);
fprintf(fptr, “%s %c %d\n”, name, section, rollNumber);
fclose(fptr);
return 0;
}
Output: [Content will be added to the file “content.txt” as per user input]
Flowchart:
Objective(s):
To store student names and addresses in a data file.
Program:
Program to enter names and addresses of the student and store them in a data file “student.dat”.
Algorithm:
- Start
- Define a structure to store student details (name, address).
- Declare a file pointer (fptr) and open the file “student.dat” in write mode.
- Display a message prompting the user to enter student details (name, address).
- Read the student details from the user.
- Write the student details to the file.
- Close the file.
- End
Code:
#include <stdio.h>
struct Student {
char name[50];
char address[100];
};
int main() {
FILE *fptr;
struct Student student;
fptr = fopen(“student.dat”, “w”);
if (fptr == NULL) {
printf(“Error opening file.\n”);
return 1;
}
printf(“Enter student details (Name Address): “);
scanf(“%s %[^\n]”, student.name, student.address);
fprintf(fptr, “%s %s\n”, student.name, student.address);
fclose(fptr);
return 0;
}
Output:
Enter student details (Name Address): John Doe 123 Main Street
John Doe 123 Main Street
Flowchart:
TrachitZ - Youtube
These are some recent videos in my YouTube channel. You can visit my channel to watch similar videos.