Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
4 views48 pages

C Lab Program

The document contains a series of C programming exercises, each with a specific task such as arithmetic operations, calculating averages, converting characters, and determining geometric properties. Each exercise includes a program code snippet, expected output, and instructions for user input. The tasks range from basic operations to more complex calculations like interest and area, aimed at enhancing programming skills.

Uploaded by

kumar4383
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)
4 views48 pages

C Lab Program

The document contains a series of C programming exercises, each with a specific task such as arithmetic operations, calculating averages, converting characters, and determining geometric properties. Each exercise includes a program code snippet, expected output, and instructions for user input. The tasks range from basic operations to more complex calculations like interest and area, aimed at enhancing programming skills.

Uploaded by

kumar4383
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/ 48

I Semester Computer Lab Practical’s

1.Write a c Program arithmetic operators


Program:
#include <stdio.h>
int main()
{
int a,b;
printf(“enter the numbers”);
scanf("%d",&a);
scanf("%d",&b);
printf("%d",a+b);
printf("\n%d",a-b);
printf("\n%d",a*b);
printf("\n%d",a/b);
printf("\n%d",a%b);
return 0;
}
OUTPUT:

enter the numbers100


200
300
-100
20000
0
100
2.Write a c Program calculate average of first n numbers
Program:
#include <stdio.h>
int main()
{
int n,sum;
float average;
printf("Enter the value of n ");
scanf("%d", &n);
sum = (n * (n + 1)) / 2;
average = sum / n;
printf("The average of the first %d numbers is: %f\n", n, average);
return 0;
}
OUTPUT:

Enter the value of n 1010


The average of the first 10 numbers is: 5.000000
3. Write a c Program convert given character is Lowercase to Uppercase & vice versa
Program:
#include<stdio.h>
int main()
{
char lowerChar, upperChar;
int ascii;
printf("Enter a lowercase Character: ");
scanf("%c", &lowerChar);
ascii = lowerChar;
upperChar = ascii-32;
printf("\%c", upperChar);
return 0;
}
OUTPUT:
Enter a lowercase Character w
W
4. Write a c Program average of 2 numbers
Program:s
#include <stdio.h>
int main()
{
int num1, num2, average;
printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);
average = (num1 + num2) / 2;
printf("The average of %d and %d is: %d\n", num1, num2, average);
return 0;
}
OUTPUT:

Enter two numbers: 10


5
The average of 10 and 5 is: 7
5. Write a c Program swap of 2 numbers
Program:
#include <stdio.h>
int main()
{
int a, b, temp;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
temp = a;
a = b;
b = temp;
printf("After swapping:\n");
printf(" %d %d\n", a, b);
return 0;
}
OUTPUT:

Enter two numbers: 2


