0% found this document useful (0 votes)
74 views

C Program

The document contains solutions to programming assignments in C language. It includes programs to: 1) Display a message and sum of two numbers. 2) Take input from the user and find sum. 3) Find sum and average of three numbers. 4) Swap values of two variables with and without a third variable. 5) Check if a number is even or odd. 6) Check if a year is a leap year. 7) Find the roots of a quadratic equation. 8) Perform arithmetic operations using switch statement. 9) Check if a number is perfect or Armstrong or strong. 10) Generate prime numbers between 1 to n.

Uploaded by

pd03487
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
74 views

C Program

The document contains solutions to programming assignments in C language. It includes programs to: 1) Display a message and sum of two numbers. 2) Take input from the user and find sum. 3) Find sum and average of three numbers. 4) Swap values of two variables with and without a third variable. 5) Check if a number is even or odd. 6) Check if a year is a leap year. 7) Find the roots of a quadratic equation. 8) Perform arithmetic operations using switch statement. 9) Check if a number is perfect or Armstrong or strong. 10) Generate prime numbers between 1 to n.

Uploaded by

pd03487
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 60

ASSIGNMENT – 1.

1.a) Write a C program to display “This is my first C program.”

Ans.-

#include<stdio.h>

#include<conio.h>

int main(void)

printf("This is my first C Program");

Output: -

This is my first C Program

1.b) Write a C program to add two numbers(2 and 6) and display its

sum.

Ans.) #include <stdio.h>

#include <conio.h>

int main()

int a,b,c;

a=2, b=6;

c=a+b;

printf("The sum of two number: %d",c);

return 0;

getch();

Output: - The sum of two number: 8


1.c) Write a C program to take input from user and display its sum.
Ans.)
#include <stdio.h>
#include <conio.h>
int main()
{
int a,b,c;
printf("Enter the 1st value.\n");
scanf("%d",&a);
printf("Enter the 2nd value.\n");
scanf("%d",&b);
c=a+b;
printf("The Sum of two number is %d",c);
return 0;
getch();
}
Output: -
Enter the 1st value.
8
Enter the 2nd value.
5
The Sum of two number is 13.
1.d) Write a C program to find sum and average of three numbers.
Ans.)
#include <stdio.h>
#include <conio.h>
int main()
{
int a,b,c,d;
float avg;
printf("Enter the 1st value: ");
scanf("%d",&a);
printf("Enter the 2nd value: ");
scanf("%d",&b);
printf("Enter the 3rd value: ");
scanf("%d",&c);
d=a+b+c;
printf("The Sum of two number is %d \n",d);
avg=d/3;
printf("\nAvg of three number is %f \n",avg);
return 0;
getch();
}
Output: -
Enter the 1st value: 4
Enter the 2nd value: 5
Enter the 3rd value: 3
The Sum of two number is 12
Avg of three number is 4.000000
1.e) Write a C program to perform swap the value of two variables with
& without using third variables.
Ans.)
// Swap 2 number using third variables

#include<stdio.h>
#include<conio.h>
int main()
{
int x,y,z;
printf("Enter the value of x\n");
scanf("%d",&x);
printf("Enter the value of y\n");
scanf("%d",&y);
z=x;
x=y;
y=z;
printf("Swap of two number: x=%d , y=%d",x,y);
return 0;
getch ();
}
Output: -
Enter the value of x
10
Enter the value of y
30
Swap of two number: x=30 , y=10
1.e) Write a C program to perform swap the value of two variables with
& without using third variables.
Ans.) // Swap 2 number without using third variables
#include<stdio.h>
#include<conio.h>
int main()
{
int x,y;
printf("Enter the 1st value of x\n");
scanf("%d",&x);
printf("Enter the 2nd value of y\n");
scanf("%d",&y);
x=x+y;
y=x-y;
x=x-y;
printf("Swap of two number: x=%d , y=%d",x,y);
return 0;
getch ();
}
Output: -
Enter the 1st value of x
20
Enter the 2nd value of y
40
Swap of two number: x=40 , y=20
1.f) Write a C program to check whether a given number is odd or even.
Ans.)
// Check whether a given number is Odd or Even

