0% found this document useful (0 votes)
3 views37 pages

Programming for Problem Solving Lab File (2)

The document contains a series of C programming exercises that cover various topics such as calculating marks, simple interest, area and circumference of a circle, temperature conversion, swapping variables, checking equality, finding the greatest number, determining even or odd numbers, and more. Each exercise includes the code implementation, expected output, and user prompts. The exercises are designed to help learners practice fundamental programming concepts and problem-solving skills.

Uploaded by

rajrahul91164
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views37 pages

Programming for Problem Solving Lab File (2)

The document contains a series of C programming exercises that cover various topics such as calculating marks, simple interest, area and circumference of a circle, temperature conversion, swapping variables, checking equality, finding the greatest number, determining even or odd numbers, and more. Each exercise includes the code implementation, expected output, and user prompts. The exercises are designed to help learners practice fundamental programming concepts and problem-solving skills.

Uploaded by

rajrahul91164
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

1. WAP to accept marks of 5 subjects and find the sum and percentage.

Code:

#include<stdio.h>
#include<conio.h>
int main()
{
clrscr();
int s1, s2, s3, s4, s5;
int sum;
float percentage;
printf("Enter marks of Subject 1: ");
scanf("%d", &s1);
printf("Enter marks of Subject 2: ");
scanf("%d", &s2);
printf("Enter marks of Subject 3: ");
scanf("%d", &s3);
printf("Enter marks of Subject 4: ");
scanf("%d", &s4);
printf("Enter marks of Subject 5: ");
scanf("%d", &s5);
sum = s1 + s2 + s3 + s4 + s5;
percentage = (sum / 5.0);
printf("Total Marks = %d\n", sum);
printf("Percentage = %.2f%%\n", percentage);
getch();
return 0;
}

Output:

Enter marks of Subject 1: 67


Enter marks of Subject 2: 80
Enter marks of Subject 3: 60
Enter marks of Subject 4: 78
Enter marks of Subject 5: 76
Total Marks = 361
Percentage = 72.20%

1
2. WAP that calculates the Simple Interest. The Principal, Amount, Rate of Interest
and Time are entered through the keyboard.

Code:
#include<stdio.h>
#include<conio.h>
int main()
{
float principal, rate, time, si;
clrscr();
printf("Enter the principal amount: ");
scanf("%f", &principal);
printf("Enter the rate of interest: ");
scanf("%f", &rate);
printf("Enter time (in years): ");
scanf("%f", &time);
si = (principal * rate * time) / 100;
printf("Simple Interest = %f", si);
getch();
return 0;
}

Output:
Enter the principal amount = 100000
Enter the rate of interest = 3
Enter time (in years) = 1 year
Simple interest = 3000.00

2
3. WAP to calculate the area and circumference of a circle.

Code:
#include<stdio.h>
#include<conio.h>
int main()
{
clrscr();
float radius, area, circumference;
const float pi = 3.14;
printf("Enter the value of radius: ");
scanf("%f", &radius);
area = pi * radius * radius;
circumference = 2 * pi * radius;
printf("Area = %.2f\n", area);
printf("Circumference = %.2f\n", circumference);
getch();
return 0;
}

Output:
Enter the value of radius = 25
Area = 1962.50
Circumference = 157.00

3
4. Write a program to find the temperature that accept in centigrade and converts
into Fahrenheit using the formula.
C / = (F - 32) / 9 × 5

Code:
#include<stdio.h>
#include<conio.h>
int main()
{
clrscr();
float fahrenheit, celsius;
printf("Enter temperature in fahrenheit: ");
scanf("%f", &fahrenheit);
celsius = (fahrenheit - 32) * 5 / 9;
printf("Celsius = %0.3f", celsius);
getch();
return 0;
}

Output:
Enter temperature in fahrenheit = 98.6
Celsius = 37.111

4
5. WAP that swaps value of two variables using a third variable.