4
After swapping:
42
6. Write a c Program Area and Perimeter of a Square:
Program:
#include <stdio.h>
int main()
{
float side, area, perimeter;
printf("Enter the length of the side of the square: ");
scanf("%f", &side);
area = side * side;
perimeter = 4 * side;
printf("Area of the square: %f\n", area);
printf("Perimeter of the square: %f\n", perimeter);
return 0;
}
OUTPUT:
Enter the length of the side of the square: 2
Area of the square: 4.000000
Perimeter of the square: 8.000000
7. Write a c Program Calculate the Circumference of a Circle:
Program:
#include <stdio.h>
#define PI 3.14159
int main()
{
float radius, circumference;
printf("Enter the radius of the circle: ");
scanf("%f", &radius);
circumference = 2 * PI * radius;
printf("The circumference of the circle is: %f\n", circumference);
return 0;
}
OUTPUT:
Enter the radius of the circle: 12
The circumference of the circle is: 75.398163
8.Write a c Program Surface Area of a Sphere:
Program:
#include <stdio.h>
#define PI 3.14159
int main()
{
float radius, surfaceArea;
printf("Enter the radius of the sphere: ");
scanf("%f", &radius);
surfaceArea = 4 * PI * radius * radius;
printf("The surface area of the sphere is: %f\n", surfaceArea);
return 0;
}
OUTPUT:
Enter the radius of the sphere: 23
The surface area of the sphere is: 6647.604492
9. Write a c Program volume of cylinder
Program:
#include <stdio.h>
#define PI 3.14159 // Define the value of Pi
int main()
{
float radius, height, volume;
printf("Enter the radius of the cylinder: ");
scanf("%f", &radius);
printf("Enter the height of the cylinder: ");
scanf("%f", &height);
volume = PI * radius * radius * height;
printf("The volume of the cylinder is: %f cubic units\n", volume);
return 0;
}
OUTPUT:
Enter the radius of the cylinder: 12
Enter the height of the cylinder: 2
The volume of the cylinder is: 904.777893 cubic units
10. Write a c Program to Find the Square and Cube of a Number
Program:
#include <stdio.h>
int main()
{
Int num;
printf("Enter a number: ");
scanf("%d", &num);
int square = num * num;
int cube = num * num * num;
printf("The square of %d is: %d\n", num, square);
printf("The cube of %d is: %d\n", num, cube);
return 0;
}
OUTPUT:
Enter a number: 4
The square of 4 is: 16
The cube of 4 is: 64
11. Write a c Program simple interest and compound interest
Program:
#include <stdio.h>
#include <math.h>
int main()
{
float principal, rate, time, simpleInterest, compoundInterest, amount;
printf("Enter the principal amount: ");
scanf("%f", &principal);
printf("Enter the rate of interest (in percentage): ");
scanf("%f", &rate);
printf("Enter the time period (in years): ");
scanf("%f", &time);
simpleInterest = (principal * rate * time) / 100;
amount = principal * pow((1 + rate / 100), time); // Amount after compound interest
compoundInterest = amount - principal;
printf("\nSimple Interest = %f\n", simpleInterest);
printf("Compound Interest = %f\n", compoundInterest);
return 0;
}
OUTPUT:
Enter the principal amount: 12
Enter the rate of interest (in percentage): 2
Enter the time period (in years): 3
Simple Interest = 0.720000
Compound Interest = 0.734495
12. Write a c Program to find area of circle
Program::
#include <stdio.h>
#define PI 3.14159
int main()
{
float radius, area;
printf("Enter the radius of the circle: ");
scanf("%f", &radius);
area = PI * radius * radius;
printf("The area of the circle is: %f", area);
return 0;
}
OUTPUT:
Enter the radius of the circle:2
The area of the circle is: 12.566360
13. Write a c Program To find the ASCII value of a character in C
Program:
#include <stdio.h>
int main()
{
char ch;
printf("Enter a character: ");
scanf("%c", &ch);
printf("The ASCII value of '%c' is %d\n", ch,ch);
return 0;
}
OUTPUT:
Enter a character: a
The ASCII value of 'a' is 97
14.Write a c Program to calculate pow(x,n)
Program:
#include <stdio.h>
#include <math.h>
int main()
{
int x, result;
int n;
printf("Enter the base (x) ");
scanf("%d", &x);
printf("Enter the exponent (n)");
scanf("%d", &n);
result = pow(x, n);
printf("%d %d %d", x, n, result);
return 0;
}
OUTPUT:
Enter the base (x)2
Enter the exponent (n) 2
224
15. Write a c Program To convert a temperature from Fahrenheit to Celsius
Program:
#include <stdio.h>
int main()
{
float fahrenheit, celsius;
printf("Enter temperature in Fahrenheit: ");
scanf("%f", &fahrenheit);
celsius = (5.0 / 9.0) * (fahrenheit - 32);
printf("%f Fahrenheit is equal to %f Celsius.\n", fahrenheit, celsius);
return 0;
}
OUTPUT:
Enter temperature in Fahrenheit: 3
3.00 Fahrenheit is equal to -16.11 Celsius.
16. Write a c Program leap year from 1900 to2100
Program:
#include<stdio.h>
void main()
{
int year;
printf("Enter a year");
scanf("%d",&year);
if((year%4==0)&&(year%400==0)||(year%100!=0))
{
printf("%d is a leap year",year);
}
else
{
printf("%d is not a leap year",year);
}
return o;
}
OUTPUT:
Enter a year 2016
2016 is a leap year
17 . write a c Program: for person is eligible for vote
Program:
#include <stdio.h>
int main()
{
int age;
printf("Enter your age: ");
scanf("%d", &age);
if (age >= 18)
{
printf("You are eligible to vote.\n");
}
else
{
printf("You are not eligible to vote.\n");
}
return 0;
}
OUTPUT:
Enter your age: 23
You are eligible to vote.
18. Write a c Program largest of 2 numbers
Program:
#include <stdio.h>
int main()
{
int a, b;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
if (a > b)
{
printf("a is largest number ");
}

else
{
printf("b is largest number ");
}
return 0;
}
OUTPUT:
Enter two numbers: 10
2
a is largest number
19. Write a c Program to check the given no is positive or negative
Program:
#include <stdio.h>
int main()
{
int n;
printf("enter a number");
scanf("%d",&n);
if(n>0)
printf("positive");
else
printf("negative");
return 0;
}
OUTPUT:
enter a number 22
positive
20. Write a Program: to print given number is even or odd number
Program:
#include <stdio.h>
int main()
{
int n;
printf(“enter a number”);
scanf("%d",&n);
if(n%2==0)
{
printf("Even");
}
else
{
printf("Odd");
}
return 0;
}
OUTPUT:
enter a number2
Even
21. Write a Program: to find smallest of given two numbers
Program:
#include <stdio.h>
int main()
{
int a,b;
printf("enter a two numbers");
scanf("%d",&a);
scanf("%d",&b);
if(a<b)
{
printf("%d",a);
}
else
{
printf("%d",b);
}
return 0;
}
OUTPUT:
enter a two numbers 1010
2
2
22. Write a c Program biggest of 3 numbers
Program:
#include <stdio.h>
int main()
{
int num1, num2, num3;
printf("Enter three numbers: ");
scanf("%d %d %d", &num1, &num2, &num3);
if (num1 >= num2 && num1 >= num3)
{
printf("The largest number is: %d\n", num1);
}
else if (num2 >= num1 && num2 >= num3)
{
printf("The largest number is: %d\n", num2);
}
else
{
printf("The largest number is: %d\n", num3);
}
return 0;
}
OUTPUT:
Enter three numbers: 10 2 3
The largest number is: 10
23. Write a c Program largest of 6 numbers
Program:s
#include <stdio.h>
int main()
{
int a, b, c, d, e, f;
printf("Enter six numbers: ");
scanf("%d %d %d %d %d %d", &a, &b, &c, &d, &e, &f);
int largest = a;
if (b > largest)
{
largest = b;
}
if (c > largest)
{
largest = c;
}
if (d > largest)
{
largest = d;
}
if (e > largest)
{
largest = e;
}
if (f > largest)
{
largest = f;
}
printf("The largest number is: %d\n", largest);
return 0;
}
OUTPUT:
Enter six numbers: 10
203
04
05
06
0
The largest number is: 20
24. Write a c Program entered character is vowel or not
Program:
#include <stdio.h>
int main()
{
char ch;
printf("Enter a character: ");
scanf("%c", &ch);
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' ||
ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U')
{
printf("%c is a vowel.\n", ch);
}
else
{
printf("%c is not a vowel.\n", ch);
}
return 0;
}
OUTPUT:
Enter a character: w
w is not a vowel.
25. Write a Program: to read the marks of 3 subjects and display the total, avg, class
Program:
#include <stdio.h>
int main()
{
float marks1, marks2, marks3, total, average;
printf("Enter the marks for three subjects: ");
scanf("%f %f %f", &marks1, &marks2, &marks3);
total = marks1 + marks2 + marks3;
average = total / 3;
printf("Total marks = %.2f\n", total);
printf("Average marks = %.2f\n", average);
if (average >= 60)
{
printf("Class: First Class\n");
}
else if (average >= 50)
{
printf("Class: Second Class\n");
}
else if (average >= 40)
{
printf("Class: Third Class\n");
}
else
{
printf("Class: Fail\n");
}
return 0;
}
OUTPUT:
Enter the marks for three subjects: 100
200
300
Total marks = 600.00
Average marks = 200.00
Class: First Class
26. Write a c Program to Print the Day of the Week Using a Switch Statement
Program:
#include <stdio.h>
int main()
{
int day;
printf("Enter a number (1-7) to get the day of the week: ");
scanf("%d", &day);
switch (day)
{
case 1:printf("Sunday\n");
break;
case 2:printf("Monday\n");
break;
case 3:printf("Tuesday\n");
break;
case 4:printf("Wednesday\n");
break;
case 5:printf("Thursday\n");
break;
case 6:printf("Friday\n");
break;
case 7:printf("Saturday\n");
break;
default: printf("Invalid input! Please enter a number between 1 and 7.\n");
}
return 0;
}
OUTPUT:
Enter a number (1-7) to get the day of the week: 6
Friday
27. write a Program: to read a character until a * encountered
Program:
#include <stdio.h>
int main()
{
char ch;
printf("Enter characters (enter '*' to stop):\n");
while (1)
{
ch = getchar();
if (ch == '*')
{
break;
}
printf("You entered: %c\n", ch);
}
printf("Program: stopped, '*' encountered.\n");
return 0;
}
OUTPUT:

Enter characters (enter '*' to stop):


*
Program: stopped, '*' encountered.
28. Write a c Program calculate sum of numbers from m to n
Program:
#include <stdio.h>
int main()
{
int m, n, sum = 0;
printf("Enter the starting number (m): ");
scanf("%d", &m);
printf("Enter the ending number (n): ");
scanf("%d", &n);
for (inti = m; i<= n; i++)
{
sum += i;
}
printf("The sum of numbers from %d to %d is: %d\n", m, n, sum);
return 0;
}
OUTPUT:Enter the starting number (m): 2
Enter the ending number (n): 4
The sum of numbers from 2 to 4 is: 9
29. Write a c Program factorial of a number
Program:
#include <stdio.h>
int main()
{
int n, i;
int factorial = 1;
printf("Enter a number: ");
scanf("%d", &n);
for (i = 1; i<= n; i++)
{
factorial *= i;
}
printf("Factorial of %d is %d\n", n, factorial);
return 0;
}
OUTPUT:
Enter a number: 10
Factorial of 10 is 3628800
30. Write a c Program print the reverse of a number
Program:
#include <stdio.h>
int main()
{
intnum, reversed = 0, remainder;
printf("Enter an integer: ");
scanf("%d", &num);
while (num != 0)
{
remainder = num % 10;
reversed = reversed * 10 + remainder;
num = num / 10;
}
printf("Reversed number: %d\n", reversed);
return 0;
}
OUTPUT:
Enter an integer: 123
Reversed number: 321
31. Write a c Program calculate the sum of its digits
Program:
#include<stdio.h>
int main()
{
intn,sum=0,m;
printf("Enter a number:");
scanf("%d",&n);
while(n>0)
{
m=n%10;
sum=sum+m;
n=n/10;
}
printf("%d",sum);
return 0;
}
OUTPUT:
Enter number:12
3
32. Write a c Program to Calculate the Sum of 10 Numbers
Program:
#include <stdio.h>
int main()
{
int sum = 0, num;
printf("Enter 10 numbers:\n");
for (inti = 1; i<= 10; i++)
{
printf("Number %d: ", i);
scanf("%d", &num);
sum += num;
}
printf("The sum of the 10 numbers is: %d\n", sum);
return 0;
}
OUTPUT:
Enter 10 numbers:
Number 1: 2
Number 2: 3
Number 3: 4
Number 4: 5
Number 5: 6
Number 6: 7
Number 7: 8
Number 8: 9
Number 9: 1
Number 10: 2
The sum of the 10 numbers is: 47
33. Write a c Program to Print Multiplication Table
Program:
#include <stdio.h>
int main()
{
int num;
printf("Enter a number to print its multiplication table: ");
scanf("%d", &num);
printf("Multiplication table of %d:\n", num);
for (inti = 1; i<= 10; i++)
{
printf("%d x %d = %d\n", num, i, num * i);
}
return 0;
}
OUTPUT:
Enter a number to print its multiplication table: 2
2x1=2
2x2=4
2x3=6
2x4=8
2 x 5 = 10
2 x 6 = 12
2 x 7 = 14
2 x 8 = 16
2 x 9 = 18
2 x 10 = 20
34. Write a c Program to Check If a Number Is Prime
#include <stdio.h>
int main()
{
int num, i, isPrime = 1;
printf("Enter a number: ");
scanf("%d", &num);
if (num<= 1)
{
isPrime = 0;
}
else
{
for (i = 2; i * i<= num; i++)
{
if (num % i == 0)
{
isPrime = 0;
break;
}
}
}
if (isPrime)
{
printf("%d is a prime number.\n", num);
}
else
{
printf("%d is not a prime number.\n", num);
}
return 0;
}
OUTPUT:
Enter a number: 2
2 is a prime number.
35. Write a c Program print 20 horizontal asterisks
Program:
#include <stdio.h>
int main()
{
for (inti = 0; i< 20; i++)
{
printf("*");
}
printf("\n");
return 0;
}
OUTPUT:
********************
36. Write a c Program Diamond Pattern
Program:
#include <stdio.h>
int main()
{
int rows;
printf("Enter number of rows: ");
scanf("%d", &rows);
for (int i = 1; i<= rows; i++)
{
for (int j = i; j < rows; j++)
{
printf(" ");
}
for (int j = 1; j <= (2 * i - 1); j++)
{
printf("*");
}
printf("\n");
}
for (inti = rows - 1; i>= 1; i--)
{
for (int j = rows; j >i; j--)
{
printf(" ");
}
for (int j = 1; j <= (2 * i - 1); j++)
{
printf("*");
}
printf("\n");
}
return 0;
}
OUTPUT:
Enter number of rows: 5
*
***
*****
*******
*********
*******
*****
***
*
37. Write a c Program to print the given no in words
Program:
#include <stdio.h>
void printInWords(intnum)
{
char *ones[] = {"", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight",
"Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen",
"Seventeen", "Eighteen", "Nineteen"};
char *tens[] = {"", "", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy",
"Eighty", "Ninety"};
char *thousands[] = {"", "Thousand", "Hundred"};
if (num == 0) {
printf("Zero\n");
return;
}
char result[1000] = "";
if (num>= 1000) {
printf("%s Thousand ", ones[num / 1000]);
num %= 1000;
}
if (num>= 100) {
printf("%s Hundred ", ones[num / 100]);
num %= 100;
} if (num>= 20) {
printf("%s ", tens[num / 10]);
num %= 10;
}
if (num> 0) {
printf("%s", ones[num]);
}
printf("\n");
}
int main() {
intnum;
printf("Enter a number (0 to 9999): ");
scanf("%d", &num);
printInWords(num);
return 0;
}