#include<stdio.h>
#include<conio.h>
int main()
{
int x,y;
printf("Enter the value.\n");
scanf("%d",&x);
y=x%2;
if(y==0)
printf("\n %d is Even number.",x,y);
else
printf("\n %d is Odd number.",x,y);
return 0;
getch();
}
Output: -
Enter the value.
10
10 is Even number.
NEXT INPUT🡪
Enter the value.
21
21 is Odd number.
1.g) Write a C program to check leap year or not.
Ans.) // To check leap year or not.
#include<stdio.h>
#include<conio.h>
int main()
{
int year;
printf("Enter a year to check if it is a leap year\n");
scanf("%d",&year);
if(year%400==0)
printf("%d is a leap year.\n",year);
else if(year%100==0)
printf("%d is not a leap year.\n",year);
else if(year%4==0)
printf("%d is a leap year.\n",year);
else
printf("%d is not a leap year.\n",year);
return 0;
getch();
}
Output: -
Enter a year to check if it is a leap year
1986
1986 is not a leap year.
NEXT INPUT🡪
Enter a year to check if it is a leap year
2124
2124 is a leap year.
ASSIGNMENT – 2.
2.a) Write a C program to find the roots of a quadratic equation.
Ans.)
#include<stdio.h>
#include<math.h>
int main(void)
{
int a,b,c,d;
float x1,x2;
printf("Enter the value a\n");
scanf("%d",&a);
printf("Enter the value b\n");
scanf("%d",&b);
printf("Enter the value c\n");
scanf("%d",&c);
d=(b*b-4*a*c);
if(d==0)
{
printf("Both roots are real.\n");
x1=(-b)/(2.0*a);
x2=x1;
printf("First Root Root1=%f\n",x1);
printf("Second Root Root2=%f\n",x2);

}
else if(d>0)
{
printf("Boot roots are real and diff\n");
x1=(-b+sqrt(d))/(2.0*a);
x2=(-b-sqrt(d))/(2.0*a);
printf("First Root Root1=%f\n",x1);
printf("Second Root Root2=%f\n",x2);

}
else
printf("Root are imaginary;\n No solution\n");
}
Output: -

Enter the value a Enter the value a


4 2
Enter the value b NEXT Enter the value b
2 INPUT🡪 9
Enter the value c Enter the value c
8 3
Roots are imaginary. Boots roots are real and diff
No solution First Root Root1= -0.362541
Second Root Root2= -4.137459
2.b) Write a C program perform arithmetic operations using switch
statement.
Ans.)
#include<stdio.h>
#include<conio.h>
int main()
{
int x,y,z,choice;
printf("Enter a value: ");
scanf("%d",&x);
printf("Enter another value: ");
scanf("%d",&y);
printf("\n1-Add,2-Sub,3-Mul,4-Div\n");
printf("\nEnter your choice\n");
scanf("%d",&choice);
switch(choice)
{
case 1:
z=x+y;
printf("The addition is %d",z);
break;
case 2:
z=x-y;
printf("The substruction is %d",z);
break;
case 3:
z=x*y;
printf("The multiplication is %d",z);
break;
case 4:
z=x/y;
printf("The division is %d",z);
break;
default:
printf("Wrong choice");
return 0;
getch();
}
}
Output: -
Enter a value: 4
Enter another value: 5
1-Add, 2-Sub, 3-Mul, 4-Div
Enter your choice
3
The multiplication is 20.
NEXT INPUT🡪
Enter a value: 6
Enter another value: 5
1-Add, 2-Sub, 3-Mul, 4-Div
Enter your choice
5
Wrong choice
2.c.) Write a C program to check whether given number is perfect number or
Not.
Ans.)
#include<stdio.h>
#include<conio.h>
int main ()
{
int i,n,sum1=0;
printf("Enter the number\n");
scanf("%d",&n);
for(i=1;i<n;i++)
{
if (n%i==0)
{
sum1=sum1+i;
}
}
if(sum1==n)
printf("It is a perfect number");
else
printf("It is not perfect number");
return 0;
getch ();
}
Output: -
Enter the number Enter the number
496 NEXT INPUT🡪 499
It is a perfect number It is not perfect number
2.d.) Write a C program to generate prime numbers between 1 to n.
Ans.)
#include<stdio.h>
#include<conio.h>
int main(void)
{
int n,m,i,j,count;
printf("Enter a number:\n");
scanf("%d",&n);
printf("\nList of prime numbers between 1 to %d:\n ",n);
for(i=1;i<=n;i++)
{
m=i;
count=0;
for(j=1;j<=m;j++)
{
if(m%j==0)
{
count++;
}
}
if(count==2)
{
printf("%d ",m);
}
}
}
Output: -
Enter a number:
50
List of prime numbers between 1 to 50:
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47

