Project file for C language
Practical No. 1
Aim:
To write a program in C using input and output statements.
Algorithm:
1. Start the program.
2. Declare variables to store user input.
3. Use printf() to display messages on the screen.
4. Use scanf() to take input from the user.
5. Display the entered data using printf().
6. Stop the program.
Here is the c program :
#include <stdio.h>
int main() {
int age;
char name[30];
printf("Enter your name: ");
scanf("%s", name);
printf("Enter your age: ");
scanf("%d", &age);
printf("\n--- OUTPUT ---\n");
printf("Hello %s, you are %d years old.\n", name, age);
return 0;
}
Execution of the program :
Enter your name: Vineet
Enter your age: 20
--- OUTPUT ---
Hello Vineet, you are 20 years old.
Practical No. 2
Aim:
To write a C program using control statements.
Algorithm:
1. Start the program.
2. Declare a variable to store a number.
3. Accept a number from the user.
4. Use an if-else statement to check whether the number is positive, negative, or zero.
5. Display the result.
6. Stop the program.
#include <stdio.h>
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
if (num > 0)
printf("%d is Positive.\n", num);
else if (num < 0)
printf("%d is Negative.\n", num);
else
printf("The number is Zero.\n");
return 0;
Output of the program :
Enter a number: -5
-5 is Negative.
Practical No. 3
Aim:
To write a program in C using functions.
Algorithm:
1. Start the program.
2. Define a function named add() that takes two numbers as arguments and returns their
sum.
3. In the main() function, declare variables for input.
4. Accept two numbers from the user.
5. Call the add() function and store the result.
6. Display the result.
7. Stop the program.
#include <stdio.h>
// Function declaration
int add(int a, int b);
int main() {
int num1, num2, sum;
printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);
sum = add(num1, num2); // function call
printf("Sum of %d and %d is %d\n", num1, num2, sum);
return 0;
// Function definition
int add(int a, int b) {
return a + b;
Output of the program
Enter two numbers: 10 25
Sum of 10 and 25 is 35
Practical No. 4
Aim:
To write a program in C using arrays.
Algorithm:
1. Start the program.
2. Declare an integer array to store elements.
3. Accept the number of elements from the user.
4. Read array elements using a for loop.
5. Display all array elements.
6. Stop the program.
#include <stdio.h>
int main() {
int arr[100], n, i;
printf("Enter number of elements: ");
scanf("%d", &n);
printf("Enter %d elements:\n", n);
for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
printf("\nArray elements are:\n");
for (i = 0; i < n; i++) {
printf("%d ", arr[i]);
return 0;
Enter number of elements: 5
Enter 5 elements:
12345
Array elements are:
12345
Practical No. 5
Aim:
To write a program in C using structures.
Algorithm:
1. Start the program.
2. Define a structure named Student with members: name, roll number, and marks.
3. Declare a structure variable.
4. Accept details of the student from the user.
5. Display the details using printf().
6. Stop the program.
#include <stdio.h>
struct Student {
char name[30];
int roll;
float marks;
};
int main() {
struct Student s;
printf("Enter name: ");
scanf("%s", [Link]);
printf("Enter roll number: ");
scanf("%d", &[Link]);
printf("Enter marks: ");
scanf("%f", &[Link]);
printf("\n--- Student Details ---\n");
printf("Name: %s\n", [Link]);
printf("Roll Number: %d\n", [Link]);
printf("Marks: %.2f\n", [Link]);
return 0;
Output of program :
Enter name: Rohan
Enter roll number: 101
Enter marks: 87.5
--- Student Details ---
Name: Rohan
Roll Number: 101
Marks: 87.50
Practical No. 6
Aim:
To write a program in C using files.
Algorithm:
1. Start the program.
2. Declare a file pointer variable.
3. Open a file in write mode using fopen().
4. Accept data from the user and write it to the file using fprintf().
5. Close the file.
6. Reopen the file in read mode using fopen().
7. Read and display the content using fgetc() or fscanf().
8. Close the file.
9. Stop the program.
#include <stdio.h>
int main() {
FILE *fp;
char name[30];
int age;
fp = fopen("[Link]", "w");
if (fp == NULL) {
printf("Error opening file!\n");
return 1;
}
printf("Enter your name: ");
scanf("%s", name);
printf("Enter your age: ");
scanf("%d", &age);
fprintf(fp, "Name: %s\nAge: %d\n", name, age);
fclose(fp);
printf("\nData written successfully!\n");
// Reading the file
fp = fopen("[Link]", "r");
char ch;
printf("\nFile content:\n");
while ((ch = fgetc(fp)) != EOF) {
putchar(ch);
fclose(fp);
return 0;
Output of the program :
Enter your name: Vineet
Enter your age: 21
Data written successfully!
File content:
Name: Vineet
Age: 21
Practical No. 7
Aim:
To write a program in C to generate the Fibonacci series.
Algorithm:
1. Start the program.
2. Declare variables a, b, and nextTerm.
3. Accept the number of terms from the user.
4. Use a for loop to generate Fibonacci numbers using the formula:
nextTerm = a + b
5. Print each term.
6. Stop the program.
#include <stdio.h>
int main() {
int n, i;
int a = 0, b = 1, next;
printf("Enter number of terms: ");
scanf("%d", &n);
printf("Fibonacci Series: ");
for (i = 1; i <= n; i++) {
printf("%d ", a);
next = a + b;
a = b;
b = next;
return 0;
}
Output of the program :
Enter number of terms: 7
Fibonacci Series: 0 1 1 2 3 5 8
Practical No. 8
Aim:
To write a program in C to generate prime numbers.
Algorithm:
1. Start the program.
2. Accept a range limit from the user.
3. Use a loop to check numbers from 2 to the limit.
4. For each number, check divisibility by any number between 2 and n/2.
5. If not divisible, print it as a prime number.
6. Stop the program.
#include <stdio.h>
int main() {
int n, i, j, flag;
printf("Enter the limit: ");
scanf("%d", &n);
printf("Prime numbers up to %d are:\n", n);
for (i = 2; i <= n; i++) {
flag = 0;
for (j = 2; j <= i / 2; j++) {
if (i % j == 0) {
flag = 1;
break;
}
}
if (flag == 0)
printf("%d ", i);
return 0;
Output of the program
Enter the limit: 20
Prime numbers up to 20 are:
2 3 5 7 11 13 17 19
Practical No. 9
Aim:
To write a program in C to sort an array using different methods.
Algorithm (Bubble Sort Example):
1. Start the program.
2. Accept the size of the array.
3. Input all elements.
4. Compare adjacent elements and swap them if they are in the wrong order.
5. Repeat this process until the array is sorted.
6. Display the sorted array.
7. Stop the program.
#include <stdio.h>
int main() {
int arr[100], n, i, j, temp;
printf("Enter number of elements: ");
scanf("%d", &n);
printf("Enter %d elements:\n", n);
for (i = 0; i < n; i++)
scanf("%d", &arr[i]);
for (i = 0; i < n - 1; i++) {
for (j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
printf("\nSorted array: ");
for (i = 0; i < n; i++)
printf("%d ", arr[i]);
return 0;
Output of the program :
Enter number of elements: 5
Enter 5 elements:
45 12 89 33 7
Sorted array: 7 12 33 45 89
Practical No. 10
Aim:
To write a program in C to search an element from an array.
Algorithm (Linear Search):
1. Start the program.
2. Accept the number of elements in the array.
3. Read all array elements.
4. Accept the element to search.
5. Compare each element with the search key.
6. If found, display its position; otherwise, display “not found.”
7. Stop the program.
#include <stdio.h>
int main() {
int arr[100], n, i, key, found = 0;
printf("Enter number of elements: ");
scanf("%d", &n);
printf("Enter %d elements:\n", n);
for (i = 0; i < n; i++)
scanf("%d", &arr[i]);
printf("Enter element to search: ");
scanf("%d", &key);
for (i = 0; i < n; i++) {
if (arr[i] == key) {
printf("Element %d found at position %d.\n", key, i + 1);
found = 1;
break;
}
if (!found)
printf("Element %d not found in the array.\n", key);
return 0;
Output of the program :
Enter number of elements: 6
Enter 6 elements:
10 20 30 40 50 60
Enter element to search: 40
Element 40 found at position 4.