Code:
#include<stdio.h>
#include<conio.h>
int main()
{
clrscr();
int a, b, temp;
printf("Enter value of a: ");
scanf("%d", &a);
printf("Enter value of b: ");
scanf("%d", &b);
temp = a;
a = b;
b = temp;
printf("After swapping:\n");
printf("a = %d\n", a);
printf("b = %d\n", b);
getch();
return 0;
}

Output:
Enter value of a: 5
Enter value of b: 10
After swapping:
a = 10
b=5

5
6. WAP that checks whether the two numbers entered by the user are equal or not.

Code:
#include<stdio.h>
#include<conio.h>
int main()
{
clrscr();
int num1, num2;
printf("Enter first number: ");
scanf("%d", &num1);
printf("Enter second number: ");
scanf("%d", &num2);
if (num1 == num2)
{
printf("Both numbers are equal.");
}
else
{
printf("The numbers are not equal.");
}
getch();
return 0;
}

Output:
Enter first number: 500
Enter second number: 400
The numbers are not equal.

6
7. WAP that checks whether the two numbers entered by the user are equal or not.

Code:
#include<stdio.h>
#include<conio.h>
int main()
{
clrscr();
int num1, num2, num3;
printf("Enter the three numbers: ");
scanf("%d %d %d", &num1, &num2, &num3);
if (num1 > num2 && num1 > num3)
{
printf("Greatest number is: %d", num1);
}
else if (num2 > num1 && num2 > num3)
{
printf("Greatest number is: %d", num2);
}
else if (num3 > num1 && num3 > num2)
{
printf("Greatest number is: %d", num3);
}
else
{
printf("Two or more numbers are equal and greatest");
}
getch();
return 0;
}

Output:
Enter first number = 100
Enter second number = 250
Enter third number = 400
Greatest number is: 400

7
8. WAP to find “the given number is even or odd.”

Code:

#include<stdio.h>
#include<conio.h>
int main()
{
int number;
clrscr();
printf("Enter a number: ");
scanf("%d", &number);
if (number % 2 == 0) // modulus operator (which gives remainder after division)
{
printf("Number is even\n");
}
else
{
printf("Number is odd\n");
}
getch();
return 0;
}

Output:
Enter a number = 10
Number is even.

8
9. Write a program to print the given year is Leap or not.

Code:
#include<stdio.h>
#include<conio.h>
void main()
{
int year;
clrscr();
printf("Enter a year: ");
scanf("%d", &year);
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
{
printf("Leap year");
}
else
{
printf("Not a leap year");
}
getch();
}

Output:
Enter a year: 2025
Not a leap year

9
10. Write WAP that accepts marks of five subjects and finds percentage and prints
grades according to the following criteria:
Between 90-100%-----Print ‘A’
80-90%-----------------Print ‘B’
60-80%-----------------Print ‘C’
Below 60%-------------Print ‘D’

Code:
#include <stdio.h>
#include<conio.h>
int main()
{
int m1, m2, m3, m4, m5, total;
float percent;
clrscr();
printf("Enter marks of 5 subjects:\n");
scanf("%d %d %d %d %d", &m1, &m2, &m3, &m4, &m5);
total = m1 + m2 + m3 + m4 + m5;
percent = total / 5.0;
printf("Percentage = %.2f\n", percent);
if (percent >= 90 && percent <= 100)
printf("Grade: A");
else if (percent >= 80)
printf("Grade: B");
else if (percent >= 60)
printf("Grade: C");
else
printf("Grade: D");
getch();
return 0;
}

Output:
Enter marks of 5 subjects:
75 80 70 90 85
Percentage = 80.00
Grade: B

10
11. WAP that takes two operands and one operator from the user, perform the
operation, and prints the result by using Switch statement.