ASSIGNMENT – 3.
3.a.) Write a C program to check whether given number is Armstrong

number or not.

Ans.)

#include<stdio.h>

#include<conio.h>

int main(void)

int a,n,b=0,t;

printf("Enter the number:\n");

scanf("%d",&n);

t=n;

while(n>0)

a=n%10;

b=b+a*a*a;

n=n/10;
n=n/10;
}
if(b==t)
{

printf("Armstrong number");

else

printf("Not an Armstrong number");

getch();

Output: -

Enter the number:

153

Armstrong number

NEXT INPUT🡪

Enter the number:

145

Not an Armstrong number


3.b.) Write a C program to check whether given number is strong number
or not.
Ans.)
#include<stdio.h>
#include<conio.h>
int main ()
{
int n,s=0,fact,i,r,n1;
printf("Enter the number:\n");
scanf("%d",&n);
n1=n;
while(n>0)
{
fact=1;
r=n%10;
for(i=1;i<=r;i++)
{
fact=fact*i;
}
s=s+fact;
n=n/10;
}
printf("%d ",s);
if(n1==s)
{
printf("\nThe number is a Strong Number");
}
else
{
printf("\nThe number is not Strong Number");
}
return 0;
getch ();
}

Output: -
Enter the number:
145
145
The number is a Strong Number
NEXT INPUT🡪
Enter the number:
45
144
The number is not Strong Number
3.c.) Write a C program to generate the first n terms of the Fibonacci
series.
Ans.)
#include<stdio.h>
#include<conio.h>
int main(void)
{
int a,b,c,i,n;
a=0;
b=1;
printf("\nEnter n for how many times generate series:\n");
scanf("%d",&n);
printf("\nFIBONACCI SERIES\n");
printf(" %d %d",a,b);
for(i=0;i<n;i++)
{
c=a+b;
a=b;
b=c;
printf(" %d",c);
}
getch();
}
Output: -
/ / Generate the first n terms of Fibonacci Series
Enter n for how many times generate series
12
FIBONACCI SERIES
0 1 1 2 3 5 8 13 21 34 55 89 144 233

3.d.) Write a C program to find GCD & LCM of a given number.


