Write a C program to compare any two strings using string handling functions.
#include <stdio.h>
#include <string.h>
int main()
{
char str1[100], str2[100];
int result;
printf(“Enter first string: “);
gets(str1);
printf(“Enter second string: “);
gets(str2);
result = strcmp(str1, str2);
if(result == 0)
printf(“The two strings are equal.”);
else
printf(“The two strings are not equal.”);
return 0;
}
C program to reverse a string without using string handling functions.
#include <stdio.h>
int main()
{
char str[100], rev[100];
int i, length = 0;
printf(“Enter a string: “);
gets(str);
/* Find length of the string */
while(str[length] != ‘\0’)
{
length++;
}
/* Reverse the string */
for(i = 0; i < length; i++)
{
rev[i] = str[length – i – 1];
}
rev[i] = ‘\0’;
printf(“Reversed string: %s”, rev);
return 0;
}
Explanation
- The user enters a string using gets().
- The program finds the length of the string manually using a while loop.
- A for loop copies characters from the end of the original string to the beginning of another array.
- The reversed string is stored in rev and printed using printf().
C program to print the elements of an array in reverse order using pointers.
#include <stdio.h>
int main()
{
int a[50], n, i;
int *p;
printf(“Enter the number of elements: “);
scanf(“%d”, &n);
printf(“Enter %d elements:\n”, n);
for(i = 0; i < n; i++)
{
scanf(“%d”, &a[i]);
}
p = &a[n – 1]; // pointer pointing to the last element
printf(“Array elements in reverse order:\n”);
for(i = n – 1; i >= 0; i–)
{
printf(“%d “, *p);
p–; // move pointer to previous element
}
return 0;
}
Explanation
- The program reads n elements into an array.
- The pointer p is initialized to the address of the last element of the array (&a[n-1]).
- Using a loop, the program prints the value pointed by p (*p).
- The pointer is decremented (p–) each time to move backward through the array.
- Thus, the array elements are displayed in reverse order using pointers.
Top of Form
Write a C program to find the occurrence of each element in an array
#include <stdio.h>
int main()
{
int arr[50], freq[50];
int n, i, j, count;
printf(“Enter the number of elements: “);
scanf(“%d”, &n);
printf(“Enter the elements:\n”);
for(i = 0; i < n; i++)
{
scanf(“%d”, &arr[i]);
freq[i] = -1; // initialize frequency array
}
for(i = 0; i < n; i++)
{
count = 1;
for(j = i + 1; j < n; j++)
{
if(arr[i] == arr[j])
{
count++;
freq[j] = 0; // mark as counted
}
}
if(freq[i] != 0)
{
freq[i] = count;
}
}
printf(“\nFrequency of each element:\n”);
for(i = 0; i < n; i++)
{
if(freq[i] != 0)
{
printf(“%d occurs %d times\n”, arr[i], freq[i]);
}
}
return 0;
}
C Program to Check Whether a Number is Present in a List
If the number is present, the program displays its location; otherwise, it inserts the number at the end of the list.
#include <stdio.h>
int main()
{
int arr[50], n, i, num, found = 0;
printf(“Enter the number of elements: “);
scanf(“%d”, &n);
printf(“Enter the elements:\n”);
for(i = 0; i < n; i++)
{
scanf(“%d”, &arr[i]);
}
printf(“Enter the number to search: “);
scanf(“%d”, &num);
for(i = 0; i < n; i++)
{
if(arr[i] == num)
{
printf(“Number found at location %d\n”, i + 1);
found = 1;
break;
}
}
if(found == 0)
{
arr[n] = num;
n++;
printf(“Number not found. Inserted at the end.\n”);
printf(“Updated list:\n”);
for(i = 0; i < n; i++)
{
printf(“%d “, arr[i]);
}
}
return 0;
}
Example
Input
Enter the number of elements: 5
Enter the elements:
10 20 30 40 50
Enter the number to search: 30
Output
Number found at location 3
If the number is not found:
Enter the number to search: 60
Number not found. Inserted at the end.
Updated list:
10 20 30 40 50 60
C Program to Find the Sum of First and Last Digit of a Number
#include <stdio.h>
int main()
{
int num, first, last, sum;
printf(“Enter a number: “);
scanf(“%d”, &num);
last = num % 10; // get the last digit
while(num >= 10) // find the first digit
{
num = num / 10;
}
first = num;
sum = first + last;
printf(“Sum of first and last digit = %d”, sum);
return 0;
}
Example
Input
Enter a number: 4567
Output
Sum of first and last digit = 11
Explanation
- First digit = 4
- Last digit = 7
- Sum = 4 + 7 = 11
Write a C program to check whether a given number is perfect or not (A perfect number is a positive integer that is equal to the sum of its factors excluding the number itself).
- Input a number n.
- Find all factors from 1 to n-1.
- Add the factors to sum.
- If sum == n, the number is Perfect; otherwise it is not Perfect.
#include <stdio.h>
int main()
{
int n, i, sum = 0;
printf(“Enter a number: “);
scanf(“%d”, &n);
for(i = 1; i < n; i++)
{
if(n % i == 0)
{
sum = sum + i;
}
}
if(sum == n)
printf(“%d is a Perfect Number”, n);
else
printf(“%d is not a Perfect Number”, n);
return 0;
}
C Program to Find Sum and Average of an Array Using User Defined Functions
#include <stdio.h>
int findSum(int arr[], int n)
{
int i, sum = 0;
for(i = 0; i < n; i++)
{
sum = sum + arr[i];
}
return sum;
}
float findAverage(int sum, int n)
{
return (float)sum / n;
}
int main()
{
int arr[50], n, i, sum;
float avg;
printf(“Enter the number of elements: “);
scanf(“%d”, &n);
printf(“Enter the elements:\n”);
for(i = 0; i < n; i++)
{
scanf(“%d”, &arr[i]);
}
sum = findSum(arr, n); // function call to find sum
avg = findAverage(sum, n); // function call to find average
printf(“Sum = %d\n”, sum);
printf(“Average = %.2f”, avg);
return 0;
}
Explanation
- findSum() function calculates the sum of array elements.
- findAverage() function calculates the average using sum and number of elements.
- The main() function reads the array and calls the user-defined functions.
Example
Input
Enter the number of elements: 5
Enter the elements:
10 20 30 40 50
Output
Sum = 150
Average = 30.00
C program to sort an array of numbers using the Bubble Sort method.
#include <stdio.h>
int main()
{
int a[50], n, i, j, temp;
printf(“Enter the number of elements: “);
scanf(“%d”, &n);
printf(“Enter %d numbers:\n”, n);
for(i = 0; i < n; i++)
{
scanf(“%d”, &a[i]);
}
/* Bubble Sort */
for(i = 0; i < n – 1; i++)
{
for(j = 0; j < n – i – 1; j++)
{
if(a[j] > a[j + 1])
{
temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
}
}
printf(“Sorted array in ascending order:\n”);
for(i = 0; i < n; i++)
{
printf(“%d “, a[i]);
}
return 0;
}
Explanation
- The program reads n numbers into an array.
- Bubble sort repeatedly compares adjacent elements.
- If the first element is greater than the next, they are swapped.
- After each pass, the largest element moves to the end of the array.
- Finally, the sorted array is printed.
Write a C program to find the largest element in an array.
#include <stdio.h>
int main()
{
int a[100], n, i, largest;
printf(“Enter the number of elements: “);
scanf(“%d”, &n);
printf(“Enter %d elements:\n”, n);
for(i = 0; i < n; i++)
{
scanf(“%d”, &a[i]);
}
largest = a[0]; // assume first element is largest
for(i = 1; i < n; i++)
{
if(a[i] > largest)
{
largest = a[i];
}
}
printf(“The largest element is: %d”, largest);
return 0;
}
How the program works
- The user enters the number of elements in the array.
- The program reads the array elements using a for loop.
- It assumes the first element as the largest.
- It compares the remaining elements with the current largest value.
- If a bigger number is found, it updates the largest variable.
- Finally, it prints the largest element in the array.
Bottom of Form
Write a C program to :
- Create a structure containing containing the fields: Name, Price, Quantity, Total Amount.
- Use separate functions to read and print the data
#include <stdio.h>
struct item
{
char name[50];
float price;
int quantity;
float total;
};
void readData(struct item *p)
{
printf(“Enter item name: “);
scanf(“%s”, p->name);
printf(“Enter price: “);
scanf(“%f”, &p->price);
printf(“Enter quantity: “);
scanf(“%d”, &p->quantity);
p->total = p->price * p->quantity;
}
void printData(struct item p)
{
printf(“\nItem Details\n”);
printf(“Name: %s\n”, p.name);
printf(“Price: %.2f\n”, p.price);
printf(“Quantity: %d\n”, p.quantity);
printf(“Total Amount: %.2f\n”, p.total);
}
int main()
{
struct item i;
readData(&i); // function to read data
printData(i); // function to print data
return 0;
}
Explanation
- A structure item is created with fields:
- name
- price
- quantity
- total
- readData() function
- Reads the item details.
- Calculates total = price × quantity.
- printData() function
- Displays all the item details.
- In main(), the structure variable is passed to these functions to read and display the data.
Write a C program to find the transpose of a matrix.
#include <stdio.h>
int main()
{
int a[10][10], transpose[10][10];
int i, j, r, c;
printf(“Enter number of rows and columns: “);
scanf(“%d %d”, &r, &c);
printf(“Enter the elements of the matrix:\n”);
for(i = 0; i < r; i++)
{
for(j = 0; j < c; j++)
{
scanf(“%d”, &a[i][j]);
}
}
// Finding transpose
for(i = 0; i < r; i++)
{
for(j = 0; j < c; j++)
{
transpose[j][i] = a[i][j];
}
}
printf(“\nTranspose of the matrix:\n”);
for(i = 0; i < c; i++)
{
for(j = 0; j < r; j++)
{
printf(“%d “, transpose[i][j]);
}
printf(“\n”);
}
return 0;
}
Example
Input Matrix
1 2 3
4 5 6
Output (Transpose)
1 4
2 5
3 6
Explanation
- The transpose of a matrix is obtained by interchanging rows and columns.
- Element at position a[i][j] becomes transpose[j][i].
Example
Input Matrix
1 2 3
4 5 6
Output (Transpose)
1 4
2 5
3 6
Explanation
- The transpose of a matrix is obtained by interchanging rows and columns.
- Element at position a[i][j] becomes <3strong>transpose[j][i].
C program to read a character and check whether it is a vowel or consonant.
#include <stdio.h>
int main()
{
char ch;
printf(“Enter a character: “);
scanf(“%c”, &ch);
if(ch==’a’ || ch==’e’ || ch==’i’ || ch==’o’ || ch==’u’ ||
ch==’A’ || ch==’E’ || ch==’I’ || ch==’O’ || ch==’U’)
{
printf(“The character is a vowel.”);
}
else
{
printf(“The character is a consonant.”);
}
return 0;
}
Explanation
- The program reads a character from the user using scanf().
- It checks whether the character is a, e, i, o, u (both uppercase and lowercase).
- If the condition is true, it prints Vowel.
- Otherwise, it prints Consonant.
Bottom of Form
Write a C program to print number of vowels and consonants in a string
#include <stdio.h>
#include <ctype.h>
int main()
{
char str[100];
int i, vowels = 0, consonants = 0;
printf(“Enter a string: “);
fgets(str, sizeof(str), stdin);
for(i = 0; str[i] != ‘\0′; i++)
{
if(isalpha(str[i])) // checks if character is a letter
{
if(str[i]==’a’||str[i]==’e’||str[i]==’i’||str[i]==’o’||str[i]==’u’||
str[i]==’A’||str[i]==’E’||str[i]==’I’||str[i]==’O’||str[i]==’U’)
{
vowels++;
}
else
{
consonants++;
}
}
}
printf(“Number of vowels = %d\n”, vowels);
printf(“Number of consonants = %d\n”, consonants);
return 0;
}
Example
Input
Enter a string: Hello World
Output
Number of vowels = 3
Number of consonants = 7
Explanation
- The program reads a string from the user.
- It checks each character:
- If it is a vowel (a, e, i, o, u) → increment vowel count.
- If it is another alphabet → increment consonant count.
- Finally, it prints the number of vowels and consonants.
Write a C program to perform linear search on an array of numbers
#include <stdio.h>
int main()
{
int arr[50], n, i, num, found = 0;
printf(“Enter the number of elements: “);
scanf(“%d”, &n);
printf(“Enter the elements:\n”);
for(i = 0; i < n; i++)
{
scanf(“%d”, &arr[i]);
}
printf(“Enter the number to search: “);
scanf(“%d”, &num);
for(i = 0; i < n; i++)
{
if(arr[i] == num)
{
printf(“Number found at position %d”, i + 1);
found = 1;
break;
}
}
if(found == 0)
{
printf(“Number not found in the array”);
}
return 0;
}
Write a C program to reverse a string without using string handling functions
#include <stdio.h>
int main()
{
char str[100], rev[100];
int i, len = 0;
printf(“Enter a string: “);
scanf(“%s”, str);
// Find length of the string
while(str[len] != ‘\0’)
{
len++;
}
// Reverse the string
for(i = 0; i < len; i++)
{
rev[i] = str[len – 1 – i];
}
rev[i] = ‘\0’;
printf(“Reversed string = %s”, rev);
return 0;
}
Example
Input
Enter a string: hello
Output
Reversed string = olleh
Explanation
- The program first calculates the length of the string manually using a loop.
- Then it copies characters from the end of the original string to the beginning of another array.
- Finally, it prints the reversed string.
C Program to Copy Contents of One File into Another
#include <stdio.h>
int main()
{
FILE *fp1, *fp2;
char ch;
fp1 = fopen(“source.txt”, “r”); // open source file for reading
fp2 = fopen(“target.txt”, “w”); // open target file for writing
if(fp1 == NULL)
{
printf(“Source file cannot be opened.”);
return 1;
}
ch = fgetc(fp1);
while(ch != EOF)
{
fputc(ch, fp2);
ch = fgetc(fp1);
}
fclose(fp1);
fclose(fp2);
printf(“File copied successfully.”);
return 0;
}
Explanation
- fopen() opens the source file in read mode and the target file in write mode.
- fgetc() reads one character from the source file.
- fputc() writes the character into the destination file.
- The loop continues until EOF (End of File) is reached.
- fclose() closes both files after copying.
Example
If source.txt contains:
Hello C Programming
After running the program, target.txt will contain:
Hello C Programming
C program to replace vowels in a text file with the character x.
#include <stdio.h>
int main()
{
FILE *fp1, *fp2;
char ch;
fp1 = fopen(“input.txt”, “r”);
fp2 = fopen(“output.txt”, “w”);
if(fp1 == NULL || fp2 == NULL)
{
printf(“Error opening file”);
return 1;
}
while((ch = fgetc(fp1)) != EOF)
{
if(ch==’a’ || ch==’e’ || ch==’i’ || ch==’o’ || ch==’u’ ||
ch==’A’ || ch==’E’ || ch==’I’ || ch==’O’ || ch==’U’)
{
ch = ‘x’;
}
fputc(ch, fp2);
}
fclose(fp1);
fclose(fp2);
printf(“Vowels replaced successfully.”);
return 0;
}
Explanation
- fp1 opens txt in read mode.
- fp2 opens txt in write mode.
- The program reads characters one by one using fgetc().
- If the character is a vowel, it is replaced with ‘x’.
- The character is written to the new file using fputc().
- Both files are closed using fclose().
Example
Input file (input.txt)
Hello World
Output file (output.txt)
Hxllx Wxrld