Code:
#include <stdio.h>
#include <conio.h>
int main()
{
float num1, num2, result;
char op;
clrscr();
printf("Enter first number: ");
scanf("%f", &num1);
printf("Enter an operator (+, -, *, /): ");
scanf(" %c", &op);
printf("Enter second number: ");
scanf("%f", &num2);
switch(op)
{
case '+':
result = num1 + num2;
printf("Result = %.2f", result);
break;
case '-':
result = num1 - num2;
printf("Result = %.2f", result);
break;
case '*':
result = num1 * num2;
printf("Result = %.2f", result);
break;
case '/':
if(num2 != 0)
{
result = num1 / num2;
printf("Result = %.2f", result);
}
else
{
printf("Error: Division by zero!");
}
break;
default:
printf("Invalid operator!");
}
getch();

11
return 0;
}

Output:
Enter first number: 12
Enter an operator (+, -, *, /): *
Enter second number: 5
Result = 60.00

12
12. WAP to print the sum of all numbers up to a given number.
Code:

#include <stdio.h>
#include <conio.h>
int main()
{
int n, i, sum = 0;
clrscr();
printf("Enter a number: ");
scanf("%d", &n);
for(i = 1; i <= n; i++)
{
sum = sum + i;
}
printf("Sum of all numbers from 1 to %d is: %d", n, sum);
getch();
return 0;
}

Output:
Enter a number: 5
Sum of all numbers from 1 to 5 is: 15

13
13. WAP to find the factorial of a given number.

Code:
#include <stdio.h>
#include <conio.h>
int main()
{
int n, i;
unsigned long long factorial = 1;
clrscr();
printf("Enter a positive integer: ");
scanf("%d", &n);
if(n < 0)
{
printf("Factorial of negative number doesn't exist.");
}
else
{
for(i = 1; i <= n; i++)
{
factorial = factorial * i;
}
printf("Factorial of %d = %llu", n, factorial);
}
getch();
return 0;
}

Output:
Enter a positive integer: 5
Factorial of 5 = 120

14
14. WAP to print sum of even and odd numbers from1 to N numbers.

Code:
#include <stdio.h>
#include <conio.h>
int main()
{
int i, n, even Sum = 0, odd Sum = 0;
clrscr();
printf("Enter a number: ");
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 from 1 to %d = %d\n", n, even Sum);
printf("Sum of odd numbers from 1 to %d = %d", n, odd Sum);
getch();
return 0;
}

Output:
Enter a number: 10
Sum of even numbers from 1 to 10 = 30
Sum of odd numbers from 1 to 10 = 25

15
15. WAP to print the Fibonacci series.

Code:
#include <stdio.h>
#include <conio.h>
int main()
{
int n, i;
int a = 0, b = 1, next;
clrscr();
printf("Enter the 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;
}
getch();
return 0;
}

Output:
Enter the number of terms: 7
Fibonacci Series: 0 1 1 2 3 5 8

16
16. WAP to check whether the entered number is prime or not.

Code:
#include <stdio.h>
#include <conio.h>
int main()
{
int num, i, is Prime = 1;
clrscr();
printf("Enter a number: ");
scanf("%d", &num);
if (num <= 1)
{
Is Prime = 0;
}
else
{
for(i = 2; i <= num / 2; i++)
{
if(num % i == 0)
{
Is Prime = 0;
break;
}
}
}
if(is Prime == 1)
{
printf("%d is a prime number.", num);
}
else
{
printf("%d is not a prime number.", num);
}
getch();
return 0;
}
Output:
Enter a number: 7
7 is a prime number.

17
17. WAP to find the sum of digits of the entered number.

Code:
#include <stdio.h>
#include <conio.h>
int main()
{
int num, digit, sum = 0;
clrscr();
printf("Enter a number: ");
scanf("%d", &num);
while(num != 0)
{
digit = num % 10;
sum = sum + digit;
num = num / 10;
}
printf("Sum of digits = %d", sum);
getch();
return 0;
}

Output:
Enter a number: 1234
Sum of digits = 10

18
18. WAP to find the reverse of a number.

Code:
#include <stdio.h>
#include <conio.h>
int main()
{
int num, digit, reverse = 0;
clrscr();
printf("Enter a number: ");
scanf("%d", &num);
while(num != 0)
{
digit = num % 10;
reverse = reverse * 10 + digit;
num = num / 10;
}
printf("Reversed number = %d", reverse);
getch();
return 0;
}

