Professional Documents
Culture Documents
CS8261-Programming in C Laboratory {
int a[50],n,i;
1. Write and execute a C program to
printf("\n Enter the no of elements :");
check whether the given number is an
scanf("%d",&n);
Armstrong number or not printf("\n Enter the elements :\n");
for(i=0;i<n;i++)
#include<stdio.h>
scanf("%d",&a[i]);
#include<math.h>
printf("\n Numbers in array divisible by
int main()
5:\n");
{
for(i=0;i<n;i++)
int n,sum=0,r=0,cube=0,t;
{
printf("Enter a number:");
if(a[i]%5==0)
scanf("%d", &n);
printf("Element: %d , Positon : %d
t=n;
\n",a[i],i+1);
while(n!=0)
}
{
return 0;
r=n%10;
}
cube=pow(r,3);
sum=sum+cube; Output:
n=n/10;
} Enter the no of elements :5
if(sum==t)
printf("The given no %d is Enter the elements :
Armstrong number.",t); 5
else 11
printf("The given no %d is not an 55
Armstrong number.",t); 30
return 0; 12
}
Output: Numbers in array divisible by 5:
Enter a number:153 Element: 5 , Positon : 1
The given no 153 is armstrong number. Element: 55 , Positon : 3
Element: 30 , Positon : 4
Enter a number:120
The given no 120 is not an armstrong
number. 3 Develop and execute a ‘C’ program to
find the sum of the digits of a given
number using while statement.
2. Develop and execute a ‘C’ program to store
‘N’ numbers in an array and print the
#include<stdio.h>
numbers that are divisible by five with the
int main()
{
array location.
int n, sum = 0,rem;
#include<stdio.h> printf ("enter a n:");
int main() scanf("%d", &n);
AMRITA COLLEGE OF ENGINEERING AND TECHNOLOGY CS8261
while (n != 0)
{ Output:
rem = n % 10; Enter the values of a,b,c: 5
sum = sum + rem; 3
2
n = n / 10;
}
The roots are imaginary
printf ("The sum of digits of given no is
%d",sum); Enter the values of a,b,c: 4
return 0; 10
} 1
Output:
enter a n:251 The roots are: Root 1= -0.104356 and Root 2= -
The sum of digits of given no is 8 2.395644
6. Write and execute a program in C to display 7.Write and execute a C program to calculate
the name of the day, depending upon the and display the total and average of N student
number entered from the keyboard using marks
switch statement
#include<stdio.h>
int main()
#include <stdio.h> {
int main() int n,i,mark[50],sum=0;
{ float avg;
int i; printf("Enter the number of
printf(" \n Enter the no : \n"); students:\n");
scanf("%d",&i); scanf("%d",&n);
switch(i) printf("Enter mark of all students\n");
{ for(i=0;i<n;i++)
case 1: printf(" \n Sunday"); {
break; scanf("%d",&mark[i]);
case 2: printf(" \n Monday"); sum=sum+mark[i];
break; }
case 3: printf(" \n Tuesday"); avg=sum/n;
break; printf("The total marks is:%d\n",sum);
case 4: printf(" \n Wednesday"); printf("The average marks
break; is:%f",avg);
AMRITA COLLEGE OF ENGINEERING AND TECHNOLOGY CS8261
return 0; flag=1;
} break;
Output: }
return flag;
Enter the number of students: }
5 int main()
Enter mark of all students {
100 int n,nArray[50],wArray[50],nelem,i,j,t;
87 printf("\nEnter the number of elements in an
76 array : ");
98 scanf("%d",&nelem);
88
The total marks is:449 printf("\nEnter %d elements\n",nelem);
The average marks is:89.000000 for(i=0;i<nelem;i++)
scanf("%d",&nArray[i]);
Enter the number of elements in an array : 4 Equivalent binary value of decimal no 10:
1010
Enter 4 elements
8 10.Write and execute a C program for Matrix
24 subtraction using two dimensional arrays
11
10 #include <stdio.h>
The sorted Array is:
<10,0> int main()
<11,3> {
<24,4> int m, n, c, d, a[10][10], b[10][10],
<8,5> sub[10][10];
printf("Enter the number of rows and
9.Write and execute a C program to convert columns of matrix\n");
the given decimal number into binary number scanf("%d%d",&m,&n);
using user defined function printf("Enter the elements of first
matrix\n");
for (c=0; c<m; c++)
#include<stdio.h> for (d=0; d<n; d++)
void dectobin(int decnum) scanf("%d", &a[c][d]);
{ printf("Enter the elements of
int rem, quo; second matrix\n");
int binarynum[100], i = 1, j; for (c=0; c<m; c++)
quo = decnum; for (d= 0 ; d<n; d++)
while (quo != 0) scanf("%d", &b[c][d]);
{
binarynum[i] = quo % 2; printf("Difference of matrices are:-\n");
quo = quo / 2; for (c=0;c<m;c++)
i++; {
} for (d=0;d<n;d++)
printf("\nEquivalent binary value of {
decimal no %d: ", decnum); sub[c][d] = a[c][d] - b[c][d];
for (j = i - 1; j > 0; j--) printf("%d\t", sub[c][d]);
printf("%d", binarynum[j]); }
} printf("\n");
int main() }
{ return 0;
int decnum; }
AMRITA COLLEGE OF ENGINEERING AND TECHNOLOGY CS8261
{
Output: int decnum;
Enter the number of rows and columns of printf("Enter decimal number: ");
matrix scanf("%d", &decnum);
2 dectooctal(decnum);
3 }
Enter the elements of first matrix
6 Output:
5 Enter decimal number: 50
4
3 Equivalent octal value of decimal no 50: 62
2
1 12.Write and execute a C program to find the
Enter the elements of second matrix min and max number in an array
1
2 #include<stdio.h>
3 int main()
4 {
5 int a[50],n,i,min,max=0;
6 printf("Enter the no. of elements in the
Difference of matrices are:- array: ");
5 3 1 scanf("%d",&n);
-1 -3 -5 printf("\nEnter %d elements,\n",n);
for(i=0;i<n;i++)
11. Write and execute a C program to convert
scanf("%d",&a[i]);
the given decimal number into octal number
for(i=0;i<n;i++)
using user defined function
{
#include<stdio.h> if(a[i]>max)
void dectooctal(int decnum) max=a[i];
{ if(a[i]<min)
int rem, quo; min=a[i];
int octalnum[100], i = 1, j; }
quo = decnum; printf("\nThe maximum value is %d and
while (quo != 0) minimum value is %d",max,min);
{ }
octalnum[i] = quo % 8;
quo = quo / 8; Output:
i++; Enter the no. of elements in the array: 5
}
printf("\nEquivalent octal value of
Enter 5 elements,
decimal no %d: ", decnum);
for (j = i - 1; j > 0; j--) 11
printf("%d", octalnum[j]); 22
} 45
int main() 13
62
AMRITA COLLEGE OF ENGINEERING AND TECHNOLOGY CS8261
#include<stdio.h>
The maximum value is 62 and minimum int main()
value is 11 {
int i, j, a, n, number[30];
printf("Enter the value of N \n");
13.Write and execute a C program to convert scanf("%d", &n);
the given decimal number into hexadecimal printf("Enter the numbers \n");
number using user defined function for (i=0;i<n;++i)
scanf("%d", &number[i]);
#include<stdio.h> for (i = 0; i < n; ++i)
void dectohex(int decnum)
{
{
for (j=i+1;j<n;++j)
int quo, rem;
int i, j = 0; {
char hexanum[100]; if (number[i]>number[j])
quo = decnum; {
while (quo != 0) a = number[i];
{ number[i] = number[j];
rem = quo % 16; number[j] = a;
if (rem < 10) }
hexanum[j++] = 48 + rem; }
else }
hexanum[j++] = 55 + rem; printf("The Sorted array in ascending order
quo = quo / 16; \n");
} for (i = 0; i < n; ++i)
printf("\nEquivalent hexadecimal printf("%d\n", number[i]);
value of decimal no %d: ", decnum); }
for (i = j-1; i >= 0; i--)
printf("%c", hexanum[i]);
Output:
}
Enter the value of N
int main()
8
{
Enter the numbers
int decnum;
11
printf("Enter decimal number: ");
15
scanf("%d", &decnum); 9
dectohex(decnum); 7
} 23
Output: 19
Enter decimal number: 58 34
2
Equivalent hexadecimal value of decimal no The Sorted array in ascending order
58: 3A 2
7
14. Write and execute a C program for 9
sorting an array of N data in Ascending order 11
15
AMRITA COLLEGE OF ENGINEERING AND TECHNOLOGY CS8261
19 2
23 3
34 4
5
15. Write and execute a C program for Matrix 6
addition using two dimensional arrays Enter the elements of second matrix
1
#include <stdio.h> 2
3
int main() 4
{ 5
int m, n, c, d, a[10][10], b[10][10], 6
sum[10][10]; Sum of matrices are:-
printf("Enter the number of rows and 2 4 6
columns of matrix\n"); 8 10 12
scanf("%d%d",&m,&n);
printf("Enter the elements of first
matrix\n"); 16.Write and execute a C program that accepts
for (c=0; c<m; c++) 4 real numbers from the keyboard and prints
for (d=0; d<n; d++) out the difference of the maximum and
scanf("%d", &a[c][d]); minimum values of these numbers
printf("Enter the elements of second
matrix\n"); #include<stdio.h>
for (c=0; c<m; c++) int main()
for (d= 0 ; d<n; d++) {
scanf("%d", &b[c][d]); int min,max;
int n[4],i,j,t;
printf("Sum of matrices are:-\n"); printf("Enter Four numbers: ");
for (c=0;c<m;c++) scanf("%d %d %d %d",
{
&n[0],&n[1],&n[2],&n[3]);
for (d=0;d<n;d++)
for(i=0;i<4;i++)
{
sum[c][d] = a[c][d] + b[c][d]; {
printf("%d\t", sum[c][d]); for(j=i+1;j<4;j++)
} {
printf("\n"); if (n[i]>n[j])
} {
return 0; t=n[i];
} n[i]=n[j];
Output: n[j]=t;
Enter the number of rows and columns of }
matrix }
2 }
3 min = n[0];
Enter the elements of first matrix max = n[3];
1 printf("\nThe Difference of Max:%d and
AMRITA COLLEGE OF ENGINEERING AND TECHNOLOGY CS8261
int a[50][50],i,j,r,c,b[50][50];
The values after swapping, x=5 and y=9 printf("Enter the no. of rows and
columns: ");
scanf("%d%d",&r,&c);
ii)Program: Pass By Reference printf("\nEnter the matrix elements,\n");
for(i=0;i<r;i++)
#include<stdio.h> {
#include<conio.h> for(j=0;j<c;j++)
void Swap(int *x,int *y) scanf("%d",&a[i][j]);
{ }
int t; for(i=0;i<r;i++)
t=*x; {
*x=*y; for(j=0;j<c;j++)
*y=t; b[j][i]=a[i][j];
printf("\nThe values inside function, x=%d }
and y=%d",*x,*y);
} printf("The original matrix:\n");
int main() for(i=0;i<r;i++)
{ {
int x,y; for(j=0;j<c;j++)
printf("%d\t",a[i][j]);
printf("Enter two numbers: ");
printf("\n");
scanf("%d%d",&x,&y);
}
printf("\nThe values before swapping, x=%d
and y=%d",x,y); printf("\nThe transposed matrix is,\n");
Swap(&x,&y); for(i=0;i<c;i++)
printf("\n\nThe values after swapping,x=%d {
and y=%d ",x,y); for(j=0;j<r;j++)
return 0; printf("%d\t",b[i][j]);
} printf("\n");
}
Output: return 0;
Enter two numbers: 10 }
43 Output:
The values before swapping, x=10 and y=43 Enter the no. of rows and columns: 2
3
The values inside function, x=43 and y=10
Enter the matrix elements,
The values after swapping,x=43 and y=10 1
2
19.Write and execute a C program to 3
print the Transpose of a Matrix 4
5
#include<stdio.h> 6
int main() The original matrix:
{ 1 2 3
AMRITA COLLEGE OF ENGINEERING AND TECHNOLOGY CS8261
4 5 6 int main()
{
The transposed matrix is, int stu[100][2];
1 4 int index[100];
2 5 int i,n;
3 6 float h;
printf("Enter the number of students : ");
20.Write and execute a C Program to find
scanf("%d",&n);
for(i=0;i<n;i++)
whether a given number is a prime number
{
#include<stdio.h> printf("Enter the Height(cm) and
Weight(kg) of student %d :",i+1);
int main()
scanf("%d%d",&stu[i][0],&stu[i][1]);
{
h = (float)(stu[i][0]/100.0);
int n , i, flag =0; index[i] = stu[i][1]/(h*h);
printf("Enter a number: "); }
scanf("%d",&n); printf("\nStu.No.\tHeight\tWeight\tBMI\tRe
for(i=2;i<n;i++) sult\n");
{ for(i=0;i<n;i++)
if(n%i==0) {
{ printf("\n%d\t%d\t%d\t%d\t",i+1,stu[i][0],st
printf("\n%d is Not Prime",n); u[i][1],index[i]);
flag = 1; if(index[i]<15)
break; printf("Starvation\n");
} else if(index[i]>14 && index[i] < 18)
} printf("Underweight\n");
if(flag==0) else if(index[i] > 17 && index[i] < 26)
printf("\n%d is Prime",n); printf("Healthy\n");
else if(index[i] > 25 && index[i] < 31)
return 0;
printf("Over weight\n");
}
else if(index[i] > 30 && index[i] < 36)
Output: printf("Obese\n");
Enter a number: 45 else
printf("Severe Obese\n");
45 is Not Prime }
return 0;
Enter a number: 19 }
Output:
19 is Prime Enter the number of students : 3
Enter the Height(cm) and Weight(kg) of
student 1 :167
21. Write and execute a C program to populate
70
a two dimensional array with height and weight Enter the Height(cm) and Weight(kg) of
of persons and compute the Body Mass Index of student 2 :155
the individuals 80
Enter the Height(cm) and Weight(kg) of
#include <stdio.h>
AMRITA COLLEGE OF ENGINEERING AND TECHNOLOGY CS8261
student 3 :162
40 Enter String 2: Language
return 0;
}
Output:
Enter string:a$f%ds^
The reversed string is:s$d%fa^