New Pps File
New Pps File
Practical file
Of
Programming and
problem solving using C
1
Index
2
Program 1: **WAP to add, subtract, multiply and divide
two numbers**
#include <stdio.h>
int main() {
char operation;
scanf("%f", &num1);
scanf("%f", &num2);
switch(operation) {
case '+':
break;
case '-':
break;
case '*':
3
printf("The result is: %.2f\n", num1 * num2);
break;
case '/':
if (num2 != 0) {
} else {
break;
default:
printf("Invalid operation.\n");
return 0;
Output:
4
Program 2 : WAP to find area and perimeter of a circle and
a triangle
#include <stdio.h>
#include <math.h>
printf("Circle: \n");
perimeter = a + b + c;
s = perimeter / 2;
printf("Triangle: \n");
5
}
int main() {
float radius, a, b, c;
scanf("%f", &radius);
calculateCircle(radius);
calculateTriangle(a, b, c);
return 0;
6
Program 3: WAP to find the largest of three number ( if-
then- else).
#include <stdio.h>
int main() {
} else {
return 0;
7
Program 4: WAP to find roots of quadratic equation using
switch statement.
#include <stdio.h>
#include <math.h>
int main() {
int choice;
// Input coefficients
discriminant = b * b - 4 * a * c;
if (discriminant > 0) {
choice = 1;
} else if (discriminant == 0) {
choice = 2;
} else {
choice = 3;
switch (choice) {
8
case 1:
break;
case 2:
break;
case 3:
realPart = -b / (2 * a);
break;
default:
printf("Invalid choice.\n");
return 0;
9
10
Program 5: WAP to find the largest number out of ten
numbers (for- statement).
#include <stdio.h>
int main() {
int numbers[10];
int i, max;
// Input 10 numbers
printf("Enter 10 numbers: \n");
for (i = 0; i < 10; i++) {
printf("Number %d: ", i + 1);
scanf("%d", &numbers[i]);
}
11
// Print the largest number
printf("The largest number is: %d\n", max);
return 0;
}
12
Program 6: WAP using arrays to find the largest and
second largest no. out of 50 nos.
#include <stdio.h>
int main() {
int numbers[50];
// Input 50 numbers
printf("Enter 50 numbers:\n");
scanf("%d", &numbers[i]);
largest = numbers[0];
second_largest = numbers[1];
} else {
largest = numbers[1];
second_largest = numbers[0];
13
if (numbers[i] > largest) {
second_largest = largest;
largest = numbers[i];
second_largest = numbers[i];
return 0;
14
15
Program 7: WAP to multiply two matrices
#include <stdio.h>
result[i][j] = 0;
if (j == column - 1)
printf("\n");
int main() {
16
// Input for first matrix
scanf("%d", &firstMatrix[i][j]);
if (c1 != r2) {
return 0;
scanf("%d", &secondMatrix[i][j]);
17
}
// Multiply matrices
printf("Resultant matrix:\n");
return 0;
18
Program 8: WAP to find factorial of a given number using
recursion.
#include <stdio.h>
if (n == 0) {
} else {
int main() {
int number;
// Input number
scanf("%d", &number);
if (number < 0) {
} else {
// Calculate factorial
19
result = factorial(number);
return 0;
20
Program 9: WAP to find the Fibonacci series using
recursion.
#include <stdio.h>
int fibonacci(int n) {
if (n <= 1) {
} else {
int main() {
int n, i;
scanf("%d", &n);
printf("\n");
return 0;
21
}
22
Program 10: WAP to convert given decimal number into
binary number.
#include <stdio.h>
binary[i] = num % 2;
num = num / 2;
i++;
printf("%d", binary[j]);
printf("\n");
int main() {
int num;
23
// Input decimal number
scanf("%d", &num);
if (num < 0) {
} else {
decimalToBinary(num);
return 0;
24
Program 11: WAP to find sum of digits of a given number.
#include <stdio.h>
int sum = 0;
while (num != 0) {
return sum;
int main() {
// Input number
scanf("%d", &num);
result = sumOfDigits(num);
25
// Output the result
return 0;
26
#include <stdio.h>
int main() {
// Input number
scanf("%d", &num);
while (num != 0) {
num = num / 10; // Remove the last digit from the original number
return 0;
27
Program 13: WAP to generate Pascal’s triangle.
#include <stdio.h>
void generatePascalsTriangle(int n) {
28
int triangle[50][50];
if (i == 0 || i == line) {
triangle[line][i] = 1;
} else {
// Other values are the sum of the values just above and to the left
printf("\n");
int main() {
int n;
scanf("%d", &n);
generatePascalsTriangle(n);
29
return 0;
#include <stdio.h>
int main() {
30
// Input number of rows for the pyramid
scanf("%d", &rows);
// Print spaces
printf(" ");
// Print numbers
num = 1;
printf("\n");
return 0;
31
32
Program 15: WAP to check that input string is palindrome
or not.
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int l = 0;
int h = strlen(str) - 1;
while (h > l) {
l++;
h--;
if (tolower(str[l++]) != tolower(str[h--])) {
return 1; // Palindrome
33
}
int main() {
char str[100];
// Input string
if (isPalindrome(str)) {
} else {
return 0;
34
Program 16: WAP to concatenate two strings.
#include <stdio.h>
#include <string.h>
int main() {
gets(str2);
strcat(str1, str2);
return 0;
35
Program 17: WAP to demonstrate the use of unions.
#include <stdio.h>
#include <string.h>
// Define a union
union Data {
int i;
float f;
char str[20];
};
int main() {
data.i = 10;
data.f = 220.5;
strcpy(data.str, "Hello");
36
// Observe the effect of the union's shared memory
return 0;
37
Program 18: WAP to illustrate the use of enumerators.
#include <stdio.h>
enum Day {
SUNDAY,
MONDAY,
TUESDAY,
WEDNESDAY,
THURSDAY,
FRIDAY,
SATURDAY
};
int main() {
today = WEDNESDAY;
switch (today) {
case SUNDAY:
printf("Today is Sunday.\n");
break;
case MONDAY:
printf("Today is Monday.\n");
break;
38
case TUESDAY:
printf("Today is Tuesday.\n");
break;
case WEDNESDAY:
printf("Today is Wednesday.\n");
break;
case THURSDAY:
printf("Today is Thursday.\n");
break;
case FRIDAY:
printf("Today is Friday.\n");
break;
case SATURDAY:
printf("Today is Saturday.\n");
break;
default:
printf("Invalid day.\n");
return 0;
Output:
39
Program 19: WAP to create and store multiple lines in a
text file.
#include <stdio.h>
int main() {
FILE *file;
char line[100];
int n, i;
if (file == NULL) {
return 1;
scanf("%d", &n);
fputs(line, file);
40
}
fclose(file);
return 0;
Output:
41
Program 20: WAP to count the number of lines, words and characters in a file.
#include <stdio.h>
#include <ctype.h>
void countFile(const char *filename, int *lines, int *words, int *characters) {
FILE *file;
char ch;
int inWord = 0;
if (file == NULL) {
return;
// Initialize counters
(*characters)++;
if (ch == '\n') {
(*lines)++;
42
// Check for word boundaries
if (isspace(ch)) {
inWord = 0;
} else if (inWord == 0) {
inWord = 1;
(*words)++;
fclose(file);
int main() {
char filename[100];
scanf("%s", filename);
43
printf("Words: %d\n", words);
return 0;
Output:
44