Output:
Enter a number: 1234
Reversed number = 4321

19
19. WAP to print Armstrong numbers from 1 to 100.

Code:
#include <stdio.h>
#include <conio.h>
int main()
{
int num, temp, digit, sum;
clrscr();
printf("Armstrong numbers from 1 to 100 are:\n");
for(num = 1; num <= 100; num++)
{
temp = num;
sum = 0;
while(temp != 0)
{
digit = temp % 10;
sum = sum + (digit * digit * digit);
temp = temp / 10;
}
if(sum == num)
{
printf("%d ", num);
}
}
getch();
return 0;
}

Output:
Armstrong numbers from 1 to 100 are:
123456789

20
20. WAP to convert binary number into decimal number and vice versa.

Code:
#include <stdio.h>
#include <conio.h>
#include <math.h>
int main()
{
long binary, temp;
int decimal = 0, base = 1, rem;
clrscr();
printf("Enter a binary number: ");
scanf("%ld", &binary);
temp = binary;
while (temp != 0)
{
rem = temp % 10;
decimal = decimal + rem * base;
base = base * 2;
temp = temp / 10;
}
printf("Decimal equivalent = %d", decimal);
getch();
return 0;
}

Output:
Enter a binary number: 1101
Decimal equivalent = 13

21
21. WAP that simply takes elements of the array from the user and finds the sum of
these elements.

Code:
#include <stdio.h>
#include <conio.h>
int main()
{
int arr[100], n, i, sum = 0;
clrscr();
printf("Enter number of elements in array: ");
scanf("%d", &n);
printf("Enter %d elements:\n", n);
for(i = 0; i < n; i++)
{
scanf("%d", &arr[i]);
sum = sum + arr[i];
}
printf("Sum of array elements = %d", sum);
getch();
return 0;
}

Output:
Enter number of elements in array: 5
Enter 5 elements:
10
20
30
40
50
Sum of array elements = 150

22
22. WAP that inputs two arrays and saves sum of corresponding elements of these
arrays in a third array and prints them.

Code:
#include <stdio.h>
#include <conio.h>
void main()
{
int arr1[100], arr2[100], sum[100], n, i;
clrscr();
printf("Enter the number of elements: ");
scanf("%d", &n);
printf("Enter elements of first array:\n");
for(i = 0; i < n; i++)
{
scanf("%d", &arr1[i]);
}
printf("Enter elements of second array:\n");
for(i = 0; i < n; i++)
{
scanf("%d", &arr2[i]);
}
for(i = 0; i < n; i++)
{
sum[i] = arr1[i] + arr2[i];
}
printf("Sum of corresponding elements:\n");
for(i = 0; i < n; i++)
{
printf("%d + %d = %d\n", arr1[i], arr2[i], sum[i]);
}
getch();
}

23
Output:
Enter the number of elements: 3
Enter elements of first array:
10
20
30
Enter elements of second array:
1
2
3
Sum of corresponding elements:
10 + 1 = 11
20 + 2 = 22
30 + 3 = 33

24
23. WAP to find the minimum and maximum element of the array.

Code:
#include <stdio.h>
#include <conio.h>
void main()
{
int arr[100], n, i;
int min, max;
clrscr();
printf("Enter the number of elements in the array: ");
scanf("%d", &n);
printf("Enter %d elements:\n", n);
for(i = 0; i < n; i++)
{
scanf("%d", &arr[i]);
}
min = 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 element = %d\n", min);
printf("Maximum element = %d", max);
getch();
}
Output:
Enter the number of elements in the array: 5
Enter 5 elements:
12
45
3
67
20
Minimum element = 3
Maximum element = 67

25
24. WAP to search an element in a array using Linear Search.

