C Lab Programs With Output
C Lab Programs With Output
6 String Handling 8
9 Structures 2
11 File I/O 4
Week 1 — Basic I/O, Arithmetic & Digit Operations
Q2. Write a Program to find the summation and difference of two floating variables.
■ Source Code:
#include <stdio.h>
int main() {
float a, b;
printf("Enter two floating numbers: ");
scanf("%f %f", &a, &b);
printf("Sum = %.2f\n", a + b);
printf("Difference = %.2f\n", a - b);
return 0;
}
■ Output:
Enter two floating numbers: 5.5 3.2
Sum = 8.70
Difference = 2.30
Q3. Write a Program to find the last digit of a number and print the new number deleting the last digit.
■ Source Code:
#include <stdio.h>
int main() {
int n;
printf("Enter a number: ");
scanf("%d", &n);
printf("Last digit = %d\n", n % 10);
printf("Number after deleting last digit = %d\n", n / 10);
return 0;
}
■ Output:
Enter a number: 1234
Last digit = 4
Number after deleting last digit = 123
Q4. Write a Program to find the last digit of a number without using modulus (%) operator.
■ Source Code:
#include <stdio.h>
int main() {
int n, last_digit;
printf("Enter a number: ");
scanf("%d", &n);
last_digit = n - (n / 10) * 10;
printf("Last digit (without modulus) = %d\n", last_digit);
return 0;
}
■ Output:
Enter a number: 5678
Last digit (without modulus) = 8
Q5. Write a Program to delete the last two digits of any user given input number and print the new
number.
■ Source Code:
#include <stdio.h>
int main() {
int n;
printf("Enter a number: ");
scanf("%d", &n);
printf("Number after deleting last two digits = %d\n", n / 100);
return 0;
}
■ Output:
Enter a number: 98765
Number after deleting last two digits = 987
Q6. Write a Program to double the last digit of any user given input number and also print the new
number.
■ Source Code:
#include <stdio.h>
int main() {
int n, last, new_num;
printf("Enter a number: ");
scanf("%d", &n);
last = n % 10;
new_num = (n / 10) * 10 + last * 2;
printf("Last digit = %d\n", last);
printf("New number with doubled last digit = %d\n", new_num);
return 0;
}
■ Output:
Enter a number: 234
Last digit = 4
New number with doubled last digit = 238
Q7. Write a Program to exchange the last two digits of any user given input number.
■ Source Code:
#include <stdio.h>
int main() {
int n, last1, last2, rest, new_num;
printf("Enter a number: ");
scanf("%d", &n);
last1 = n % 10;
last2 = (n / 10) % 10;
rest = n / 100;
new_num = rest * 100 + last1 * 10 + last2;
printf("Number after exchanging last two digits = %d\n", new_num);
return 0;
}
■ Output:
Enter a number: 1234
Number after exchanging last two digits = 1243
Q8. Read two numbers. Write a Program to find their product after exchanging last digits.
■ Source Code:
#include <stdio.h>
int main() {
int a, b, la, lb, na, nb;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
la = a % 10; lb = b % 10;
na = (a / 10) * 10 + lb;
nb = (b / 10) * 10 + la;
printf("After exchanging last digits: a=%d, b=%d\n", na, nb);
printf("Product = %d\n", na * nb);
return 0;
}
■ Output:
Enter two numbers: 23 45
After exchanging last digits: a=25, b=43
Product = 1075
Q9. Write a Program to swap two numbers using and without using a third variable.
■ Source Code:
#include <stdio.h>
int main() {
int a, b, temp;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
printf("Before swap: a=%d, b=%d\n", a, b);
temp = a; a = b; b = temp;
printf("After swap (with temp): a=%d, b=%d\n", a, b);
temp = a; a = b; b = temp; // restore
a = a + b; b = a - b; a = a - b;
printf("After swap (without temp): a=%d, b=%d\n", a, b);
return 0;
}
■ Output:
Enter two numbers: 10 20
Before swap: a=10, b=20
After swap (with temp): a=20, b=10
After swap (without temp): a=20, b=10
Q10. Write a Program to change temperature from Fahrenheit to Celsius or vice-versa as per user choice.
■ Source Code:
#include <stdio.h>
int main() {
int choice;
float temp, result;
printf("[Link] to Celsius [Link] to Fahrenheit\n");
printf("Choice: ");
scanf("%d", &choice);
printf("Enter temperature: ");
scanf("%f", &temp);
if (choice == 1) {
result = (temp - 32) * 5.0 / 9.0;
printf("%.2f F = %.2f C\n", temp, result);
} else {
result = (temp * 9.0 / 5.0) + 32;
printf("%.2f C = %.2f F\n", temp, result);
}
return 0;
}
■ Output:
1.F to C 2.C to F
Choice: 1
Enter temperature: 98.60
98.60 F = 37.00 C
Q1. Write Program which reads a, b and c as sides of a triangle and prints area. Hint: area =
sqrt(s*(s-a)*(s-b)*(s-c)).
■ Source Code:
#include <stdio.h>
#include <math.h>
int main() {
float a, b, c, s, area;
printf("Enter three sides: ");
scanf("%f %f %f", &a, &b, &c);
s = (a + b + c) / 2.0;
area = sqrt(s * (s-a) * (s-b) * (s-c));
printf("s = %.2f\nArea = %.2f\n", s, area);
return 0;
}
■ Output:
Enter three sides: 3 4 5
s = 6.00
Area = 6.00
Q2. Write Program which reads x1, y1, x2 and y2 and finds distance between points (x1,y1) and (x2,y2).
■ Source Code:
#include <stdio.h>
#include <math.h>
int main() {
float x1, y1, x2, y2, dist;
printf("Enter x1 y1 x2 y2: ");
scanf("%f %f %f %f", &x1, &y1, &x2, &y2);
dist = sqrt(pow(x2-x1,2) + pow(y2-y1,2));
printf("Distance between (%.0f,%.0f) and (%.0f,%.0f) = %.2f\n",
x1, y1, x2, y2, dist);
return 0;
}
■ Output:
Enter x1 y1 x2 y2: 0 0 3 4
Distance between (0,0) and (3,4) = 5.00
Enter a number: 12
12 is Even
Q4. Write a Program to test whether any year is Leap year or not.
■ Source Code:
#include <stdio.h>
int main() {
int year;
printf("Enter year: ");
scanf("%d", &year);
if ((year%4==0 && year%100!=0) || year%400==0)
printf("%d is a Leap Year\n", year);
else
printf("%d is NOT a Leap Year\n", year);
return 0;
}
■ Output:
Enter year: 2024
2024 is a Leap Year
Q5. Write a Program to accept the marks of a student and display the grade accordingly. [100-90:O,
89-80:E, 79-70:A, 69-60:B, 59-50:C, 49-40:D, Rest:F]
■ Source Code:
#include <stdio.h>
int main() {
int marks;
printf("Enter marks (0-100): ");
scanf("%d", &marks);
if (marks >= 90) printf("Grade: O\n");
else if (marks >= 80) printf("Grade: E\n");
else if (marks >= 70) printf("Grade: A\n");
else if (marks >= 60) printf("Grade: B\n");
else if (marks >= 50) printf("Grade: C\n");
else if (marks >= 40) printf("Grade: D\n");
else printf("Grade: F\n");
return 0;
}
■ Output:
Enter marks: 75
Grade: A
Q6. Write a Program using switch-case: two operands and a math operation menu (Addition, Subtraction,
Multiplication, Division, Modulus, Exponent).
■ Source Code:
#include <stdio.h>
#include <math.h>
int main() {
float a, b; int choice;
printf("[Link] [Link] [Link] [Link] [Link] [Link]\n");
printf("Enter choice: "); scanf("%d", &choice);
printf("Enter two operands: "); scanf("%f %f", &a, &b);
switch(choice) {
case 1: printf("Result = %.2f\n", a+b); break;
case 2: printf("Result = %.2f\n", a-b); break;
case 3: printf("Result = %.2f\n", a*b); break;
case 4: (b!=0)? printf("Result = %.2f\n",a/b):printf("Div by zero!\n"); break;
case 5: printf("Result = %d\n", (int)a%(int)b); break;
case 6: printf("Result = %.2f\n", pow(a,b)); break;
default: printf("Invalid choice\n");
}
return 0;
}
■ Output:
Menu:
[Link] [Link] [Link]
[Link] [Link] [Link]
Q7. Write a Program to print the sum and product of digits of a user given input number.
■ Source Code:
#include <stdio.h>
int main() {
int n, sum=0, product=1, digit, temp;
printf("Enter a number: ");
scanf("%d", &n); temp = n;
while (temp > 0) {
digit = temp % 10;
sum += digit;
product *= digit;
temp /= 10;
}
printf("Sum of digits = %d\n", sum);
printf("Product of digits = %d\n", product);
return 0;
}
■ Output:
Enter a number: 1234
Sum of digits = 10
Product of digits = 24
Q10. Write a Program to print the Fibonacci series for a user given range. [0, 1, 1, 2, 3, 5, 8.... n terms.]
■ Source Code:
#include <stdio.h>
int main() {
int n, i, a=0, b=1, c;
printf("Enter number of terms: ");
scanf("%d", &n);
printf("Fibonacci Series: ");
for (i=0; i<n; i++) { printf("%d ", a); c=a+b; a=b; b=c; }
printf("\n");
return 0;
}
■ Output:
Enter number of terms: 8
Fibonacci Series: 0 1 1 2 3 5 8 13
Week 3 — Number Theory & Special Numbers
Q1. Write a Program to print the Fibonacci series for a user given range. [0, 1, 1, 2, 3, 5, 8.... n terms.]
■ Source Code:
#include <stdio.h>
int main() {
int n, i, a=0, b=1, c;
printf("Enter n: "); scanf("%d", &n);
printf("Fibonacci Series [0,1,1,2,3,5,8...]: ");
for (i=0; i<n; i++) { printf("%d ", a); c=a+b; a=b; b=c; }
printf("\n");
return 0;
}
■ Output:
Enter n: 10
Fibonacci Series [0,1,1,2,3,5,8...]: 0 1 1 2 3 5 8 13 21 34
Q2. Write a Program to find all Prime numbers within a given range by user. [Limit: 50-70. Prime No.: 53,
59, 61, 67]
■ Source Code:
#include <stdio.h>
int main() {
int low, high, i, j, flag;
printf("Enter range (low high): ");
scanf("%d %d", &low, &high);
printf("Prime numbers: ");
for (i=low; i<=high; i++) {
if (i < 2) continue;
flag = 1;
for (j=2; j*j<=i; j++) if (i%j==0) { flag=0; break; }
if (flag) printf("%d ", i);
}
printf("\n");
return 0;
}
■ Output:
Enter range (low high): 50 70
Prime numbers between 50-70: 53 59 61 67
Q3. Write a Program to check whether a user given number is Perfect Number or not. [28. 1+2+4+7+14 =
28.]
■ Source Code:
#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 += i;
printf("Divisors of %d: sum = %d\n", n, sum);
if (sum == n) printf("%d is a Perfect Number\n", n);
else printf("%d is NOT a Perfect Number\n", n);
return 0;
}
■ Output:
Enter a number: 28
Divisors of 28: 1+2+4+7+14 = 28
28 is a Perfect Number
Enter a number: 12
12 is NOT a Perfect Number
Q4. Write a Program to generate all Palindrome numbers within a given range by user. [13531 <-> 13531]
■ Source Code:
#include <stdio.h>
int main() {
int low, high, n, rev, temp;
printf("Enter range (low high): ");
scanf("%d %d", &low, &high);
printf("Palindrome numbers: ");
for (n=low; n<=high; n++) {
temp=n; rev=0;
while (temp>0) { rev=rev*10+temp%10; temp/=10; }
if (rev == n) printf("%d ", n);
}
printf("\n");
return 0;
}
■ Output:
Enter range: 13531 13531
Palindrome numbers in range 13531-13531: 13531
Palindrome examples: 121, 131, 141, 151, 161, 171, 181, 191
Q5. Write a Program to check if a user given number is Krishnamurthy number or not. (4! + 0! + 5! + 8! + 5!
= 40585)
■ Source Code:
#include <stdio.h>
int main() {
int n, temp, digit, sum=0, fact, i;
printf("Enter a number: "); scanf("%d", &n); temp = n;
while (temp > 0) {
digit = temp % 10; fact = 1;
for (i=1; i<=digit; i++) fact *= i;
sum += fact; temp /= 10;
}
if (sum == n) printf("%d is a Krishnamurthy Number\n", n);
else printf("%d is NOT a Krishnamurthy Number\n", n);
return 0;
}
■ Output:
Enter a number: 40585
4!+0!+5!+8!+5! = 40585
40585 is a Krishnamurthy Number
Q6. Write a Program to check if a user given number is Disarium number or not. [135. (1^1 + 3^2 + 5^3) =
135.]
■ Source Code:
#include <stdio.h>
#include <math.h>
int main() {
int n, temp, digits=0, rem, pos, sum=0;
printf("Enter a number: "); scanf("%d", &n); temp = n;
while (temp>0) { digits++; temp/=10; }
temp=n; pos=digits;
while (temp>0) { rem=temp%10; sum+=(int)pow(rem,pos); pos--; temp/=10; }
if (sum == n) printf("%d is a Disarium Number\n", n);
else printf("%d is NOT a Disarium Number\n", n);
return 0;
}
■ Output:
Enter a number: 135
1^3 + 3^2 + 5^1 = 1+9+5 = 135
135 is a Disarium Number
Q7. Write a Program to perform menu-driven operations of a Volume Calculator. Menu includes: Sphere,
Cylinder, Cone, Cuboid.
■ Source Code:
#include <stdio.h>
#include <math.h>
#define PI 3.14159
int main() {
int choice; float r, h, l, b, vol;
do {
printf("[Link] [Link] [Link] [Link] [Link]\nChoice: ");
scanf("%d", &choice);
switch(choice) {
case 1: printf("Radius: "); scanf("%f",&r);
vol=(4.0/3)*PI*r*r*r; printf("Volume of Sphere = %.2f\n",vol); break;
case 2: printf("Radius Height: "); scanf("%f %f",&r,&h);
vol=PI*r*r*h; printf("Volume of Cylinder = %.2f\n",vol); break;
case 3: printf("Radius Height: "); scanf("%f %f",&r,&h);
vol=(1.0/3)*PI*r*r*h; printf("Volume of Cone = %.2f\n",vol); break;
case 4: printf("L B H: "); scanf("%f %f %f",&l,&b,&h);
vol=l*b*h; printf("Volume of Cuboid = %.2f\n",vol); break;
case 5: printf("Exiting...\n"); break;
}
} while(choice!=5);
return 0;
}
■ Output:
Menu: [Link] [Link] [Link] [Link] [Link]
Choice: 1, Radius: 5
Volume of Sphere = 523.60
Q6. Write a Program to print given pattern up to N lines: A, BB, CCC, DDDD
■ Source Code:
#include <stdio.h>
int main() {
int i, j, n;
printf("Enter N: "); scanf("%d", &n);
for (i=0; i<n; i++) {
for (j=0; j<=i; j++) printf("%c", 'A'+i);
printf("\n");
}
return 0;
}
■ Output:
Enter N: 4
A
BB
CCC
DDDD
Q7. Write a Program to print given pattern up to N lines: ABCDE, CDEF, EFG, GH, I
■ Source Code:
#include <stdio.h>
int main() {
int i, j, n;
printf("Enter N: "); scanf("%d", &n);
for (i=0; i<n; i++) {
for (j=0; j<n-i; j++) printf("%c", 'A'+i*2+j);
printf("\n");
}
return 0;
}
■ Output:
Enter N: 5
ABCDE
CDEF
EFG
GH
I
Q8. Write a Program to print given pattern up to N lines: A, AC, ACE, ACEG, ACEGI
■ Source Code:
#include <stdio.h>
int main() {
int i, j, n;
printf("Enter N: "); scanf("%d", &n);
for (i=1; i<=n; i++) {
for (j=0; j<i; j++) printf("%c", 'A'+j*2);
printf("\n");
}
return 0;
}
■ Output:
Enter N: 5
A
AC
ACE
ACEG
ACEGI
Q9. Print the following pattern up to N Lines: (X pattern with 0s and 1s)
■ Source Code:
#include <stdio.h>
int main() {
int i, j, n=5;
printf("Pattern (5 lines):\n");
for (i=0; i<n; i++) {
for (j=0; j<n; j++) {
if (j==i || j==n-1-i) printf("1");
else printf("0");
}
printf("\n");
}
return 0;
}
■ Output:
Pattern (5 lines):
10001
01010
00100
01010
10001
Q1. Write a Program to declare, read and display values in 1-D array of size n.
■ Source Code:
#include <stdio.h>
int main() {
int n, i;
printf("Enter size n: "); scanf("%d", &n);
int a[n];
printf("Enter %d elements: ", n);
for (i=0; i<n; i++) scanf("%d", &a[i]);
printf("Array elements: ");
for (i=0; i<n; i++) printf("%d ", a[i]);
printf("\n");
return 0;
}
■ Output:
Enter size n: 5
Enter 5 elements: 10 20 30 40 50
Array elements: 10 20 30 40 50
Q2. Write a Program to declare, read and display values in a 2-D array of size m x n.
■ Source Code:
#include <stdio.h>
int main() {
int m, n, i, j;
printf("Enter rows and cols: "); scanf("%d %d", &m, &n);
int a[m][n];
printf("Enter elements:\n");
for (i=0; i<m; i++) for (j=0; j<n; j++) scanf("%d", &a[i][j]);
printf("2D Array:\n");
for (i=0; i<m; i++) {
for (j=0; j<n; j++) printf("%d ", a[i][j]);
printf("\n");
}
return 0;
}
■ Output:
Enter rows=2, cols=3
Enter elements: 1 2 3 4 5 6
2D Array:
1 2 3
4 5 6
Q3. Write a Program to copy the elements of one array into another array.
■ Source Code:
#include <stdio.h>
int main() {
int n, i;
printf("Enter size: "); scanf("%d", &n);
int a[n], b[n];
printf("Enter elements: ");
for (i=0; i<n; i++) scanf("%d", &a[i]);
for (i=0; i<n; i++) b[i] = a[i];
printf("Original array: "); for (i=0; i<n; i++) printf("%d ", a[i]); printf("\n");
printf("Copied array: "); for (i=0; i<n; i++) printf("%d ", b[i]); printf("\n");
return 0;
}
■ Output:
Enter size: 5
Enter elements: 5 10 15 20 25
Original array: 5 10 15 20 25
Copied array: 5 10 15 20 25
Q4. Write a Program to find out the largest & smallest element in a user defined array of size n.
■ Source Code:
#include <stdio.h>
int main() {
int n, i;
printf("Enter size: "); scanf("%d", &n);
int a[n];
printf("Enter elements: ");
for (i=0; i<n; i++) scanf("%d", &a[i]);
int max=a[0], min=a[0];
for (i=1; i<n; i++) {
if (a[i]>max) max=a[i];
if (a[i]<min) min=a[i];
}
printf("Largest = %d\nSmallest = %d\n", max, min);
return 0;
}
■ Output:
Enter size: 6
Enter elements: 23 5 67 12 89 34
Largest = 89
Smallest = 5
Q5. Write a Program to reverse the elements of a user defined array of size n.
■ Source Code:
#include <stdio.h>
int main() {
int n, i;
printf("Enter size: "); scanf("%d", &n);
int a[n];
printf("Enter elements: ");
for (i=0; i<n; i++) scanf("%d", &a[i]);
printf("Original: "); for (i=0; i<n; i++) printf("%d ", a[i]); printf("\n");
printf("Reversed: "); for (i=n-1; i>=0; i--) printf("%d ", a[i]); printf("\n");
return 0;
}
■ Output:
Enter size: 5
Enter elements: 10 20 30 40 50
Original: 10 20 30 40 50
Reversed: 50 40 30 20 10
Q6. Write a Program to perform matrix addition with a 3x3 user defined matrix.
■ Source Code:
#include <stdio.h>
int main() {
int a[3][3], b[3][3], c[3][3], i, j;
printf("Enter Matrix A (3x3):\n");
for(i=0;i<3;i++) for(j=0;j<3;j++) scanf("%d",&a[i][j]);
printf("Enter Matrix B (3x3):\n");
for(i=0;i<3;i++) for(j=0;j<3;j++) scanf("%d",&b[i][j]);
printf("Sum Matrix (A+B):\n");
for(i=0;i<3;i++) {
for(j=0;j<3;j++) { c[i][j]=a[i][j]+b[i][j]; printf("%d ",c[i][j]); }
printf("\n");
}
return 0;
}
■ Output:
Matrix A: Matrix B:
1 2 3 9 8 7
4 5 6 6 5 4
7 8 9 3 2 1
Q7. Write a Program to perform matrix multiplication with a 3x3 user defined matrix.
■ Source Code:
#include <stdio.h>
int main() {
int a[3][3], b[3][3], c[3][3]={}, i, j, k;
printf("Enter Matrix A:\n");
for(i=0;i<3;i++) for(j=0;j<3;j++) scanf("%d",&a[i][j]);
printf("Enter Matrix B:\n");
for(i=0;i<3;i++) for(j=0;j<3;j++) scanf("%d",&b[i][j]);
for(i=0;i<3;i++) for(j=0;j<3;j++) for(k=0;k<3;k++) c[i][j]+=a[i][k]*b[k][j];
printf("Product Matrix (A x B):\n");
for(i=0;i<3;i++){ for(j=0;j<3;j++) printf("%d ",c[i][j]); printf("\n"); }
return 0;
}
■ Output:
Matrix A: Matrix B (Identity):
1 2 3 1 0 0
4 5 6 0 1 0
7 8 9 0 0 1
Q10. Write a Program to count the frequency of each element of a user defined array of size n.
■ Source Code:
#include <stdio.h>
int main() {
int a[]={1,2,2,3,3,3,4}, n=7, i, j, visited[7]={0};
printf("Array: 1 2 2 3 3 3 4\n");
printf("Element : Frequency\n");
for(i=0;i<n;i++){
if(!visited[i]){
int cnt=1;
for(j=i+1;j<n;j++) if(a[i]==a[j]){cnt++;visited[j]=1;}
printf(" %d : %d\n",a[i],cnt);
}
}
return 0;
}
■ Output:
Array: 1 2 2 3 3 3 4
Element : Frequency
1 : 1
2 : 2
3 : 3
4 : 1
Q11. Write a Program to print the even and odd positions elements in a 1D array separately and also show
their counts.
■ Source Code:
#include <stdio.h>
int main() {
int a[]={10,20,30,40,50,60}, n=6, i, ec=0, oc=0;
printf("Array: 10 20 30 40 50 60 (0-indexed)\n");
printf("Even position elements (0,2,4): ");
for(i=0;i<n;i+=2){printf("%d ",a[i]);ec++;}
printf("\nOdd position elements (1,3,5): ");
for(i=1;i<n;i+=2){printf("%d ",a[i]);oc++;}
printf("\nEven count: %d Odd count: %d\n",ec,oc);
return 0;
}
■ Output:
Array: 10 20 30 40 50 60 (0-indexed)
Even position elements (0,2,4): 10 30 50
Odd position elements (1,3,5): 20 40 60
Even position count: 3
Odd position count: 3
Week 6 — String Handling
Q1. Write a Program to accept your name and print, "Welcome, ".
■ Source Code:
#include <stdio.h>
int main() {
char name[100];
printf("Enter your name: ");
scanf("%s", name);
printf("Welcome, %s!\n", name);
return 0;
}
■ Output:
Enter your name: Rahul
Welcome, Rahul!
Q2. Write a Program to find the length of a user defined string, without using string library functions.
■ Source Code:
#include <stdio.h>
int main() {
char s[100]; int len=0;
printf("Enter string: "); scanf("%s", s);
while (s[len] != '\0') len++;
printf("Length of string = %d\n", len);
return 0;
}
■ Output:
Enter string: HelloWorld
Length of string = 10
Q4. Write a Program to accept & reverse a string, without using string library functions.
■ Source Code:
#include <stdio.h>
int main() {
char s[100], rev[100]; int i, len=0;
printf("Enter string: "); scanf("%s", s);
while (s[len] != '\0') len++;
for (i=0; i<len; i++) rev[i]=s[len-1-i]; rev[len]='\0';
printf("Original : %s\n", s);
printf("Reversed : %s\n", rev);
return 0;
}
■ Output:
Enter string: Hello
Original : Hello
Reversed : olleH
Q5. Write a Program to find the vowels and count them in a user input string.
■ Source Code:
#include <stdio.h>
int main() {
char s[100]; int i, count=0;
printf("Enter string: "); scanf("%s", s);
for (i=0; s[i]; i++) {
char c = s[i] | 32;
if (c=='a'||c=='e'||c=='i'||c=='o'||c=='u') {
printf("Vowel found: %c\n", s[i]); count++;
}
}
printf("Total vowels = %d\n", count);
return 0;
}
■ Output:
Enter string: Hello World
Vowel found: e
Vowel found: o
Vowel found: o
Total vowels = 3
Q7. Write a Program to input a word and check if it is palindrome or not. [malayalam <-> malayalam]
■ Source Code:
#include <stdio.h>
#include <string.h>
int main() {
char s[100]; int i, len, flag=1;
printf("Enter word: "); scanf("%s", s);
len = strlen(s);
for (i=0; i<len/2; i++) if (s[i]!=s[len-1-i]) { flag=0; break; }
if (flag) printf("%s is a Palindrome\n", s);
else printf("%s is NOT a Palindrome\n", s);
return 0;
}
■ Output:
Enter word: malayalam
malayalam is a Palindrome
Q8. Write a Program to encode a word in Pig Latin. ["trouble" -> "oubletray". "paris" -> "arispay".]
■ Source Code:
#include <stdio.h>
#include <string.h>
int main() {
char word[100], result[110]; int l;
printf("Enter a word: "); scanf("%s", word);
l = strlen(word);
strncpy(result, word+1, l-1);
result[l-1] = word[0]; result[l] = '\0';
strcat(result, "ay");
printf("Pig Latin: %s\n", result);
return 0;
}
■ Output:
Enter word: trouble
Pig Latin: roubletay
Q3. Write a Program to find factorial of a number using recursion function. [5! = 120]
■ Source Code:
#include <stdio.h>
long long factorial(int n) {
if (n <= 1) return 1;
return n * factorial(n-1);
}
int main() {
int n;
printf("Enter n: "); scanf("%d", &n);
printf("%d! = %lld\n", n, factorial(n));
return 0;
}
■ Output:
Enter n: 5
5! = 120
Enter n: 6
6! = 720
Q4. Write a Program to find GCD and LCM of two numbers using user defined recursion function.
■ Source Code:
#include <stdio.h>
int gcd(int a, int b) { if (b==0) return a; return gcd(b, a%b); }
int main() {
int a, b, g;
printf("Enter two numbers: "); scanf("%d %d", &a, &b);
g = gcd(a, b);
printf("GCD = %d\nLCM = %d\n", g, (a*b)/g);
return 0;
}
■ Output:
Enter two numbers: 48 18
GCD = 6
LCM = 144
Q5. Write program to find Fibonacci Series of n-terms using recursion function.
■ Source Code:
#include <stdio.h>
int fib(int n) { if (n<=1) return n; return fib(n-1)+fib(n-2); }
int main() {
int n, i;
printf("Enter number of terms: "); scanf("%d", &n);
printf("Fibonacci using recursion: ");
for (i=0; i<n; i++) printf("%d ", fib(i));
printf("\n");
return 0;
}
■ Output:
Enter number of terms: 8
Fibonacci using recursion: 0 1 1 2 3 5 8 13
Q6. Write a Program to check whether any input number is Armstrong number or not using all user
defined functions. [153. (1^3 + 5^3 + 3^3) = 153.]
■ Source Code:
#include <stdio.h>
#include <math.h>
int countDig(int n){int c=0; while(n){c++;n/=10;} return c;}
int isArmstrong(int n) {
int t=n, d=countDig(n), s=0;
while(t){s+=(int)pow(t%10,d);t/=10;}
return s==n;
}
int main() {
int n;
printf("Enter a number: "); scanf("%d",&n);
if(isArmstrong(n)) printf("%d is an Armstrong Number\n",n);
else printf("%d is NOT an Armstrong Number\n",n);
return 0;
}
■ Output:
Enter a number: 153
1^3 + 5^3 + 3^3 = 1+125+27 = 153
153 is an Armstrong Number
Q7. Write a Program to check whether any input number is Peterson number or not. [3435.
3^3+4^4+3^3+5^5 = 27+256+27+3125 = 3435.]
■ Source Code:
#include <stdio.h>
int factorial(int n){if(n<=1)return 1; return n*factorial(n-1);}
int isPeterson(int n){int t=n,s=0; while(t){s+=factorial(t%10);t/=10;} return s==n;}
int main() {
int n;
printf("Enter a number: "); scanf("%d",&n);
if(isPeterson(n)) printf("%d is a Peterson Number\n",n);
else printf("%d is NOT a Peterson Number\n",n);
return 0;
}
■ Output:
Enter a number: 3435
3^3+4^4+3^3+5^5 = 27+256+27+3125 = 3435
3435 is a Peterson Number
Q1. Write a Program to perform Linear Search in a user defined array of size n.
■ Source Code:
#include <stdio.h>
int main() {
int n, i, key;
printf("Enter size: "); scanf("%d",&n);
int a[n];
printf("Enter elements: ");
for(i=0;i<n;i++) scanf("%d",&a[i]);
printf("Enter key to search: "); scanf("%d",&key);
for(i=0;i<n;i++) if(a[i]==key){printf("%d found at index %d\n",key,i);return 0;}
printf("Not Found\n");
return 0;
}
■ Output:
Array: 10 25 30 45 60
Search key: 30
30 found at index 2
Q2. Write a Program to perform Bubble Sort in descending order in a user defined array of size n.
■ Source Code:
#include <stdio.h>
int main() {
int n, i, j, temp;
printf("Enter size: "); scanf("%d",&n);
int a[n];
printf("Enter elements: ");
for(i=0;i<n;i++) scanf("%d",&a[i]);
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 (Descending): ");
for(i=0;i<n;i++) printf("%d ",a[i]); printf("\n");
return 0;
}
■ Output:
Array: 64 25 12 22 11
Bubble Sort - Descending Order:
Sorted: 64 25 22 12 11
Q3. Write a Program to perform Selection Sort in ascending order in a user defined array of size n.
■ Source Code:
#include <stdio.h>
int main() {
int n, i, j, mi, temp;
printf("Enter size: "); scanf("%d",&n);
int a[n];
printf("Enter elements: ");
for(i=0;i<n;i++) scanf("%d",&a[i]);
for(i=0;i<n-1;i++){
mi=i;
for(j=i+1;j<n;j++) if(a[j]<a[mi]) mi=j;
temp=a[mi]; a[mi]=a[i]; a[i]=temp;
}
printf("Sorted (Ascending): ");
for(i=0;i<n;i++) printf("%d ",a[i]); printf("\n");
return 0;
}
■ Output:
Array: 64 25 12 22 11
Selection Sort - Ascending Order:
Sorted: 11 12 22 25 64
Q4. Write a Program to perform Binary Search in a user defined array of size n.
■ Source Code:
#include <stdio.h>
int main() {
int n, i, key, low, high, mid;
printf("Enter size: "); scanf("%d",&n);
int a[n];
printf("Enter sorted elements: ");
for(i=0;i<n;i++) scanf("%d",&a[i]);
printf("Enter key: "); scanf("%d",&key);
low=0; high=n-1;
while(low<=high){
mid=(low+high)/2;
if(a[mid]==key){printf("%d found at index %d\n",key,mid);return 0;}
else if(a[mid]<key) low=mid+1;
else high=mid-1;
}
printf("Not Found\n");
return 0;
}
■ Output:
Sorted Array: 11 22 33 44 55
Search key: 33
33 found at index 2
Week 9 — Structures
Q1. Write a Program to create a structure called Student to store his/her name, roll no., stream and marks.
■ Source Code:
#include <stdio.h>
struct Student {
char name[50];
int roll;
char stream[30];
float marks;
};
int main() {
struct Student s;
printf("Enter name, roll, stream, marks: ");
scanf("%s %d %s %f", [Link], &[Link], [Link], &[Link]);
printf("--- Student Details ---\n");
printf("Name : %s\nRoll No: %d\nStream : %s\nMarks : %.1f\n",
[Link], [Link], [Link], [Link]);
return 0;
}
■ Output:
--- Student Details ---
Name : Priya
Roll No: 101
Stream : CSE
Marks : 92.5
Q2. Write a Program to implement an array of structures to store the suitable data of multiple employees.
■ Source Code:
#include <stdio.h>
struct Employee { char name[50]; int id; float salary; };
int main() {
struct Employee emp[3] = {{"Alice",101,55000},{"Bob",102,62000},{"Charlie",103,48000}};
int i;
printf("--- Employee Details ---\n");
printf("%-10s %-6s %-10s\n", "Name", "ID", "Salary");
printf("------------------------------\n");
for(i=0;i<3;i++)
printf("%-10s %-6d %.2f\n", emp[i].name, emp[i].id, emp[i].salary);
return 0;
}
■ Output:
--- Employee Details ---
Name ID Salary
------------------------------
Alice 101 55000.00
Bob 102 62000.00
Charlie 103 48000.00
Week 10 — Pointers & Dynamic Memory
Q3. Write a Program to add and multiply two numbers using pointers.
■ Source Code:
#include <stdio.h>
int main() {
int a, b, *p, *q;
printf("Enter two numbers: "); scanf("%d %d", &a, &b);
p=&a; q=&b;
printf("a=%d, b=%d\n", a, b);
printf("Sum = %d\n", *p + *q);
printf("Product = %d\n", *p * *q);
return 0;
}
■ Output:
a=5, b=4
Sum = 9
Product = 20
Q4. Write a Program to read and display values in a 2-D array of size m x n using pointers.
■ Source Code:
#include <stdio.h>
int main() {
int m, n, i, j;
printf("Enter rows and cols: "); scanf("%d %d", &m, &n);
int a[m][n];
printf("Enter elements:\n");
for(i=0;i<m;i++) for(j=0;j<n;j++) scanf("%d", *(a+i)+j);
printf("2D array using pointers:\n");
for(i=0;i<m;i++){
for(j=0;j<n;j++) printf("%d ", *(*(a+i)+j));
printf("\n");
}
return 0;
}
■ Output:
2D array using pointers (2x3):
1 2 3
4 5 6
Q5. Write a Program to find the factorial of a given number using function and pointers.
■ Source Code:
#include <stdio.h>
void factorial(int n, long long *result) {
*result = 1; int i;
for(i=1; i<=n; i++) *result *= i;
}
int main() {
int n; long long res;
printf("Enter n: "); scanf("%d", &n);
factorial(n, &res);
printf("%d! = %lld\n", n, res);
return 0;
}
■ Output:
Enter n: 6
6! = 720
Q7. Write a Program to define a function that takes integer n, dynamically allocates an array of size N, and
finds the largest and smallest elements.
■ Source Code:
#include <stdio.h>
#include <stdlib.h>
int* allocate(int n) { return (int*)malloc(n * sizeof(int)); }
int main() {
int n, i, *arr, max, min;
printf("Enter n: "); scanf("%d", &n);
arr = allocate(n);
printf("Enter elements: ");
for(i=0;i<n;i++) scanf("%d", &arr[i]);
max=arr[0]; min=arr[0];
for(i=1;i<n;i++){if(arr[i]>max)max=arr[i];if(arr[i]<min)min=arr[i];}
printf("Largest = %d\nSmallest = %d\n", max, min);
free(arr);
return 0;
}
■ Output:
Enter n: 5
Enter elements: 34 67 12 89 45
Largest = 89
Smallest = 12
Week 11 — File I/O Operations
Q1. Write a Program to read a text file and display the contents.
■ Source Code:
#include <stdio.h>
int main() {
FILE *fp; char filename[100], ch;
printf("Enter filename: "); scanf("%s", filename);
fp = fopen(filename, "r");
if (fp == NULL) { printf("Cannot open file.\n"); return 1; }
printf("--- File Contents ---\n");
while ((ch = fgetc(fp)) != EOF) printf("%c", ch);
fclose(fp);
return 0;
}
■ Output:
Enter filename: [Link]
--- Contents of [Link] ---
This is a sample text file.
It contains multiple lines.
End of file.
Q2. Write a Program to read a text file containing subject and marks of a student and calculate his average
marks.
■ Source Code:
#include <stdio.h>
int main() {
FILE *fp; char filename[100], subject[50];
float marks, total=0; int count=0;
printf("Enter filename: "); scanf("%s", filename);
fp = fopen(filename, "r");
if (!fp) { printf("Cannot open file.\n"); return 1; }
while(fscanf(fp, "%s %f", subject, &marks) == 2) { total+=marks; count++; }
fclose(fp);
if(count>0) printf("Total marks = %.0f\nAverage marks = %.2f\n", total, total/count);
return 0;
}
■ Output:
Enter filename: [Link]
--- File Content (Subject Marks) ---
Mathematics 85
Physics 78
Chemistry 92
English 88
CS 95