Ans.)
#include<stdio.h>
#include<conio.h>
#include<math.h>
int main()
{
int num1, num2, gcd, lcm, remainder, numerator, denominator;
printf("Enter two numbers:\n");
scanf("%d %d", &num1, &num2);
if (num1 > num2)
{
numerator = num1;
denominator = num2;
}
else
{
numerator = num2;
denominator = num1;
}
remainder = num1 % num2;
while (remainder != 0)
{
numerator = denominator;
denominator = remainder;
remainder = numerator % denominator;
}
gcd = denominator;
lcm = num1 * num2 / gcd;
printf("GCD of %d and %d = %d\n", num1, num2, gcd);
printf("LCM of %d and %d = %d\n", num1, num2, lcm);
return 0;
getch();
}
Output: -
Enter two numbers:
24
8
GCD of 24 and 8 = 8
LCM of 24 and 8 = 24
ASSIGNMENT – 4.
4.a.) Write a C program to find factorial of a given integer using non –
- recursive function.
Ans.)
#include<stdio.h>
#include<conio.h>
int main()
{
int i,n,fact=1;
printf("Enter the number.\n");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
fact=fact*i;
}
printf("The factorial %d = %d",n,fact);
return 0;
getch ();
}
Output: -
Enter the number.
5
The factorial 5 = 120
NEXT INPUT 🡪
Enter the number.
7
The factorial 7 = 5040
4.b.) Write a C program to find factorial of a given integer using recursive
function.
Ans.)
#include<stdio.h>
#include<conio.h>
int fact(int);
int main()
{
int n,m;
printf("Enter the value \n");
scanf("%d",&n);
m=fact(n);
printf("The factorial of %d=%d",n,m);
return 0;
getch();
}
int fact(int x)
{
if(x<=1)
{
return 1;
}
else
{
return x*fact(x-1);
}
}
Output: -
Enter the number.
6
The factorial of 6 = 720
4.c.) Write a C program to find GCD of two integers by using recursive
function.
Ans.)
#include<stdio.h>
#include<conio.h>
int gcd(int,int);
int main()
{
int x,y,z;
printf("Enter the first value. \n");
scanf("%d",&x);
printf("Enter the second value. \n");
scanf("%d",&y);
z=gcd(x,y);
printf("GCD = %d",z);
return 0;
getch ();
}
int gcd(int x,int y)
{
if(y!=0)
return gcd(y,x%y);
else
return x;
}
Output: -
Enter the first value.
44
Enter the second value.
24
GCD = 4
4.d.) Write a C program to find GCD of two integers using non – recursive
function.
Ans.)
#include<stdio.h>
#include<conio.h>
int main()
{
int n1,n2,i,GCD_NUM;
printf("Enter the first integers.\n");
scanf("%d",&n1);
printf("Enter the second integers.\n");
scanf("%d",&n2);
for(i=1;i<=n1&&i<=n2;++i)
{
if(n1%i==0&&n2%i==0)
GCD_NUM=i;
}
printf("GCD of two numbers %d and %d is %d.",n1,n2,GCD_NUM);
return 0;
}
Output: -
Enter the first integers.
96
Enter the second integers.
36
GCD of two numbers 96 and 36 is 12.
4.e.) Write a C program to generate the first n terms of the Fibonacci
sequence using recursion.
Ans.)
#include<stdio.h>
#include<conio.h>
int recursivefibo(int);
int main()
{
int m,i;
printf("\nEnter m for how many times generate series:\n");
scanf("%d",&m);
printf("The Fibonacci series of these numbers would be equal to:\n");
for(i=0;i<m;i++)
{
printf("%d ",recursivefibo(i));
}
getch();
}
int recursivefibo(int i)
{
if(i==0)
return 0;
else if(i==1)
return 1;
else
return (recursivefibo(i-1)+recursivefibo(i-2));
}
Output: -
Enter m for how many times generate series:
15
The Fibonacci series of these numbers would be equal to:
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377