Code:
#include <stdio.h>
#include <conio.h>
void main()
{
int a[100], n, i, key, found = 0;
clrscr();
printf("Enter the number of elements in array: ");
scanf("%d", &n);
printf("Enter %d elements:\n", n);
for(i = 0; i < n; i++)
{
scanf("%d", &a[i]);
}
printf("Enter the element to search: ");
scanf("%d", &key);
for(i = 0; i < n; i++)
{
if(a[i] == key)
{
printf("Element %d found at position %d\n", key, i + 1);
found = 1;
break;
}
}
if(found == 0)
{
printf("Element %d not found in the array.\n", key);
}
getch();

Output:
Enter the number of elements in array: 5
Enter 5 elements:
10
20
30
40
50
Enter the element to search: 60
Element 60 not found in the array.

26
25. WAP to sort the elements of the array in ascending order using Bubble Sort
technique.

Code:
#include <stdio.h>
#include <conio.h>
void main() {
int a[100], n, i, j, temp;
clrscr();
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]);
}
// Bubble Sort Logic
for(i = 0; i < n - 1; i++) {
for(j = 0; j < n - 1 - i; j++) {
if(a[j] > a[j + 1]) {
// Swap a[j] and a[j+1]
temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
}
}

printf("Array after sorting in ascending order:\n");


for(i = 0; i < n; i++) {
printf("%d ", a[i]);
}
getch();
}

27
Output:
Enter the number of elements: 5
Enter 5 elements:
55
34
7
87
56
Array after sorting in ascending order:
7 34 55 56 87

28
26. WAP to add and multiply two matrices of order n x n.

Code:
#include <stdio.h>
#include <conio.h>
void main()
{
int a[10][10], b[10][10], sum[10][10], mul[10][10];
int n, i, j, k;
clrscr();
printf("Enter the order of the square matrix (n x n): ");
scanf("%d", &n);

// Input matrix A
printf("Enter elements of Matrix A:\n");
for(i = 0; i < n; i++)
{
for(j = 0; j < n; j++)
{
scanf("%d", &a[i][j]);
}
}

// Input matrix B
printf("Enter elements of Matrix B:\n");
for(i = 0; i < n; i++)
{
for(j = 0; j < n; j++)
{
scanf("%d", &b[i][j]);
}
}

// Matrix Addition
for(i = 0; i < n; i++)
{
for(j = 0; j < n; j++)
{
sum[i][j] = a[i][j] + b[i][j];
}
}

// Matrix Multiplication
for(i = 0; i < n; i++)
{
for(j = 0; j < n; j++)
{
mul[i][j] = 0;
for(k = 0; k < n; k++)
{

29
mul[i][j] += a[i][k] * b[k][j];
}
}
}

// Display Addition Result


printf("\nMatrix Addition (A + B):\n");
for(i = 0; i < n; i++)
{
for(j = 0; j < n; j++)
{
printf("%d ", sum[i][j]);
}
printf("\n");
}

// Display Multiplication Result


printf("\nMatrix Multiplication (A * B):\n");
for(i = 0; i < n; i++)
{
for(j = 0; j < n; j++)
{
printf("%d ", mul[i][j]);
}
printf("\n");
}
getch();
}

Output:
Enter the order of the square matrix (n x n): 2

Enter elements of Matrix A:


12
34

Enter elements of Matrix B:


56
78

Matrix Addition (A + B):


68
10 12

Matrix Multiplication (A * B):


19 22
43 50

30
27. WAP that finds the sum of diagonal elements of a m x n matrix.

Code:
#include <stdio.h>
#include <conio.h>
void main()
{
int a[10][10], m, n, i, j;
int sum = 0;
clrscr();
printf("Enter the number of rows (m): ");
scanf("%d", &m);
printf("Enter the number of columns (n): ");
scanf("%d", &n);
printf("Enter elements of the matrix:\n");
for(i = 0; i < m; i++)
{
for(j = 0; j < n; j++)
{
scanf("%d", &a[i][j]);
}
}

// Calculate sum of diagonal elements (only if i == j)


for(i = 0; i < m; i++)
{
for(j = 0; j < n; j++)
{
if(i == j)
{
sum += a[i][j];
}
}
}
printf("\nSum of diagonal elements: %d", sum);
getch();
}

