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

CS8261-C Programming Lab Programs

The document contains C programs and outputs for: 1. Checking if a number is Armstrong number or not. 2. Storing numbers in an array and printing numbers divisible by five with their positions. 3. Finding the sum of digits of a given number using while loop. 4. Finding the roots of a quadratic equation. 5. Generating numbers between 1-100 divisible by 2 but not 3 or 5. 6. Printing the day of the week for a given number using switch case. 7. Calculating total and average of marks of N students. 8. Finding sum of weights based on certain conditions for a given set of numbers.

Uploaded by

thanammal indu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
81 views

CS8261-C Programming Lab Programs

The document contains C programs and outputs for: 1. Checking if a number is Armstrong number or not. 2. Storing numbers in an array and printing numbers divisible by five with their positions. 3. Finding the sum of digits of a given number using while loop. 4. Finding the roots of a quadratic equation. 5. Generating numbers between 1-100 divisible by 2 but not 3 or 5. 6. Printing the day of the week for a given number using switch case. 7. Calculating total and average of marks of N students. 8. Finding sum of weights based on certain conditions for a given set of numbers.

Uploaded by

thanammal indu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

AMRITA COLLEGE OF ENGINEERING AND TECHNOLOGY CS8261

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

4. Write and execute a C Program to find Enter the values of a,b,c: 5


