0% found this document useful (0 votes)
24 views44 pages

New Pps File

Uploaded by

alokp064
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)
24 views44 pages

New Pps File

Uploaded by

alokp064
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
You are on page 1/ 44

International Institute of

Technology and Management


Murthal, Sonipat, Haryana

Practical file
Of
Programming and
problem solving using C

Submitted to: Submitted by:


Dr. Manish Jha Sir Vanshika
HOD 24015021037
CSE, AIML, DS B. Tech
1st Sem. (AIML)

1
Index

Sr. Programs Remarks


No
1. WAP to add, subtract, multiply and divide two numbers.
2. WAP to find area and perimeter of a circle and a triangle.
3. WAP to find the largest of three number ( if- then- else).
4. WAP to find roots of quadratic equation using switch
statement.
5. WAP to find the largest number out of ten numbers (for-
statement).
6. WAP using arrays to find the largest and second largest no. out
of 50 nos.
7. WAP to multiply two matrices.
8. WAP to find factorial of a given number using recursion.
9. WAP to find the Fibonacci series using recursion.
10. WAP to convert a given decimal number into binary number.
11. WAP to find sum of digits of a given number.
12. WAP to reverse a given number.
13. WAP to generate Pascal’s triangle.
14. WAP to generate pyramid of numbers.
15. WAP to check that the input string is a palindrome or not.
16. WAP to concatenate two strings.
17. WAP to demonstrate the use of unions.
18. WAP to illustrate the use of enumerators.
19. WAP to create and store multiples lines in a text file.
20. WAP to count the number of lines, words and character in a
file.

2
Program 1: **WAP to add, subtract, multiply and divide
two numbers**

#include <stdio.h>