ASSIGNMENT – 5.
5.a.) Write a C program to add two numbers using pointers.
Ans.)
#include<stdio.h>
int main()
{
int a,b,*p,*q,sum;
printf("Enter the 1st value.\n");
scanf("%d",&a);
printf("Enter the 2nd value.\n");
scanf("%d",&b);
p=&a;
q=&b;
sum=*p+*q;
printf("Sum of the numbers = %d\n",sum);
return 0;
}
Output: -
Enter the 1st value. Enter the 1st value.
54 NEXT 115
Enter the 2nd value. INPUT🡪 Enter the 2nd value.
62 987
Sum of the numbers = 116 Sum of the numbers = 1102
5.b.) Write a C program to swap value of two variables using pointer.
Ans.)
#include<stdio.h>
int main()
{
int a,b, *p,*q,temp;
printf("Enter the value of a.\n");
scanf("%d",&a);
printf("Enter the value of b.\n");
scanf("%d",&b);
p=&a;
q=&b;
temp=*p;
*p=*q;
*q=temp;
printf("Swap a = %d , b = %d",a,b);
return 0;
}
Output: -
Enter the value of a.
50
Enter the value of b.
30
Swap a = 30 , b = 50
5.c.) Write a C program to find the sum of all the elements of an array
using pointers.
Ans.)
#include<stdio.h>
int main()
{
int arr[50],i,n;
int *ptr=&arr[0],sum=0;
printf("Enter the size of elements:\n");
scanf("%d",&n);
printf("\n");
for(i=0;i<n;i++)
{
printf("Enter the arr[%d] = ",i);
scanf("%d",&arr[i]);
}
for(i=0;i<n;i++)
{
sum+=*ptr;
ptr++;
}
printf("\n");
printf("Sum of array elements. = %d\n",sum);
return 0;
}
Output: -
Enter the size of elements:
8
Enter the arr[0] = 10
Enter the arr[1] = 20
Enter the arr[2] = 30
Enter the arr[3] = 40
Enter the arr[4] = 50
Enter the arr[5] = 60
Enter the arr[6] = 70
Enter the arr[7] = 80
Sum of all array elements. = 360

5.d.) Write a C program to input and print array elements using pointers.
Ans.)
#include<stdio.h>
int main()
{
int arr[100],i,n;
int *ptr=&arr[0];
printf("Enter the size of elements n:\n");
scanf("%d",&n);
printf("\n");
for(i=0;i<n;i++)
{
printf("Enter the arr[%d]= ",i);
scanf("%d",ptr);
*ptr++;
}
ptr=&arr[0];
printf("\n");
printf("DISPLAY ALL THE NUMBERS TAKEN BY USER.\n");
for(i=0;i<n;i++)
{
printf("%d\t",*(ptr+i));
}
return 0;
}
Output: -
Enter the size of elements n:
7
Enter the arr[0]= 10
Enter the arr[1]= 20
Enter the arr[2]= 30
Enter the arr[3]= 40
Enter the arr[4]= 50
Enter the arr[5]= 60
Enter the arr[6]= 70

DISPLAY ALL THE NUMBERS TAKEN BY USER.


10 20 30 40 50 60 70
5.e.) Write a C program to Display array elements using calloc () &
malloc () function.
Ans.) / / calloc () function.

#include<stdio.h>
#include<stdlib.h>
int main()
{
int* ptr;
int n,i,x;
printf("Enter size of elements:\n");
scanf("%d",&n);
printf("Entered number of elements:%d\n",n);
ptr=(int*)calloc(n,sizeof(int));
if(ptr==NULL)
{
printf("Memory not allocated.\n");
exit(0);
}
else
{
printf("Memory successfully allocated using malloc.\n");
for(i=0;i<n;++i)
{
printf("Enter the elements.\n");
scanf("%d",&x);
ptr[i]=x;
}
printf("The elements of the array are: ");
for(i=0;i<n;++i)
{
printf("%d ",ptr[i]);
}
}
return 0;
}

Output: -
Enter size of elements:
4
Entered number of elements: 4
Memory successfully allocated using malloc.
Enter the elements.
1
Enter the elements.
2
Enter the elements.
3
Enter the elements.
4
The elements of the array are: 1 2 3 4
5.e.) Write a C program to Display array elements using calloc () &
malloc () function.
Ans.) / / malloc () function.
#include<stdio.h>
#include<stdlib.h>
int main()
{
int* ptr;
int n,i,x;
printf("Enter number of elements:\n");
scanf("%d",&n);
printf("Entered number of elements:%d\n",n);
ptr=(int*)malloc(n*sizeof(int));
if(ptr==NULL)
{
printf("Memory not allocated.\n");
exit(0);
}
else
{
printf("Memory successfully allocated using malloc.\n");
for(i=0;i<n;++i)
{
printf("Enter the elements.\n");
scanf("%d",&x);
ptr[i]=x;
}
printf("The elements of the array are: ");
for(i=0;i<n;++i)
{
printf("%d ",ptr[i]);
}
}
return 0;
}

