BCS151 / BCS251: PROGRAMMING FOR PROBLEM SOLVING LAB
AKTU — Complete C Programs with Output
Q1. WAP that accepts the marks of 5 subjects and finds the sum and percentage marks obtained by
the student.
#include
int main() {
int m1, m2, m3, m4, m5, sum;
float percent;
printf("Enter marks of 5 subjects: ");
scanf("%d %d %d %d %d", &m1;, &m2;, &m3;, &m4;, &m5;);
sum = m1 + m2 + m3 + m4 + m5;
percent = (sum / 500.0) * 100;
printf("Sum = %d\n", sum);
printf("Percentage = %.2f%%\n", percent);
return 0;
}
Output:
Enter marks of 5 subjects: 80 75 90 85 70
Sum = 400
Percentage = 80.00%
Q2. WAP that calculates the Simple Interest and Compound Interest.
#include
#include
int main() {
float p, r, t, si, ci;
printf("Enter Principal, Rate, Time: ");
scanf("%f %f %f", &p;, &r;, &t;);
si = (p * r * t) / 100;
ci = p * pow((1 + r / 100), t) - p;
printf("Simple Interest = %.2f\n", si);
printf("Compound Interest = %.2f\n", ci);
return 0;
}
Output:
Enter Principal, Rate, Time: 1000 10 2
Simple Interest = 200.00
Compound Interest = 210.00
Q3. WAP to calculate the area and circumference of a circle.
#include
#define PI 3.14159
int main() {
float r, area, circ;
printf("Enter radius: ");
scanf("%f", &r;);
area = PI * r * r;
circ = 2 * PI * r;
printf("Area = %.2f\n", area);
printf("Circumference = %.2f\n", circ);
return 0;
}
Output:
Enter radius: 7
Area = 153.94
Circumference = 43.98
Q4. WAP that accepts temperature in Centigrade and converts it into Fahrenheit using C/5=(F-32)/9.
#include
int main() {
float c, f;
printf("Enter temperature in Celsius: ");
scanf("%f", &c;);
f = (c * 9 / 5) + 32;
printf("Temperature in Fahrenheit = %.2f\n", f);
return 0;
}
Output:
Enter temperature in Celsius: 37
Temperature in Fahrenheit = 98.60
Q5. WAP that swaps values of two variables using a third variable.
#include
int main() {
int a, b, temp;
printf("Enter two numbers: ");
scanf("%d %d", &a;, &b;);
temp = a;
a = b;
b = temp;
printf("After swap: a = %d, b = %d\n", a, b);
return 0;
}
Output:
Enter two numbers: 10 20
After swap: a = 20, b = 10
Q6. WAP that checks whether the two numbers entered by the user are equal or not.
#include
int main() {
int a, b;
printf("Enter two numbers: ");
scanf("%d %d", &a;, &b;);
if (a == b)
printf("Numbers are equal\n");
else
printf("Numbers are not equal\n");
return 0;
}
Output:
Enter two numbers: 15 15
Numbers are equal
Q7. WAP to find the greatest of three numbers.
#include
int main() {
int a, b, c;
printf("Enter three numbers: ");
scanf("%d %d %d", &a;, &b;, &c;);
if (a >= b && a >= c)
printf("Greatest = %d\n", a);
else if (b >= a && b >= c)
printf("Greatest = %d\n", b);
else
printf("Greatest = %d\n", c);
return 0;
}
Output:
Enter three numbers: 12 45 33
Greatest = 45
Q8. WAP that finds whether a given number is even or odd.
#include
int main() {
int n;
printf("Enter a number: ");
scanf("%d", &n;);
if (n % 2 == 0)
printf("%d is Even\n", n);
else
printf("%d is Odd\n", n);
return 0;
}
Output:
Enter a number: 7
7 is Odd
Q9. WAP that tells whether a given year is a leap year or not.
#include
int main() {
int year;
printf("Enter a 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 a year: 2024
2024 is a Leap Year
Q10. WAP that accepts marks of 5 subjects, finds percentage, and prints grades (A/B/C/D).
#include
int main() {
int m1, m2, m3, m4, m5, sum;
float percent;
printf("Enter marks of 5 subjects: ");
scanf("%d %d %d %d %d", &m1;, &m2;, &m3;, &m4;, &m5;);
sum = m1 + m2 + m3 + m4 + m5;
percent = (sum / 500.0) * 100;
printf("Percentage = %.2f%%\n", percent);
if (percent >= 90)
printf("Grade: A\n");
else if (percent >= 80)
printf("Grade: B\n");
else if (percent >= 60)
printf("Grade: C\n");
else
printf("Grade: D\n");
return 0;
}
Output:
Enter marks of 5 subjects: 95 90 88 92 91
Percentage = 91.20%
Grade: A
Q11. WAP that takes two operands and one operator, performs the operation using Switch
statement.
#include
int main() {
float a, b, result;
char op;
printf("Enter operand1 operator operand2: ");
scanf("%f %c %f", &a;, &op;, &b;);
switch (op) {
case '+': result = a + b; break;
case '-': result = a - b; break;
case '*': result = a * b; break;
case '/':
if (b != 0) result = a / b;
else { printf("Division by zero!\n"); return 0; }
break;
default: printf("Invalid operator\n"); return 0;
}
printf("Result = %.2f\n", result);
return 0;
}
Output:
Enter operand1 operator operand2: 10 + 5
Result = 15.00
Q12. WAP to print the sum of all numbers up to a given number.
#include
int main() {
int n, i, sum = 0;
printf("Enter a number: ");
scanf("%d", &n;);
for (i = 1; i <= n; i++)
sum = sum + i;
printf("Sum = %d\n", sum);
return 0;
}
Output:
Enter a number: 10
Sum = 55
Q13. WAP to find the factorial of a given number.
#include
int main() {
int n, i;
long long fact = 1;
printf("Enter a number: ");
scanf("%d", &n;);
for (i = 1; i <= n; i++)
fact = fact * i;
printf("Factorial of %d = %lld\n", n, fact);
return 0;
}
Output:
Enter a number: 6
Factorial of 6 = 720
Q14. WAP to print sum of even and odd numbers from 1 to N.
#include
int main() {
int n, i, even_sum = 0, odd_sum = 0;
printf("Enter N: ");
scanf("%d", &n;);
for (i = 1; i <= n; i++) {
if (i % 2 == 0)
even_sum = even_sum + i;
else
odd_sum = odd_sum + i;
}
printf("Sum of even numbers = %d\n", even_sum);
printf("Sum of odd numbers = %d\n", odd_sum);
return 0;
}
Output:
Enter N: 10
Sum of even numbers = 30
Sum of odd numbers = 25
Q15. WAP to print the Fibonacci series.
#include
int main() {
int n, i, a = 0, b = 1, c;
printf("Enter number of terms: ");
scanf("%d", &n;);
printf("Fibonacci Series: ");
for (i = 1; 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
Q16. WAP to check whether the entered number is prime or not.
#include
int main() {
int n, i, flag = 0;
printf("Enter a number: ");
scanf("%d", &n;);
for (i = 2; i <= n / 2; i++) {
if (n % i == 0) {
flag = 1;
break;
}
}
if (flag == 0 && n > 1)
printf("%d is a Prime number\n", n);
else
printf("%d is not a Prime number\n", n);
return 0;
}
Output:
Enter a number: 17
17 is a Prime number
Q17. WAP to find the sum of digits of the entered number.
#include
int main() {
int n, sum = 0, digit;
printf("Enter a number: ");
scanf("%d", &n;);
while (n != 0) {
digit = n % 10;
sum = sum + digit;
n = n / 10;
}
printf("Sum of digits = %d\n", sum);
return 0;
}
Output:
Enter a number: 1234
Sum of digits = 10
Q18. WAP to find the reverse of a number.
#include
int main() {
int n, rev = 0, digit;
printf("Enter a number: ");
scanf("%d", &n;);
while (n != 0) {
digit = n % 10;
rev = rev * 10 + digit;
n = n / 10;
}
printf("Reversed number = %d\n", rev);
return 0;
}
Output:
Enter a number: 4567
Reversed number = 7654
Q19. WAP to print Armstrong numbers from 1 to 100.
#include
int main() {
int i, n, digit, sum;
printf("Armstrong numbers from 1 to 100:\n");
for (i = 1; i <= 100; i++) {
n = i;
sum = 0;
while (n != 0) {
digit = n % 10;
sum = sum + digit * digit * digit;
n = n / 10;
}
if (sum == i)
printf("%d ", i);
}
printf("\n");
return 0;
}
Output:
Armstrong numbers from 1 to 100:
1 153 (Note: 153 > 100, so only: 1)
Q20. WAP to convert binary number into decimal and vice versa.
#include
#include
int main() {
int choice, n, decimal = 0, binary = 0, i = 0, rem;
printf("1. Binary to Decimal\n2. Decimal to Binary\nChoice: ");
scanf("%d", &choice;);
if (choice == 1) {
printf("Enter binary number: ");
scanf("%d", &n;);
int temp = n;
while (temp != 0) {
rem = temp % 10;
decimal = decimal + rem * pow(2, i);
temp = temp / 10;
i++;
}
printf("Decimal = %d\n", decimal);
} else {
printf("Enter decimal number: ");
scanf("%d", &n;);
int temp = n;
while (temp != 0) {
rem = temp % 2;
binary = binary + rem * pow(10, i);
temp = temp / 2;
i++;
}
printf("Binary = %d\n", binary);
}
return 0;
}
Output:
1. Binary to Decimal
2. Decimal to Binary
Choice: 1
Enter binary number: 1010
Decimal = 10
Q21. WAP that takes elements of an array from the user and finds the sum of these elements.
#include
int main() {
int n, i, sum = 0;
printf("Enter number of elements: ");
scanf("%d", &n;);
int arr[n];
printf("Enter elements: ");
for (i = 0; i < n; i++)
scanf("%d", &arr;[i]);
for (i = 0; i < n; i++)
sum = sum + arr[i];
printf("Sum = %d\n", sum);
return 0;
}
Output:
Enter number of elements: 5
Enter elements: 10 20 30 40 50
Sum = 150
Q22. WAP that inputs two arrays and saves sum of corresponding elements in a third array.
#include
int main() {
int n, i;
printf("Enter number of elements: ");
scanf("%d", &n;);
int a[n], b[n], c[n];
printf("Enter elements of array 1: ");
for (i = 0; i < n; i++) scanf("%d", &a;[i]);
printf("Enter elements of array 2: ");
for (i = 0; i < n; i++) scanf("%d", &b;[i]);
for (i = 0; i < n; i++)
c[i] = a[i] + b[i];
printf("Sum array: ");
for (i = 0; i < n; i++)
printf("%d ", c[i]);
printf("\n");
return 0;
}
Output:
Enter number of elements: 3
Enter elements of array 1: 1 2 3
Enter elements of array 2: 4 5 6
Sum array: 5 7 9
Q23. WAP to find the minimum and maximum element of the array.
#include
int main() {
int n, i, min, max;
printf("Enter number of elements: ");
scanf("%d", &n;);
int arr[n];
printf("Enter elements: ");
for (i = 0; i < n; i++)
scanf("%d", &arr;[i]);
min = arr[0];
max = arr[0];
for (i = 1; i < n; i++) {
if (arr[i] < min) min = arr[i];
if (arr[i] > max) max = arr[i];
}
printf("Minimum = %d\n", min);
printf("Maximum = %d\n", max);
return 0;
}
Output:
Enter number of elements: 5
Enter elements: 34 12 78 5 56
Minimum = 5
Maximum = 78
Q24. WAP to search an element in an array using Linear Search.
#include
int main() {
int n, i, key, found = 0;
printf("Enter number of elements: ");
scanf("%d", &n;);
int arr[n];
printf("Enter elements: ");
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 found at position %d\n", i + 1);
found = 1;
break;
}
}
if (found == 0)
printf("Element not found\n");
return 0;
}
Output:
Enter number of elements: 5
Enter elements: 10 20 30 40 50
Enter element to search: 30
Element found at position 3
Q25. WAP to sort the elements of the array in ascending order using Bubble Sort.
#include
int main() {
int n, i, j, temp;
printf("Enter number of elements: ");
scanf("%d", &n;);
int arr[n];
printf("Enter elements: ");
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("Sorted array: ");
for (i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
return 0;
}
Output:
Enter number of elements: 5
Enter elements: 64 34 25 12 22
Sorted array: 12 22 25 34 64
Q26. WAP to add and multiply two matrices of order n x n.
#include
int main() {
int n, i, j, k;
printf("Enter order n: ");
scanf("%d", &n;);
int a[n][n], b[n][n], add[n][n], mul[n][n];
printf("Enter matrix A:\n");
for (i = 0; i < n; i++)
for (j = 0; j < n; j++)
scanf("%d", &a;[i][j]);
printf("Enter matrix B:\n");
for (i = 0; i < n; i++)
for (j = 0; j < n; j++)
scanf("%d", &b;[i][j]);
for (i = 0; i < n; i++)
for (j = 0; j < n; j++) {
add[i][j] = a[i][j] + b[i][j];
mul[i][j] = 0;
for (k = 0; k < n; k++)
mul[i][j] += a[i][k] * b[k][j];
}
printf("Addition:\n");
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) printf("%d ", add[i][j]);
printf("\n");
}
printf("Multiplication:\n");
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) printf("%d ", mul[i][j]);
printf("\n");
}
return 0;
}
Output:
Enter order n: 2
Enter matrix A:
1 2
3 4
Enter matrix B:
5 6
7 8
Addition:
6 8
10 12
Multiplication:
19 22
43 50
Q27. WAP that finds the sum of diagonal elements of a m x n matrix.
#include
int main() {
int m, n, i, j, sum = 0;
printf("Enter rows and columns: ");
scanf("%d %d", &m;, &n;);
int a[m][n];
printf("Enter matrix elements:\n");
for (i = 0; i < m; i++)
for (j = 0; j < n; j++)
scanf("%d", &a;[i][j]);
for (i = 0; i < m; i++)
for (j = 0; j < n; j++)
if (i == j)
sum = sum + a[i][j];
printf("Sum of diagonal elements = %d\n", sum);
return 0;
}
Output:
Enter rows and columns: 3 3
Enter matrix elements:
1 2 3
4 5 6
7 8 9
Sum of diagonal elements = 15
Q28. WAP to implement strlen(), strcat(), strcpy() using the concept of Functions.
#include
int myStrlen(char s[]) {
int len = 0;
while (s[len] != '\0') len++;
return len;
}
void myStrcpy(char dest[], char src[]) {
int i = 0;
while (src[i] != '\0') { dest[i] = src[i]; i++; }
dest[i] = '\0';
}
void myStrcat(char dest[], char src[]) {
int i = 0, j = 0;
while (dest[i] != '\0') i++;
while (src[j] != '\0') { dest[i] = src[j]; i++; j++; }
dest[i] = '\0';
}
int main() {
char s1[50] = "Hello";
char s2[20] = " World";
char s3[20];
printf("Length of s1 = %d\n", myStrlen(s1));
myStrcpy(s3, s1);
printf("Copied string = %s\n", s3);
myStrcat(s1, s2);
printf("Concatenated string = %s\n", s1);
return 0;
}
Output:
Length of s1 = 5
Copied string = Hello
Concatenated string = Hello World
Q29. Define a structure TRAIN_INFO with Train No., Train Name, Departure Time, Arrival Time, Start
Station, End Station and implement operations on train timetable.
#include
#include
struct Time {
int hour, minute;
};
struct TRAIN_INFO {
int trainNo;
char trainName[50];
struct Time departure;
struct Time arrival;
char startStation[30];
char endStation[30];
};
int main() {
int n, i;
printf("Enter number of trains: ");
scanf("%d", &n;);
struct TRAIN_INFO trains[n];
for (i = 0; i < n; i++) {
printf("Train No: "); scanf("%d", &trains;[i].trainNo);
printf("Train Name: "); scanf("%s", trains[i].trainName);
printf("Departure (hh mm): ");
scanf("%d %d", &trains;[i].[Link], &trains;[i].[Link]);
printf("Arrival (hh mm): ");
scanf("%d %d", &trains;[i].[Link], &trains;[i].[Link]);
printf("Start Station: "); scanf("%s", trains[i].startStation);
printf("End Station: "); scanf("%s", trains[i].endStation);
}
char section[30];
printf("\nEnter section (start station) to search: ");
scanf("%s", section);
printf("Trains from %s:\n", section);
for (i = 0; i < n; i++) {
if (strcmp(trains[i].startStation, section) == 0)
printf("Train %d: %s Dep: %02d:%02d Arr: %02d:%02d -> %s\n",
trains[i].trainNo, trains[i].trainName,
trains[i].[Link], trains[i].[Link],
trains[i].[Link], trains[i].[Link],
trains[i].endStation);
}
return 0;
}
Output:
Enter number of trains: 1
Train No: 12345
Train Name: Shatabdi
Departure (hh mm): 6 00
Arrival (hh mm): 10 30
Start Station: Delhi
End Station: Agra
Enter section to search: Delhi
Trains from Delhi:
Train 12345: Shatabdi Dep: 06:00 Arr: 10:30 -> Agra
Q30. WAP to swap two elements using the concept of pointers.
#include
void swap(int *x, int *y) {
int temp;
temp = *x;
*x = *y;
*y = temp;
}
int main() {
int a, b;
printf("Enter two numbers: ");
scanf("%d %d", &a;, &b;);
printf("Before swap: a = %d, b = %d\n", a, b);
swap(&a;, &b;);
printf("After swap: a = %d, b = %d\n", a, b);
return 0;
}
Output:
Enter two numbers: 5 9
Before swap: a = 5, b = 9
After swap: a = 9, b = 5
Q31. WAP to compare the contents of two files and determine whether they are same or not.
#include
int main() {
FILE *f1, *f2;
char c1, c2;
int same = 1;
f1 = fopen("[Link]", "r");
f2 = fopen("[Link]", "r");
if (f1 == NULL || f2 == NULL) {
printf("Error opening file\n");
return 0;
}
while (1) {
c1 = fgetc(f1);
c2 = fgetc(f2);
if (c1 == EOF && c2 == EOF) break;
if (c1 != c2) { same = 0; break; }
}
if (same)
printf("Files are same\n");
else
printf("Files are different\n");
fclose(f1);
fclose(f2);
return 0;
}
Output:
([Link] and [Link] contain same content)
Files are same
Q32. WAP to check whether a given word exists in a file or not. If yes then find the number of times
it occurs.
#include
#include
int main() {
FILE *fp;
char word[50], fileWord[50];
int count = 0;
printf("Enter word to search: ");
scanf("%s", word);
fp = fopen("[Link]", "r");
if (fp == NULL) {
printf("Error opening file\n");
return 0;
}
while (fscanf(fp, "%s", fileWord) != EOF) {
if (strcmp(fileWord, word) == 0)
count++;
}
if (count > 0)
printf("'%s' found %d time(s) in file\n", word, count);
else
printf("'%s' not found in file\n", word);
fclose(fp);
return 0;
}
Output:
Enter word to search: hello
'hello' found 3 time(s) in file