the roots of the quadratic equation 10
5
#include<stdio.h>
#include<math.h> The roots are: Root 1= -1.000000 and Root 2= -
1.000000
int main()
{ 5. Write and execute a C program to generate
float a,b,c,d,r1,r2;
numbers between 1 and 100 which are divisible
printf("Enter the values of a,b,c: ");
by 2 and are not divisible by 3 and 5.
scanf("%f%f%f",&a,&b,&c);
d=(b*b)-(4*a*c); #include<stdio.h>
if(d>0) int main()
{ {
r1=(-b+sqrt(d))/(2*a); int i=1;
r2=(-b-sqrt(d))/(2*a); printf("The Numbers are: \n");
printf("\nThe roots are: Root 1= %f and while(i<=100)
Root 2= %f",r1,r2); {
} if (i%2 == 0 && i%3 != 0 && i%5 != 0)
printf("%d\n",i);
else if(d==0)
i++;
{
}
r1=r2=-b/(2*a); return 0;
printf("\nThe roots are: Root 1= %f and }
Root 2= %f",r1,r2);
} Output:
else
printf("\nThe roots are imaginary”); The Numbers are:
2
return 0; 4
} 8
14
AMRITA COLLEGE OF ENGINEERING AND TECHNOLOGY CS8261

16 case 5: printf(" \n Thursday");


22 break;
26 case 6: printf(" \n Friday");
28 break;
32 case 7: printf(" \n Saturday");
34 break;
38 default:
44 printf(" \n Please enter the no betwen 1 to
46 7 ");
52
56 }
58 return 0;
62 }
64
68 Output:
74
76 Enter the no :
82
8
86
Please enter the no betwen 1 to 7
88
92
94 Enter the no :
98 3
Tuesday

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

8. Given a set of numbers, write and execute a for(i=0; i<nelem; i++)


c program to find the sum of weights based on {
the following conditions. wArray[i] = 0;

 5 if it is a perfect cube. if(percube(nArray[i]))


 4 if it is a multiple of 4 and wArray[i] = wArray[i] + 5;
divisible by 6.
 3 if it is a prime number. if(nArray[i]%4==0 &&
Sort the numbers based on the weight in the nArray[i]%6==0)
increasing order wArray[i] = wArray[i] + 4;

#include <stdio.h> if(prime(nArray[i]))


#include <math.h> wArray[i] = wArray[i] + 3;
int prime(int num) }
{ //sorting both arrays
int flag=1,i; for(i=0;i<nelem;i++)
for(i=2;i<=num/2;i++) {
if(num%i==0) for(j=i+1;j<nelem;j++)
{ {
flag=0; if(wArray[i] >wArray[j])
break; {
} t = wArray[i];
return flag; wArray[i] = wArray[j];
} wArray[j] = t;
int percube(int num) t = nArray[i];
{ nArray[i] = nArray[j];
int i,flag=0; nArray[j] = t;
for(i=2;i<=num/2;i++) }
if((i*i*i)==num) }
{ }
AMRITA COLLEGE OF ENGINEERING AND TECHNOLOGY CS8261

printf("Enter decimal number: ");


printf("The sorted Array is:\n"); scanf("%d", &decnum);
for(i=0; i<nelem; i++) dectobin(decnum);
printf("<%d,%d>\n", nArray[i],wArray[i]); }
return 0; Output:
}
Output: Enter decimal number: 10

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

Min:%d is %d", max,min,max-min); Enter the Height of each person in


return 0; centimeter
} 170
Output: 165
Enter Four numbers: 100 153
50 169
75
25 Average Height of 4 persons is : 164.250000
The Difference of Max:100 and Min:25 is 75
The number of persons above average : 3

18.Write and execute a C program to swap


17. Write and execute a C program to two number using pass by value and
populate an array with height of persons and references
find how many persons are above the average
i)Program: Pass By Value
height.
#include<stdio.h>
#include <stdio.h>
void Swap(int x,int y)
int main() {
{ int t;
int i,n,sum=0,count=0,height[100]; t=x;
float avg; x=y;
printf("Enter the Number of Persons : "); y=t;
scanf("%d",&n); printf("\nThe values inside function, x=%d
printf("\nEnter the Height of each and y=%d",x,y);
person in centimeter\n"); }
for(i=0;i<n;i++) int main()
{ {
scanf("%d",&height[i]); int x,y;
sum = sum + height[i]; printf("Enter two numbers: ");
} scanf("%d%d",&x,&y);
avg = (float)sum/n; printf("\nThe values before swapping, x=%d
and y=%d",x,y);
for(i=0;i<n;i++)
Swap(x,y);
if(height[i]>avg)
printf("\n\nThe values after swapping, x=%d
count++; and y=%d ",x,y);
printf("\nAverage Height of %d return 0;
persons is : %f\n",n,avg); }
printf("\nThe number of persons Output:
above average : %d ",count);
return 0; Enter two numbers: 5
} 9
Output:
The values before swapping, x=5 and y=9
Enter the Number of Persons : 4 The values inside function, x=9 and y=5
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

Stu.No. Height Weight BMI Result Length of String1:11


Length of String2:8
1 167 70 25 Healthy Concatenating String 1 and String 2:
ProgrammingLanguage
2 155 80 33 Obese
Comparing String 1 and String 2: 1
After Copying String2 to String1: Language
3 162 40 15 Underweight
23.Write and execute a C program to sort the
given names in alphabetical order
22.Write and execute a C program for
finding String Length, String
#include<stdio.h>
Concatenation, StringComparison and
#include<string.h>
String Copy using Library function
int main()
{
#include<stdio.h>
char *name[5]=
#include<string.h>
{"sss","ddd","ttt","bbb","qqq"},*t;
int main() int i,j;
{ printf("\nThe given array of names: \n");
int c; for(i=0;i<5;i++)
char str1[100],str2[100]; printf("%s\n",name[i]);
printf("Enter String 1: "); for(i=0;i<5;i++)
gets(str1); {
printf("\nEnter String 2: "); for(j=i+1;j<5;j++)
gets(str2); {
printf("\nLength of if(strcmp(name[i],name[j]) > 0)
String1:%d",strlen(str1)); {
printf("\nLength of t = name[i];
String2:%d",strlen(str2)); name[i] = name[j];
strcat(str1,str2); name[j] = t;
printf("\nConcatenating String 1 and }
}
String 2: %s",str1);
}
c = strcmp(str1,str2);
printf("\nThe sorted array of names: \n");
printf("\nComparing String 1 and
for(i=0;i<5;i++)
String 2: %d",c); printf("%s\n",name[i]);
strcpy(str1,str2); return 0;
printf("\nAfter Copying String2 to }
String1: %s",str1); Output:
return 0;
} The given array of names:
Output: sss
ddd
Enter String 1: Programming
ttt
AMRITA COLLEGE OF ENGINEERING AND TECHNOLOGY CS8261

bbb The reversed array is:


qqq 34 45 12 3 10

The sorted array of names:


bbb
ddd 25.Given a string “a$bcd./fg” write and
qqq execute a C program to find its reverse without
sss changing the position of special characters
ttt (Example inpu t: a@gh%;j and output :
j@hg%;a)

24.Write and execute a ‘C’ program to read


#include<stdio.h>
an array of ‘N’ integers and print its elements
#include<string.h>
in reverse order.

#include<stdio.h> int isAlphabet(char x)


{
int main()
return ( (x >= 'A' && x <= 'Z') ||
{
(x >= 'a' && x <= 'z') );
int a[100],i,n,j,t; }
printf("Enter the total number of void reverseString(char s[])
elements: "); {
scanf("%d",&n); int r = strlen(s) - 1, l = 0;
printf("\nEnter %d Elements",n); char t;
for(i=0;i<n;i++) while (l < r)
scanf("%d",&a[i]); {
for(i=0,j=n-1;i<j;i++,j--) if (!isAlphabet(s[l]))
{ l++;
t = a[i]; else if(!isAlphabet(s[r]))
a[i] = a[j]; r--;
a[j] = t; else
} {
printf("\nThe reversed array is: \n"); t=s[l];
s[l]=s[r];
for(i=0;i<n;i++)
s[r]=t;
printf("%d\t",a[i]);
l++;
return 0; r--;
}
}
}
Output: }
Enter the total number of elements: 5
int main()
Enter 5 Elements10 {
3 char s[100];
12 printf("Enter string:");
45 gets(s);
34 reverseString(s);
printf("The reversed string is:%s",s);
AMRITA COLLEGE OF ENGINEERING AND TECHNOLOGY CS8261

return 0;
}
Output:
Enter string:a$f%ds^
The reversed string is:s$d%fa^

You might also like