31
Output:
Enter the number of rows (m): 3
Enter the number of columns (n): 3
Enter elements of the matrix:
123
456
789

Sum of diagonal elements: 15

32
28. WAP to implement strlen (), strcat (),strcpy () using the concept of Functions.

Code:
#include <stdio.h>
#include<conio.h>
int my_strlen(char str[])
{
int i = 0;
while (str[i] != '\0')
{
i++;
}
return i;
}

// User-defined strcpy()
void my_strcpy(char dest[], char src[])
{
int i = 0;
while (src[i] != '\0')
{
dest[i] = src[i];
i++;
}
dest[i] = '\0'; // Add null character
}

// User-defined strcat()
void my_strcat(char str1[], char str2[])
{
int i = 0, j = 0;

// Move i to the end of str1


while (str1[i] != '\0')
{
i++;
}

// Append str2 to str1


while (str2[j] != '\0')

33
{
str1[i] = str2[j];
i++;
j++;
}
str1[i] = '\0'; // Add null character
}
int main()
{
char str1[100], str2[100], copied[100];
int length;
printf("Enter first string: ");
fgets(str1, sizeof(str1), stdin);
printf("Enter second string: ");
fgets(str2, sizeof(str2), stdin);

// Remove newline character from fgets


int len1 = my_strlen(str1);
if (str1[len1 - 1] == '\n')
{
str1[len1 - 1] = '\0';
}
int len2 = my_strlen(str2);
if (str2[len2 - 1] == '\n')
{
str2[len2 - 1] = '\0';
}

// strlen
length = my_strlen(str1);
printf("\nLength of first string = %d", length);

// strcpy
my_strcpy(copied, str1);
printf("\nCopied string = %s", copied);

// strcat
my_strcat(str1, str2);
printf("\nConcatenated string = %s\n", str1);
getch();

34
return 0;
}

Output:
Enter first string: Hello
Enter second string: Everyone

Length of first string = 5


Copied string = Hello
Concatenated string = HelloEveryone

35
29. WAP to swap two elements using the concept of pointers.

Code:
#include <stdio.h>
#include<conio.h>
void swap(int *a, int *b)
{
int temp;
temp = *a;
*a = *b;
*b = temp;
}
int main()
{
int x, y;
clrscr();
printf("Enter first number: ");
scanf("%d", &x);
printf("Enter second number: ");
scanf("%d", &y);
printf("\nBefore swapping:\n");
printf("x = %d, y = %d\n", x, y);
swap(&x, &y);
printf("\nAfter swapping:\n");
printf("x = %d, y = %d\n", x, y);
getch();
return 0;
}

Output:
Enter first number: 10
Enter second number: 20

Before swapping:
x = 10, y = 20

After swapping:
x = 20, y = 10

36
30. WAP to check whether a given word exists in a file or not. If yes then find the
number of times it occurs.

Code:
#include <stdio.h>
#include <string.h>
#include <conio.h>

int main()
{
FILE *fp;
char filename[50];
char word[50], temp[50];
int count = 0;
clrscr();
printf("Enter the filename: ");
scanf("%s", filename);
fp = fopen(filename, "r");
if (fp == NULL)
{
printf("Error: Could not open file.\n");
return 1;
}
printf("Enter the word to search: ");
scanf("%s", word);
while (fscanf(fp, "%s", temp) != EOF) {
if (strcmp(temp, word) == 0) {
count++;
}
}
fclose(fp);
if (count > 0)
{
printf("The word '%s' exists and occurs %d time(s) in the file.\n", word, count);
} else
{
printf("The word '%s' does not exist in the file.\n", word);
}
getch();
return 0;
}

Output:
Enter the word to search: hello
The word 'hello' occurs 3 time(s).

37

You might also like