Output: -
Enter number of elements:
5
Entered number of elements: 5
Memory successfully allocated using malloc.
Enter the elements.
5
Enter the elements.
9
Enter the elements.
4
Enter the elements.
7
Enter the elements.
6
The elements of the array are: 5 9 4 7 6
ASSIGNMENT – 6.
6.a.) Write a C program of find both the largest and smallest number
from a given array.
Ans.)
#include<stdio.h>
int main()
{
int arr[100],n,i,largest,smallest;
printf("Enter the number of elements you want to insert :\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter element %d: ",i+1);
scanf("%d",&arr[i]);
}
printf("\n");
printf("DISPLAY ALL THE ELEMENTS TAKEN BY USERS\n");
for(i=0;i<n;i++)
{
printf("%d\t",arr[i]);
}
largest=arr[0];
smallest=arr[0];
for(i=1;i<n;i++)
{
if(arr[i]>largest)
{
largest=arr[i];
}
if(arr[i]<smallest)
{
smallest=arr[i];
}
}
printf("\n");
printf("\nLargest element is : %d",largest);
printf("\nSmallest element is : %d",smallest);
return 0;
}
Output: -
Enter the number of elements you want to insert:
5
Enter element 1: 66
Enter element 2: 59
Enter element 3: 95
Enter element 4: 84
Enter element 5: 40

DISPLAY ALL THE ELEMENTS TAKEN BY USERS


66 59 95 84 40

Largest element is: 95


Smallest element is: 40
6.b.) Write a C program to perform Addition of two matrices.
Ans.)
#include<stdio.h>
int main()
{
int i,j,sum[10][10],mat1[10][10],mat2[10][10],r1,r2,c1,c2;
printf("Enter the row number of 1st Matrix: ");
scanf("%d",&r1);
printf("Enter the column number of 1st Matrix: ");
scanf("%d",&c1);
printf("\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
printf("Enter (%d,%d) no element: ",i,j);
scanf("%d",&mat1[i][j]);
}
}
printf("\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
printf("%d\t",mat1[i][j]);
}
printf("\n");
}
printf("\n");
printf("Enter the row number of 2nd Matrix: ");
scanf("%d",&r2);
printf("Enter the column number of 2nd Matrix: ");
scanf("%d",&c2);
printf("\n");
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
{
printf("Enter (%d,%d) no element: ",i,j);
scanf("%d",&mat2[i][j]);
}
}
printf("\n");
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
{
printf("%d\t",mat2[i][j]);
}
printf("\n");
}
if(r1==r2&&c1==c2)
{
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
sum[i][j]=mat1[i][j]+mat2[i][j];
}
}
printf("\nTHE SUM OF TWO MATRIX IS:\n");
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
{
printf("%d\t",sum[i][j]);
}
printf("\n");
}
}
else
{
printf("Those two matrix sum is not possible...");
}
}
Output: -
Enter the row number of 1st Matrix: 2
Enter the column number of 1st Matrix: 2

Enter (0,0) no element: 3


Enter (0,1) no element: 5
Enter (1,0) no element: 7
Enter (1,1) no element: 9

3 5
7 9

Enter the row number of 2nd Matrix: 2


Enter the column number of 2nd Matrix: 2

Enter (0,0) no element: 1


Enter (0,1) no element: 5
Enter (1,0) no element: 9
Enter (1,1) no element: 3

1 5
9 3

THE SUM OF TWO MATRIX IS:


4 10
16 12
6.c) Write a C program to display Multiplication of two matrices.
Ans.)
#include<stdio.h>
int main()
{
int mat_1[10][10],mat_2[10][10],mul[10][10];
int r1,r2,c1,c2,i,j,k;
printf("Enter the 1st matrix row number.\n");
scanf("%d",&r1);
printf("Enter the 1st matrix column number.\n");
scanf("%d",&c1);
printf("Enter the 2nd matrix row number.\n");
scanf("%d",&r2);
printf("Enter the 2nd matrix column number.\n");
scanf("%d",&c2);
printf("Input of 1st matrix\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
scanf("%d",&mat_1[i][j]);
}
}
printf("After taking Input of 1st matrix\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
printf("%d ",mat_1[i][j]);
}
printf("\n");
}
printf("Input of 2nd matrix\n");
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
{
scanf("%d",&mat_2[i][j]);
}
}
printf("After taking Input of 2nd matrix\n");
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
{
printf("%d ",mat_2[i][j]);
}
printf("\n");
}
if(c1==r2)
{
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
mul[i][j]=0;
for(k=0;k<c1;k++)
{
mul[i][j]+=mat_1[i][k]*mat_2[k][j];
}
}
}
printf("\n");
printf("After Multiplication\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c2;j++)
{
printf("%d ",mul[i][j]);
}
printf("\n");
}
}
else
{
printf("Those two matrix multiplication is not possible");
}
return 0;
}
Output: -
Enter the 1st matrix row number.
2
Enter the 1st matrix column number.
3
Enter the 2nd matrix row number.
3
Enter the 2nd matrix column number.
2
Input of 1st matrix
5
2
1
2
3
2
After taking Input of 1st matrix
5 2 1
2 3 2
Input of 2nd matrix
3
2
1
4
2
3
After taking Input of 1st matrix
3 2
1 4
2 3
After Multiplication
19 21
13 22
6.d) Write a C program to display Upper & Lower Triangular of a given
matrix.
Ans.) // UPPER TRIANGULAR MATRIX
#include<stdio.h>
#include<conio.h>
int main()
{
int mat_1[10][10],i,j,r1,c1;
printf("Enter the number of rows number: ");
scanf("%d",&r1);
printf("Enter the number of column number: ");
scanf("%d",&c1);
printf("\n");
printf("Input of 1st matrix\n");
for(i=0;i<r1;i++)
{

for(j=0;j<c1;j++)
{
scanf("%d",&mat_1[i][j]);
}
}
printf("\n");
printf("After taking input of 1st matrix\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
printf("%d ",mat_1[i][j]);
}
printf("\n");
}
printf("\n");
printf("UPPER TRIANGULAR OF THE MATRIX\n");

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

for(j=0;j<c1;j++)
{
if(i<=j)
{
printf("%d ",mat_1[i][j]);
}
else
{
printf("%d ",0);
}
}
printf("\n");
}
return 0;
}
Output: -
Enter the number of rows number: 3
Enter the number of column number: 3

Input of 1st matrix


3
2
5
6
4
1
5
2
3

After taking input of 1st matrix


3 2 5
6 4 1
5 2 3

UPPER TRIANGULAR OF THE MATRIX


3 2 5
0 4 1
0 0 3
6.d) Write a C program to display Upper & Lower Triangular of a given
matrix.
Ans.) // LOWER TRIANGULAR MATRIX
#include<stdio.h>
#include<conio.h>
int main()
{
int mat_1[10][10],i,j,r1,c1;
printf("Enter the number of rows number: ");
scanf("%d",&r1);
printf("Enter the number of column number: ");
scanf("%d",&c1);
printf("\n");
printf("Input of 1st matrix\n");
for(i=0;i<r1;i++)
{

for(j=0;j<c1;j++)
{
scanf("%d",&mat_1[i][j]);
}
}
printf("\n");
printf("After taking input of 1st matrix\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
printf("%d ",mat_1[i][j]);
}
printf("\n");
}
printf("\n");
printf("LOWER TRIANGULAR OF THE MATRIX\n");

for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
if(i>=j)
{
printf("%d ",mat_1[i][j]);
}
else
{
printf("%d ",0);
}
}
printf("\n");
}
return 0;
}
Output: -
Enter the number of rows number: 3
Enter the number of column number: 3

Input of 1st matrix


2
5
1
2
3
4
5
6
9

After taking input of 1st matrix


2 5 1
2 3 4
5 6 9

LOWER TRIANGULAR OF THE MATRIX