OUTPUT:
Enter a number (0 to 9999): 45
Forty Five
38.Write a c Program sum of array elements
Program:
#include<stdio.h>
int main()
{
intarr[100], size, i, sum = 0;
printf("Enter array size\n");
scanf("%d",&size);
printf("Enter array elements\n");
for(i = 0; i< size; i++)
scanf("%d",&arr[i]);
for(i = 0; i< size; i++)
sum = sum + arr[i];
printf("Sum of the array = %d\n",sum);
return 0;
}
OUTPUT:
Enter array size
3
Enter array elements
123
Sum of the array = 6
39. Write a c Program find largest in array
Program:
#include <stdio.h>
int main() {
int n, i, largest;
printf("Enter the number of elements: ");
scanf("%d", &n);
intarr[n];
printf("Enter %d elements:\n", n);
for (i = 0; i< n; i++) {
scanf("%d", &arr[i]);
}
largest = arr[0];
for (i = 1; i< n; i++) {
if (arr[i] > largest) {
largest = arr[i];
}
}
printf("The largest element in the array is: %d\n", largest);
return 0;
}
OUTPUT:
Enter the number of elements: 6
Enter 6 elements:
123456
The largest element in the array is: 6
40.Write a c Program simple sorting
Program:
#include<stdio.h>
int main()
{
int a[6]= {12,5,10,9,7,6};
int temp;
inti, j;
printf("Before Sorting ");
for(i=0; i<6; i++)
{
printf("%d ",a[i]);
}

for(i=0; i<6; i++)


{
for(j=i+1; j<6; j++) { if(a[i]>a[j])
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
printf("\nAfter Sorting ");
for(i=0; i<6; i++)
{
printf("%d ",a[i]);
}
return 0;
}
OUTPUT:
Before Sorting 12 5 10 9 7 6
After Sorting 5 6 7 9 10 12
41.Write a c Program simple searching in array
Program:
#include <stdio.h>
int main()
{
int n, i, target, found = 0;
printf("Enter the number of elements: ");
scanf("%d", &n);
intarr[n];
printf("Enter %d elements:\n", n);
for (i = 0; i< n; i++)
{
scanf("%d", &arr[i]);
}
printf("Enter the element to search for: ");
scanf("%d", &target);
for (i = 0; i< n; i++)
{
if (arr[i] == target)
{
found = 1; // Element found
printf("Element %d found at index %d\n", target, i);
break;
}
}
if (!found)
{
printf("Element %d not found in the array\n", target);
}
return 0;
}
OUTPUT:
Enter the number of elements: 3
Enter 3 elements:
123
Enter the element to search for: 2
Element 2 found at index 1
42. Write a c Program matrix addition
Program:
#include <stdio.h>
int main()
{
int r, c, a[100][100], b[100][100], sum[100][100], i, j;
printf("Enter the number of rows (between 1 and 100): ");
scanf("%d", &r);
printf("Enter the number of columns (between 1 and 100): ");
scanf("%d", &c);
printf("\nEnter elements of 1st matrix:\n");
for (i = 0; i< r; i++)
{
for (j = 0; j < c; j++)
{
printf("Enter element a%d%d: ", i + 1, j + 1);
scanf("%d", &a[i][j]);
}
}
printf("Enter elements of 2nd matrix:\n");
for (i = 0; i< r; i++)
{
for (j = 0; j < c; j++)
{
printf("Enter element a%d%d: ", i + 1, j + 1);
scanf("%d", &b[i][j]);
}
}
for (i = 0; i< r; i++)
{
for (j = 0; j < c; j++)
{

diff[i][j] = a[i][j] - b[i][j];


}
}
printf("\nSum of two matrices: \n");
for (i = 0; i< r; i++)
{
for (j = 0; j < c; j++)
{
printf("%d ", sum[i][j]);
}
printf("\n\n");
}
return 0;
}

OUTPUT:
Enter the number of rows (between 1 and 100): 2
Enter the number of columns (between 1 and 100): 2

Enter elements of 1st matrix:


Enter element a11: 1
Enter element a12: 2
Enter element a21: 3
Enter element a22: 4
Enter elements of 2nd matrix:
Enter element b11: 5
Enter element b12: 6
Enter element b21: 7
Enter element b22: 8
Sum of two matrices:
6 8
10 12
43.Write a c Program matrix subtraction
Program:
#include <stdio.h>
int main()
{
int m, n, i, j, A[m][n], B[m][n], diff[m][n];
printf("Enter the number of rows and columns of the matrices: ");
scanf("%d %d", &m, &n);
printf("Enter elements for matrix A:\n");
for (i = 0; i< m; i++)
{
for (j = 0; j < n; j++)
{
printf("A[%d][%d]: ", i + 1, j + 1);
scanf("%d", &A[i][j]);
}
}
printf("Enter elements for matrix B:\n");
for (i = 0; i< m; i++)
{
for (j = 0; j < n; j++)
{
printf("B[%d][%d]: ", i + 1, j + 1);
scanf("%d", &B[i][j]);
}
}
for (i = 0; i< r; ++i)
{
for (j = 0; j < c; ++j)
{
diff[i][j] = a[i][j] - b[i][j];
}
}
printf("Resulting matrix (Difference of A and B):\n");
for (i = 0; i< r; ++i)
{
for (j = 0; j < c; ++j)
{
printf("%d ", sum[i][j]);
}
printf("\n\n");
}
return 0;
}
}
return 0;
}
OUTPUT:
Enter the number of rows and columns of the matrices: 2
2
Enter elements for matrix A:
A[1][1]: 1
A[1][2]: 2
A[2][1]: 3
A[2][2]: 4
Enter elements for matrix B:
B[1][1]: 5
B[1][2]: 6
B[2][1]: 7
B[2][2]: 8
Resulting matrix (Difference of A and B):
-4 -4
-4 -4
44.Write a c Program to sort the elements in ascending order
Program:
#include <stdio.h>
int main()
{
int n, i, j, temp;
printf("Enter the number of elements: ");
scanf("%d", &n);
intarr[n];
printf("Enter the elements:\n");
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("Array sorted in ascending order: ");
for (i = 0; i< n; i++)
{
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
OUTPUT:
Enter the number of elements: 3
Enter the elements:
432
Array sorted in ascending order: 2 3 4
45 Write a c Program to sort the elements in descending order
Program:
#include <stdio.h>
int main()
{
int n, i, j, temp;
printf("Enter the number of elements: ");
scanf("%d", &n);
intarr[n];
printf("Enter the elements:\n");
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("Array sorted in descending order: ");
for (i = 0; i< n; i++)
{
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
OUTPUT:
Enter the number of elements: 3
Enter the elements:
232
Array sorted in descending order: 3 2 2

You might also like