int main() {

float num1, num2;

char operation;

// Take input from the user

printf("Enter first number: ");

scanf("%f", &num1);

printf("Enter second number: ");

scanf("%f", &num2);

// Choose the operation

printf("Select operation (+, -, *, /): ");

scanf(" %c", &operation);

switch(operation) {

case '+':

printf("The result is: %.2f\n", num1 + num2);

break;

case '-':

printf("The result is: %.2f\n", num1 - num2);

break;

case '*':

3
printf("The result is: %.2f\n", num1 * num2);

break;

case '/':

if (num2 != 0) {

printf("The result is: %.2f\n", num1 / num2);

} else {

printf("Error! Division by zero.\n");

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>

// Function to calculate the area and perimeter of a circle

void calculateCircle(float radius) {

float area, perimeter;

area = M_PI * radius * radius;

perimeter = 2 * M_PI * radius;

printf("Circle: \n");

printf("Area: %.2f\n", area);

printf("Perimeter: %.2f\n", perimeter);

// Function to calculate the area and perimeter of a triangle

void calculateTriangle(float a, float b, float c) {

float perimeter, area, s;

perimeter = a + b + c;

s = perimeter / 2;

area = sqrt(s * (s - a) * (s - b) * (s - c));

printf("Triangle: \n");

printf("Area: %.2f\n", area);

printf("Perimeter: %.2f\n", perimeter);

5
}

int main() {

float radius, a, b, c;

// Input for circle

printf("Enter radius of the circle: ");

scanf("%f", &radius);

// Calculate and display circle area and perimeter

calculateCircle(radius);

// Input for triangle

printf("Enter sides of the triangle (a, b, c): ");

scanf("%f %f %f", &a, &b, &c);

// Calculate and display triangle area and perimeter

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() {

float num1, num2, num3;

// Input three numbers

printf("Enter three numbers: ");

scanf("%f %f %f", &num1, &num2, &num3);

// Find the largest number using if-then-else statements

if (num1 >= num2 && num1 >= num3) {

printf("The largest number is: %.2f\n", num1);

} else if (num2 >= num1 && num2 >= num3) {

printf("The largest number is: %.2f\n", num2);

} else {

printf("The largest number is: %.2f\n", num3);

return 0;

7
Program 4: WAP to find roots of quadratic equation using
switch statement.

#include <stdio.h>

#include <math.h>

int main() {

float a, b, c, discriminant, root1, root2, realPart, imaginaryPart;

int choice;

// Input coefficients

printf("Enter coefficients a, b, and c: ");

scanf("%f %f %f", &a, &b, &c);

// Calculate the discriminant

discriminant = b * b - 4 * a * c;

// Determine the type of roots based on the discriminant

if (discriminant > 0) {

choice = 1;

} else if (discriminant == 0) {

choice = 2;

} else {

choice = 3;

// Switch statement to handle different cases

switch (choice) {

8
case 1:

// Two distinct real roots

root1 = (-b + sqrt(discriminant)) / (2 * a);

root2 = (-b - sqrt(discriminant)) / (2 * a);

printf("Roots are real and distinct.\n");

printf("Root 1 = %.2f\n", root1);

printf("Root 2 = %.2f\n", root2);

break;

case 2:

// Two equal real roots

root1 = root2 = -b / (2 * a);

printf("Roots are real and equal.\n");

printf("Root 1 = Root 2 = %.2f\n", root1);

break;

case 3:

// Two complex roots

realPart = -b / (2 * a);

imaginaryPart = sqrt(-discriminant) / (2 * a);

printf("Roots are complex.\n");

printf("Root 1 = %.2f + %.2fi\n", realPart, imaginaryPart);

printf("Root 2 = %.2f - %.2fi\n", realPart, imaginaryPart);

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]);
}

// Initialize max to the first number


max = numbers[0];

// Iterate through the array to find the largest number


for (i = 1; i < 10; i++) {
if (numbers[i] > max) {
max = 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];

int i, largest, second_largest;

// Input 50 numbers

printf("Enter 50 numbers:\n");

for (i = 0; i < 50; i++) {

printf("Number %d: ", i + 1);

scanf("%d", &numbers[i]);

// Initialize largest and second largest

if (numbers[0] > numbers[1]) {

largest = numbers[0];

second_largest = numbers[1];

} else {

largest = numbers[1];

second_largest = numbers[0];

// Find the largest and second largest numbers

for (i = 2; i < 50; i++) {

13
if (numbers[i] > largest) {

second_largest = largest;

largest = numbers[i];

} else if (numbers[i] > second_largest && numbers[i] != largest) {

second_largest = numbers[i];

// Print the largest and second largest numbers

printf("The largest number is: %d\n", largest);

printf("The second largest number is: %d\n", second_largest);

return 0;

14
15
Program 7: WAP to multiply two matrices

#include <stdio.h>

void multiplyMatrices(int firstMatrix[][10], int secondMatrix[][10], int result[][10], int r1,


int c1, int r2, int c2) {

for (int i = 0; i < r1; ++i) {

for (int j = 0; j < c2; ++j) {

result[i][j] = 0;

for (int k = 0; k < c1; ++k) {

result[i][j] += firstMatrix[i][k] * secondMatrix[k][j];

void displayMatrix(int matrix[][10], int row, int column) {

for (int i = 0; i < row; ++i) {

for (int j = 0; j < column; ++j) {

printf("%d ", matrix[i][j]);

if (j == column - 1)

printf("\n");

int main() {

int firstMatrix[10][10], secondMatrix[10][10], result[10][10], r1, c1, r2, c2;

16
// Input for first matrix

printf("Enter rows and columns for the first matrix: ");

scanf("%d %d", &r1, &c1);

printf("Enter elements of the first matrix:\n");

for (int i = 0; i < r1; ++i) {

for (int j = 0; j < c1; ++j) {

printf("Enter element a%d%d: ", i + 1, j + 1);

scanf("%d", &firstMatrix[i][j]);

// Input for second matrix

printf("Enter rows and columns for the second matrix: ");

scanf("%d %d", &r2, &c2);

// Check if multiplication is possible

if (c1 != r2) {

printf("Matrix multiplication is not possible.\n");

return 0;

printf("Enter elements of the second matrix:\n");

for (int i = 0; i < r2; ++i) {

for (int j = 0; j < c2; ++j) {

printf("Enter element b%d%d: ", i + 1, j + 1);

scanf("%d", &secondMatrix[i][j]);

17
}

// Multiply matrices

multiplyMatrices(firstMatrix, secondMatrix, result, r1, c1, r2, c2);

// Display the result

printf("Resultant matrix:\n");

displayMatrix(result, r1, c2);

return 0;

18
Program 8: WAP to find factorial of a given number using
recursion.

#include <stdio.h>

// Function to calculate the factorial of a number using recursion

unsigned long long factorial(int n) {

if (n == 0) {

return 1; // Base case: factorial of 0 is 1

} else {

return n * factorial(n - 1); // Recursive case

int main() {

int number;

unsigned long long result;

// Input number

printf("Enter a number to find its factorial: ");

scanf("%d", &number);

// Check for negative input

if (number < 0) {

printf("Factorial of a negative number doesn't exist.\n");

} else {

// Calculate factorial

19
result = factorial(number);

// Output the result

printf("Factorial of %d = %llu\n", number, result);

return 0;

20
Program 9: WAP to find the Fibonacci series using
recursion.
#include <stdio.h>

// Function to calculate the nth Fibonacci number using recursion

int fibonacci(int n) {

if (n <= 1) {

return n; // Base cases: fibonacci(0) = 0, fibonacci(1) = 1

} else {

return fibonacci(n - 1) + fibonacci(n - 2); // Recursive case

int main() {

int n, i;

// Input number of terms

printf("Enter the number of terms: ");

scanf("%d", &n);

// Print the Fibonacci series

printf("Fibonacci series: ");

for (i = 0; i < n; i++) {

printf("%d ", fibonacci(i));

printf("\n");

return 0;

21
}

22
Program 10: WAP to convert given decimal number into
binary number.

#include <stdio.h>

// Function to convert decimal to binary

void decimalToBinary(int num) {

int binary[32]; // Array to store binary digits

int i = 0; // Counter for binary array

// Process to convert decimal to binary

while (num > 0) {

binary[i] = num % 2;

num = num / 2;

i++;

// Print the binary number in reverse order

printf("Binary representation: ");

for (int j = i - 1; j >= 0; j--) {

printf("%d", binary[j]);

printf("\n");

int main() {

int num;

23
// Input decimal number

printf("Enter a decimal number: ");

scanf("%d", &num);

// Check for non-negative input

if (num < 0) {

printf("Please enter a non-negative integer.\n");

} else {

// Convert and display binary number

decimalToBinary(num);

return 0;

24
Program 11: WAP to find sum of digits of a given number.

#include <stdio.h>

// Function to find the sum of digits of a given number

int sumOfDigits(int num) {

int sum = 0;

// Loop to extract each digit and add to sum

while (num != 0) {

sum += num % 10; // Add the last digit to sum

num = num / 10; // Remove the last digit from num

return sum;

int main() {

int num, result;

// Input number

printf("Enter a number: ");

scanf("%d", &num);

// Calculate sum of digits

result = sumOfDigits(num);

25
// Output the result

printf("Sum of digits of %d = %d\n", num, result);

return 0;

Program 12 : WAP to reverse a given number.

26
#include <stdio.h>

int main() {

int num, reversedNum = 0, remainder;

// Input number

printf("Enter a number: ");

scanf("%d", &num);

// Process to reverse the number

while (num != 0) {

remainder = num % 10; // Get the last digit

reversedNum = reversedNum * 10 + remainder; // Append it to the reversed number

num = num / 10; // Remove the last digit from the original number

// Output the reversed number

printf("Reversed number: %d\n", reversedNum);

return 0;

27
Program 13: WAP to generate Pascal’s triangle.

#include <stdio.h>

// Function to generate Pascal's triangle

void generatePascalsTriangle(int n) {

28
int triangle[50][50];

// Initialize the top of the triangle

for (int line = 0; line < n; line++) {

for (int i = 0; i <= line; i++) {

// The first and last values in every row are 1

if (i == 0 || i == line) {

triangle[line][i] = 1;

} else {

// Other values are the sum of the values just above and to the left

triangle[line][i] = triangle[line-1][i-1] + triangle[line-1][i];

printf("%d ", triangle[line][i]);

printf("\n");

int main() {

int n;

// Input number of rows

printf("Enter the number of rows for Pascal's triangle: ");

scanf("%d", &n);

// Generate Pascal's triangle

generatePascalsTriangle(n);

29
return 0;

Program 14: WAP to generate pyramid of numbers.

#include <stdio.h>

int main() {

int rows, i, j, space, num;

30
// Input number of rows for the pyramid

printf("Enter the number of rows: ");

scanf("%d", &rows);

// Generate pyramid of numbers

for (i = 1; i <= rows; i++) {

// Print spaces

for (space = 1; space <= rows - i; space++) {

printf(" ");

// Print numbers

num = 1;

for (j = 1; j <= i; j++) {

printf("%d ", num++);

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>

// Function to check if a string is a palindrome

int isPalindrome(char str[]) {

int l = 0;

int h = strlen(str) - 1;

while (h > l) {

// Ignore spaces and punctuation

while (!isalnum(str[l]) && l < h) {

l++;

while (!isalnum(str[h]) && l < h) {

h--;

// Compare characters case-insensitively

if (tolower(str[l++]) != tolower(str[h--])) {

return 0; // Not a palindrome

return 1; // Palindrome

33
}

int main() {

char str[100];

// Input string

printf("Enter a string: ");

fgets(str, sizeof(str), stdin);

// Remove newline character from the string

str[strcspn(str, "\n")] = '\0';

// Check if the string is a palindrome

if (isPalindrome(str)) {

printf("\"%s\" is a palindrome.\n", str);

} else {

printf("\"%s\" is not a palindrome.\n", str);

return 0;

34
Program 16: WAP to concatenate two strings.
#include <stdio.h>

#include <string.h>

int main() {

char str1[100], str2[100];

// Input two strings

printf("Enter the first string: ");

gets(str1); // Using gets() for simplicity; normally use fgets()

printf("Enter the second string: ");

gets(str2);

// Concatenate the strings

strcat(str1, str2);

// Output the result

printf("Concatenated string: %s\n", str1);

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() {

union Data data; // Declare a union variable

// Store an integer value

data.i = 10;

printf("data.i: %d\n", data.i);

// Store a float value

data.f = 220.5;

printf("data.f: %.2f\n", data.f);

// Store a string value

strcpy(data.str, "Hello");

printf("data.str: %s\n", data.str);

36
// Observe the effect of the union's shared memory

printf("\nAfter storing different values:\n");

printf("data.i: %d\n", data.i);

printf("data.f: %.2f\n", data.f);

printf("data.str: %s\n", data.str);

return 0;

37
Program 18: WAP to illustrate the use of enumerators.
#include <stdio.h>

// Define an enumerator for days of the week

enum Day {

SUNDAY,

MONDAY,

TUESDAY,

WEDNESDAY,

THURSDAY,

FRIDAY,

SATURDAY

};

int main() {

enum Day today;

// Assign a value to the enumerator variable

today = WEDNESDAY;

// Check the value of the enumerator

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;

// Open the file in write mode

file = fopen("output.txt", "w");

if (file == NULL) {

printf("Error opening file!\n");

return 1;

// Input the number of lines to be written

printf("Enter the number of lines: ");

scanf("%d", &n);

getchar(); // Clear the newline character from the buffer

// Input lines and write them to the file

printf("Enter %d lines:\n", n);

for (i = 0; i < n; i++) {

printf("Line %d: ", i + 1);

fgets(line, sizeof(line), stdin);

fputs(line, file);

40
}

// Close the file

fclose(file);

printf("Lines successfully written to output.txt\n");

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>

// Function to count lines, words, and characters in a file

void countFile(const char *filename, int *lines, int *words, int *characters) {

FILE *file;

char ch;

int inWord = 0;

// Open the file in read mode

file = fopen(filename, "r");

if (file == NULL) {

printf("Error opening file!\n");

return;

// Initialize counters

*lines = *words = *characters = 0;

// Read the file character by character

while ((ch = fgetc(file)) != EOF) {

(*characters)++;

// Check for new lines

if (ch == '\n') {

(*lines)++;

42
// Check for word boundaries

if (isspace(ch)) {

inWord = 0;

} else if (inWord == 0) {

inWord = 1;

(*words)++;

// Close the file

fclose(file);

int main() {

char filename[100];

int lines, words, characters;

// Input the filename

printf("Enter the filename: ");

scanf("%s", filename);

// Count lines, words, and characters

countFile(filename, &lines, &words, &characters);

// Output the results

printf("File: %s\n", filename);

printf("Lines: %d\n", lines);

43
printf("Words: %d\n", words);

printf("Characters: %d\n", characters);

return 0;

Output:

44

You might also like