2 0 0
2 3 0
5 6 9
6.e) Write a C program to display diagonal elements of a given matrix.
Ans.) // DIAGONAL MATRIX
#include<stdio.h>
#include<conio.h>
int main()
{
int mat_1[10][10],i,j,r1,c1;
printf("Enter the number of rows number: ");
scanf("%d",&r1);
printf("Enter the number of column number: ");
scanf("%d",&c1);
printf("\n");
printf("Input of 1st matrix\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
scanf("%d",&mat_1[i][j]);
}
}
printf("\n");
printf("After taking the 1st matrix\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
printf("%d ",mat_1[i][j]);
}
printf("\n");
}
printf("\n");
printf("DIAGONAL TRIANGULAR OF THE MATRIX\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
if(i==j)
{
printf("%d ",mat_1[i][j]);
}
else
{
printf("%d ",0);
}
}
printf("\n");
}
return 0;
}
Output: -
Enter the number of rows number: 3
Enter the number of column number: 3

Input of 1st matrix


2
3
6
5
9
8
7
4
1

After taking the 1st matrix


2 3 6
5 9 8
7 4 1

DIAGONAL TRIANGULAR OF THE MATRIX


2 0 0
0 9 0
0 0 1
ASSIGNMENT: - 7
7.a) Write a C program to check whether a given string is palindrome or not.
Ans.)

#include<stdio.h>
#include<string.h>
int main()
{
char str[]={"MADAM"};
int l=0;
int h=strlen(str)-1;
while(h>1)
{
if(str[l++]!=str[h--])
{
printf("%s\nIs not a palindrome.\n",str);
return 0;
}
}
printf("%s\nIs a palindrome.\n",str);
return 0;
}

Output: -
MADAM
Is a palindrome.
7.b) Write a C program to count the number of Words, Digits, Vowel &
Consonant in a give text.
Ans.)
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<ctype.h>
int main()
{
char line[150];
int vowels,consonant,digit,space;
vowels=consonant=digit=space=0;
printf("Enter a line of string:\n");
fgets(line,sizeof(line),stdin);
for(int i=0;line[i]!='\0';++i)
{
line[i]=tolower(line[i]);
if(line[i]=='a'||line[i]=='e'||line[i]=='i'||line[i]=='o'||line[i]=='u')
{
++vowels;
}
else if((line[i]>='a'&&line[i]<='z'))
{
++consonant;
}
else if(line[i]>='0'&&line[i]<='9')
{
++digit;
}
else if(line[i]==' ')
{
++space;
}
}
printf("Vowels:%d",vowels);
printf("\nConsonants:%d",consonant);
printf("\nDigit:%d",digit);
printf("\nWhite space:%d",space);
return 0;
}

Output: -

Enter a line of string:


You are the shadow to my life
Vowels: 10
Consonants: 13
Digit: 0
White space: 6
7.c) Write a C program to find the length of the string using pointer.
Ans.)
#include<stdio.h>
int main()
{
char str[50], *pt;
int i=0;
printf("Pointer Example Program:Find or Calculate Length of String\n");
printf("Enter Any string[below 50 chars]:\n");
gets(str);
pt=str;
while(*pt!='\0')
{
i++;
pt++;
}
printf("Length of String:%d",i);
return 0;
}

Output: -
Pointer Example Program: Find or Calculate Length of String
Enter Any string[below 50 chars]:
I feel happy when I completed my lab assignment
Length of String: 47
7.d) Write a C program to copy one string to another using pointer.
Ans.)
#include<stdio.h>
void copy_string(char*,char*);
main()
{
char source[100],target[100];
printf("Enter source string.\n");
gets(source);
copy_string(target,source);
printf("Target string is \"%s\"\n",target);
return 0;
}
void copy_string(char *target,char *source)
{
while(*source)
{
*target=*source;
source++;
target++;
}
*target='\0';
}
Output: -
Enter source string.
Iswar Chandra Vidyasagar Polytechnic
Target string is “Iswar Chandra Vidyasagar Polytechnic”

You might also like