C Lab Programs
C Lab Programs
C Lab Programs
C Lab Programs’
1. Program to Check whether given number is Amstrong OR Not.
#include<stdio.h>
int main()
{
int a,s,i,j;
printf("Enter a:");
scanf("%d",&a);
s=0;
i=a;
while(a>0)
{
j=a%10;
s=s+(j*j*j);
a=a/10;
}
if(i==s)
{
printf("Given number is Amstrong");
}
else
{
printf("Given number is Not a Amstrong ");
}
return 0;
}
Sample Output:
Enter number:153
Given number is Amstrong.
#include<stdio.h>
int main()
{
int a,s,i,j;
printf("Enter number:");
scanf("%d",&a);
s=0;
i=a;
for(j=1;j<a;j++)
{ if(a%j==0)
2
{ s=s+j;
}
}
if(i==s)
{ printf("Given number is Perfect");
}
else
{ printf("Given number is Not a perfect");
}
return 0;
}
Sample Output:
Enter number:6
Given number is perfect.
#include<stdio.h>
int main()
{
int a,i,j;
printf("Enter number:");
scanf("%d",&a);
i=0;
while(a>0)
{ j=j%10;
i=i+1;
a=a/10;
}
printf("Number of digits in the given number is: ",i);
return 0;
}
Sample Output:
Enter number:123456789
Number of digits in the given number is: 9
#include<stdio.h>
int main()
{
int month,year;
printf("Enter Year:");
scanf("%d",&year);
3
printf("1.January\n2.February\n3.March\n4.April\n5.May\n6.June\n7.July\n8.August
\n9.September\n10.October\n11.November\n12.December\n");
printf("Enter Month Number:");
scanf("%d",&month);
if(year%4==0)
{ if(month==2)
{ printf("The February month has 29 days in Leap Year\n");
}
}
else if(month==2)
{
printf("The February month has 28 days in normal Year\n");
}
else if(month==1 || month==3 || month==5 || month==7 || month==8 || month==10 ||
month==12)
{
printf("Number of day in this month is 31\n");
}
else if(month==4 || month==6 || month==9 || month==11)
{
printf("Number of days in this month is 30\n");
}
else
{
printf("Month range is from 1 to 12\n");
}
return 0;
}
Sample Output:
Enter year:2000
1.January
2.February
3.March
4.April
5.May
6.June
7.July
8.August
9.September
10.October
11.November
12.December
Enter month number: 2
The February month has 29 days in Leap Year.
4
#include<stdio.h>
int main()
{
int n,sum,i,f,r,temp;
printf("Enter number:");
scanf("%d",&n);
sum=0;
temp=n;
while(n>0)
{ i=1;
f=1;
r=n%10;
while(i<=r)
{ f=f*i;
i++;
}
sum=sum+f;
n=n/10;
}
if(temp==sum)
{
printf("Given number is strong\n");
}
else
{
printf("Given number is Not a strong\n");
}
return 0;
}
Sample Output:
Enter number:145
Given number is strong.
#include<stdio.h>
int main()
{
int a,b,c,i,d;
printf("Enter two numbers:");
scanf("%d %d",&a,&b);
5
if(a>b)
{
d=a;
}
else
{ d=b;
}
for(i=d;i<=a*b;i++)
{ if(i%a==0 && i%b==0)
{
break;
}
}
printf("lcm is %d",i);
return 0;
}
Sample Output:
#include<stdio.h>
int main()
{
int a,b,c,i,d;
printf("Enter two numbers:");
scanf("%d %d",&a,&b);
c=1;
if(a>b)
{ d=a;
}
else
{ d=b;
}
for(i=1;i<=a && i<=b;i++)
{ if(a%i==0 && b%i==0)
{
d=c*i;
}
}
printf("hcf is %d",d);
return 0;
}
6
Sample Output:
Arrays’
8. Program to Insert an element in an given array.
#include<stdio.h>
int main()
{
int i,b,c,e,f,n,x;
printf("Enter length of array:");
scanf("%d",&n);
int a[n];
for(i=0;i<n;i++)
{
printf("Enter number:");
scanf("%d",&a[i]);
}
for(f=0;f<n;f++)
{
printf("%d ",a[f]);
}
printf("\n");
printf("Enter number to be added:");
scanf("%d",&b);
printf("Enter the position:");
scanf("%d",&c);
for(x=n-1;x>=c;x--)
{
a[x+1]=a[x];
a[x]=b;
}
for(e=0;e<=n;e++)
{
printf("%d ",a[e]);
}
return 0;
}
Sample Output:
Enter element: 2
Enter element: 3
Enter element: 4
1 2 3 4
Enter number to be added: 100
Enter the position: 2
1 2 100 3
#include<stdio.h>
int main()
{
int a,c,e,f;
printf("Enter length of array:");
scanf("%d",&a);
int b[a];
for(c=0;c<a;c++)
{ printf("Enter element:");
scanf("%d",&b[c]);
}
for(c=0;c<a;c++)
{ printf("%d\t",b[c]);
}
printf("\n");
printf("Enter position to be deleted:");
scanf("%d",&e);
for(f=e;f<a;f++)
{ b[f]=b[f+1];
}
for(c=0;c<a-1;c++)
{ printf("%d\t",b[c]);
}
printf("\n");
return 0;
}
Sample output:
10.Program to find the negetive elements in an array and count of that elements.
#include<stdio.h>
int main()
{
int n,i,c;
printf("Enter length of array:");
scanf("%d",&n);
int a[n];
for(i=0;i<n;i++)
{ printf("Enter number:");
scanf("%d",&a[i]);
}
for(i=0;i<n;i++)
{ printf("%d ",a[i]);
}
printf("\n");
c=0;
for(i=0;i<n;i++)
{ if(a[i]<0)
{ printf("The -ve number at position a[%d] is %d\n",i,a[i]);
c=c+1;
}
}
printf("number of -ve digits is %d\n",c);
return 0;
}
Sample output:
#include<stdio.h>
void main()
{
int b,c,n;
printf("Enter length of an array:");
9
scanf("%d",&n);
int a[n];
for(b=0;b<n;b++)
{ printf("Enter element:");
scanf("%d",&a[b]);
}
for(b=0;b<n;b++)
{ printf("%d ",a[b]);
}
printf("\n");
c=a[0];
for(b=0;b<n;b++)
{ a[b]=a[b+1];
}
a[n-1]=c;
printf("After left shift:");
for(b=0;b<n;b++)
{ printf("%d",a[b]);
}
return 0;
}
Sample output:
#include<stdio.h>
int main()
{
int b,c,n;
printf("Enter length of an array:");
scanf("%d",&n);
int a[n];
for(b=0;b<n;b++)
{ printf("Enter element:");
scanf("%d",&a[b]);
}
for(b=0;b<n;b++)
{ printf("%d ",a[b]);
}
printf("\n");
c=a[n-1];
10
for(b=n-2;b>-1;b--)
{ a[b+1]=a[b];
}
a[0]=c;
printf("After right shift:")
for(b=0;b<n;b++)
{ printf("%d ",a[b]);
}
return 0;
}
Sample output:
#include <stdio.h>
int main()
{
int h,k,m,b;
printf("Enter No.of Elements : ");
scanf("%d",&k);
int a[k];
for(h=0;h<k;h++)
{
printf("Enter The Element No.%d : ",h+1);
scanf("%d",&a[h]);
}
printf("Array Before Reversing : ");
for (b=0;b<=k-1;b++)
{
printf("%d",a[b]);
}
printf("\n");
printf("Array After Reversing : ");
for (b=0;b<k;b++)
{
printf("%d",a[k-1-b]);
}
printf("\n");
}
11
SAMPLE OUTPUT:
#include<stdio.h>
int main()
{
int h,k,i,j,m,z;
printf("Enter No.of Elements : ");
scanf("%d",&k);
int arr[k];
for(h=0;h<k;h++)
{
printf("Enter The Element No.%d : ",h+1);
scanf("%d",&arr[h]);
}
printf("Enter the Element To Search: ");
scanf("%d",&i);
for(j=0;j<=k-1;j++)
{
if (arr[j]==i)
{ z=1;
m=j;
break;
}
}
if (z==1)
{
printf("The Number %d Exist in an Array i.e; arr[%d]=%d.\n",i,m,i);
}
else
{
printf("The Number %d is Not Found in an Array.\n",i);
}
return 0;
}
12
SAMPLE OUTPUT:
CASE :1
CASE :2
#include <stdio.h>
int main() {
int E,x,yes,cen=0,q,k,w;
printf("Enter No.of Elements : ");
scanf("%d",&x);
int a[x];
printf("Enter Sorted Elements : ");
printf("\n");
for(k=0;k<x;k++)
{
printf("Enter The Element No.%d : ",k+1);
scanf("%d",&a[k]);
}
printf("The List is : ");
for(k=0;k<x;k++)
{
printf("%d,",a[k]);
}
printf("\n");
printf("Enter Element To Search : ");
scanf("%d",&E);
w=0;
q=x-1;
yes=0;
while (w<=q)
{ cen=(w+q)/2;
if (E<a[cen])
13
q=cen-1;
else if(E>a[cen])
w=cen+1;
else if(E==a[cen])
{printf("The Element is : %d & Present At the Index Position:- %d.\n",E,cen);
yes=1;
break;
}
}
if(yes==0)
{ printf("Element %d Not Found.\n",E);
}
return 0;
}
SAMPLE OUTPUT:
CASE:1
#include<stdio.h>
int main()
{
int s,t,v,w,z,u,no;
printf("Enter the No.of Elements in an Array :");
scanf("%d",&s);
int a[s];
for (w=0;w<s;w++)
{
printf("Enter the Element No.%d : ",w+1);
scanf("%d",&a[w]);
}
printf("The Array is : ");
for (w=0;w<s;w++)
{
printf("%d,",a[w]);
}
14
printf("\n");
for(v=0;v<s;v++)
{
u=0;
for(w=0;w<s;w++)
{
if(a[v]==a[w])
{
u++;
}
}
printf("The count of Number %d is : %d.\n",a[v],u);}
}
return 0;
}
SAMPLE OUTPUT:
2-D Arrays
17.Program to Sum of The Two Matrices :
#include<stdio.h>
int main()
{
int n,m,c,d,s,t,u,v,x;
printf("Enter number of rows:");
scanf("%d",&n);
printf("Enter number of columns:");
scanf("%d",&m);
int a[n][m];
for(c=0;c<n;c++)
{ for(d=0;d<m;d++)
{ printf("Enter element:");
scanf("%d",&a[c][d]);
}
}
15
#include<stdio.h>
int main()
{
int n,m,c,d,s,t,u,v,x;
printf("Enter number of rows:");
scanf("%d",&n);
printf("Enter umber of columns:");
scanf("%d",&m);
int a[n][m];
for(c=0;c<n;c++)
{ for(d=0;d<m;d++)
{ printf("Enter element:");
scanf("%d",&a[c][d]);
}
}
printf("First matrix is:");
for(c=0;c<n;c++)
{ for(d=0;d<m;d++)
{ printf("%d ",a[c][d]);
}
printf("\n");
}
printf("\n");
printf("Enter number of rows for second matrix:");
scanf("%d",&s);
printf("Enter number of columns for second matrix:");
scanf("%d",&t);
int z[s][t];
for(u=0;u<s;u++)
{ for(v=0;v<t;v++)
{ printf("Enter element:");
17
scanf("%d",&z[u][v]);
}
}
printf("Second matrix is:");
for(u=0;u<s;u++)
{ for(v=0;v<t;v++)
{ printf("%d ",z[u][v]);
}
printf("\n");
}
printf("\n");
SAMPLE OUTPUT:
difference of matrix is :
0 0
0 0
#include<stdio.h>
int main()
{
int n,m,c,d,s,t,u,v;
printf("Enter number of rows:");
scanf("%d",&n);
printf("Enter umber of columns:");
scanf("%d",&m);
int a[n][m];
for(c=0;c<n;c++)
{ for(d=0;d<m;d++)
{ printf("Enter element:");
scanf("%d",&a[c][d]);
}
}
printf("First matrix is:");
for(c=0;c<n;c++)
{ for(d=0;d<m;d++)
{ printf("%d ",a[c][d]);
}
printf("\n");
}
printf("\n");
printf("Enter number of rows for second matrix:");
scanf("%d",&s);
printf("Enter number of columns for second matrix:");
scanf("%d",&t);
int z[s][t];
for(u=0;u<s;u++)
{ for(v=0;v<t;v++)
{ printf("Enter element:");
scanf("%d",&z[u][v]);
}
}
printf("Second matrix is:");
for(u=0;u<s;u++)
{ for(v=0;v<t;v++)
{ printf("%d ",z[u][v]);
}
printf("\n");
}
printf("\n");
int x[10][10];
19
for(c=0;c<n;c++)
{ for(d=0;d<t;d++)
{ x[c][d]=0;
}
}
printf("After multiplication:");
for(c=0;c<n;c++)
{ for(d=0;d<t;d++)
{ for(u=0;u<s;u++)
{ x[c][d]=x[c][d]+a[c][u]*z[u][d];
}
}
}
for(c=0;c<n;c++)
{ for(d=0;d<t;d++)
{ printf("%d ",x[c][d]);
}
printf("\n");
}
printf("\n");
}
Sample output:
After multiplication:
7 10
15 22
20
#include<stdio.h>
int main()
{
int a,b,d,e;
printf("Enter length of rows:");
scanf("%d",&b);
printf("Enter number of column:");
scanf("%d",&a);
int c[b][a];
int x[10][10];
for(d=0;d<b;d++)
{ for(e=0;e<a;e++)
{ printf("Enter element:");
scanf("%d",&c[d][e]);
}
}
for(d=0;d<b;d++)
{ for(e=0;e<a;e++)
{ x[e][d]=c[d][e];
}
}
printf("\n");
printf("After transposing the matrix:");
printf("\n");
for(d=0;d<a;d++)
{ for(e=0;e<b;e++)
{ printf("%d ",x[d][e]);
}
printf("\n");
}
if(a==b)
{ for(d=0;d<a;d++)
{ for(e=0;e<b;e++)
{ if(c[d][e]==x[d][e])
{
}
else
{ goto new;
}
}
}
printf("Matric is Symmetric");
}
else
{ new:printf("Matrix is not symmetric");
}
}
21
Sample output:
#include<stdio.h>
int main()
{
int a,b,d,e;
printf("Enter length of rows:");
scanf("%d",&b);
printf("Enter lenght of column:");
scanf("%d",&a);
int c[b][a];
int x[10][10];
for(d=0;d<b;d++)
{ for(e=0;e<a;e++)
{ printf("Enter element:");
scanf("%d",&c[d][e]);
}
}
printf("Given matrix is:\n");
for(d=0;d<b;d++)
{ for(e=0;e<a;e++)
{ printf("%d ",c[d][e]);
}
printf("\n");
}
for(d=0;d<b;d++)
{ for(e=0;e<a;e++)
{ x[e][d]=c[d][e];
}
}
printf("\n");
printf("After transposing the matrix:");
22
printf("\n");
for(d=0;d<a;d++)
{ for(e=0;e<b;e++)
{ printf("%d ",x[d][e]);
}
printf("\n");
}
}
Sample Output:
#include <stdio.h>
int main()
{
int y,s,r,t,Hi,w;
printf("Enter No.of Elements : ");
scanf("%d",&t);
int a[t];
printf("Enter Those %d Elements of an Array: \n",t);
for(s=0;s<t;s++)
{
printf("Enter The Element No.%d : ",s+1);
scanf("%d",&a[s]);
}
printf("The Array Before Sorting : \n");
for(r=0;r<t;r++)
{
if (r==t-1)
{ .printf("%d .",a[r]);
}
else
{ printf("%d,",a[r]);
}
}
23
for (r=0;r<t-1;r++)
{
for (s=0;s<t-1-r;s++)
{
if (a[s]>a[s+1])
{
Hi=a[s];
a[s]=a[s+1];
a[s+1]=Hi;
}
}
}
printf("\n");
printf("The Array After Sorting : \n");
for(r=0;r<t;r++)
{
if (r==t-1)
{
.printf("%d.",a[r]);
}
else
{
printf("%d,",a[r]);
}
}
printf("\n");
return 0;
}
Sample output:
#include<stdio.h>
int main()
{
int w,g,h,x,v,min,Bye;
printf("Enter No.of Elements : ");
scanf("%d",&x);
int hi[x];
24
for(v=0;v<x;v++)
{
printf("Enter Element NO.%d : ",v+1);
scanf("%d",&hi[v]);
}
printf("\n");
printf("The Array Before Sorting is : ");
for(v=0;v<x;v++)
{
printf("%d,",hi[v]);
}
for (g=0;g<x-1;g++)
{
min=g;
for(w=g+1;w<x;w++)
{ if (hi[w]<hi[min])
{ min=w;
}
}
Bye=hi[g];
hi[g]=hi[min];
hi[min]=Bye;
}
printf("\n");
printf("The Array After Sorting is : ");
for(v=0;v<x;v++)
{
printf("%d,",hi[v]);
}
printf("\n");
}
SAMPLE OUTPUT:-
#include<stdio.h>
int main()
{
int u,v,w,x,Hi;
printf("Enter No.of Elements in an Array : ");
scanf("%d",&x);
int a[x*x];
for(w=0;w<x;w++)
{
printf("Enter The Element No.%d : ",w+1);
scanf("%d",&a[w]);
}
printf("\n");
printf("The Array Before Sorting : ");
for(w=0;w<x;w++)
{
printf("%d,",a[w]);
}
printf("\n");
for(u=1;u<=x-1;u++)//u<x
{
v=u;
while(v>0 && a[v-1] > a[v])
{
Hi = a[v];
a[v] = a[v-1];
a[v-1] = Hi;
v--;
}
}
printf("Sorted List in Ascending Order : ");
for(w=0;w<x;w++)
{
printf("%d,",a[w]);
}
printf("\n");
}
SAMPLE OUTPUT:-
Character Arrays’
25.Program To Reverse an String And Whether it is Palindrome Or Not:-
#include<stdio.h>
#include<string.h>
int main()
{
int l,e=0;
char t[1000],v[1000];
printf("Enter The String : ");
gets(t);
int c=strlen(t);
for (l=0;l<=c-1;l++)
{ v[l]=t[l];
}
printf("The Reverse of String is : ");
for (l=c-1;l>=0;l--)
{ printf("%c",v[l]);
}
for (l=0;l<=c-1;l++) //c-1 last index value of Element
{ if(t[l]==t[c-1-l])
{ e++;
}
}
if(e==c)
{ printf("\nThe Given String '%s' is Palindrome.\n",t);
}
else if(e!=c)
{ printf("\nThe Given String '%s' is NOT Palindrome.\n",t);
}
}
SAMPLE OUTPUT:-
#include<stdio.h>
#include<string.h>
int main()
{
char x[1000],y[1000],r[1000];
printf("Enter The String : ");
27
gets(x);
printf("Enter The String : ");
gets(y);
int z=strlen(x),k=strlen(y);
int i=0,j;
for(;i<=z-1;i++)
{ r[i]=x[i];
}
for(j=z;j<=z+k-1;j++)
{ r[j]=y[j-z];
}
printf("Concatenation of Strings is : %s.\n",r);
}
SAMPLE OUTPUT:-
#include<stdio.h>
#include<string.h>
int main()
{
char o[1000],p[1000];
printf("Enter The 1st String : ");
gets(o);
printf("Enter The 2nd String : ");
gets(p);
int z=strlen(o),y=strlen(p),s=0,u=0,v=0;
for(int h=0;o[h]!='\0';h++)
{ u++;
}
for(int f=0;p[f]!='\0';f++)
{ v++;
}
if(u==v)
{
for(int k=0;k<=u-1;k++)
{
if(o[k]==p[k])
{ s++;
}
}
if(s==u)
{ printf("%d\nStrings are Equal.\n",o[0]-p[0]);
}
28
else
{ printf("%d\nStrings Are Not Equal.\n",o[0]-p[0]);
}
}
else
{ printf("%d\nStrings Are Not Equal.\n",o[0]-p[0]);
}
}
SAMPLE OUTPUT:-
CASE:1
Enter The 1st String : Excuse Me.
Enter The 2nd String : Where The IIT clg is?
-18
Strings Are Not Equal.
CASE:2
Enter The 1st String : Hi
Enter The 2nd String : Hi
0
Strings are Equal.
#include<stdio.h>
#include<string.h>
int main()
{
char Hai[1000],Bye[1000];
printf("Enter the 1st String : ");
gets(Hai);
printf("Enter the 2nd String : ");
gets(Bye);
printf("The 1st String is : ");
puts(Hai);
printf("The 2nd String is : ");
puts(Bye);
strcat(Hai,Bye);
puts(Hai);
}
SAMPLE OUTPUT:
#include<stdio.h>
#include<string.h>
int main()
{
char r[1000],t[1000];
printf("Enter the 1st String : ");
gets(r);
printf("Enter the 2nd String : ");
gets(t);
int Hi=strcmp(r,t);
if (Hi==0)
{ printf("Strings Are Equal.\n");
}
else
{ printf("Strings Are Not Equal.\n");
}
printf("Strcmp() Value is : %d.\n",Hi);
}
SAMPLE OUTPUT:-
CASE:1
CASE:2
#include<stdio.h>
#include<string.h>
int main()
{
char Tom[1000],Cat[1000];
printf("Enter the 1st String : ");
gets(Tom);
printf("Enter the 2nd String : ");
gets(Cat);
strcpy(Cat,Tom);
30
puts(Tom);
printf("The Copy of String is : ");
puts(Cat);
}
SAMPLE OUTPUT:-
#include<stdio.h>
int main()
{
int x=0,b=0;
char j[1000];
printf("Enter the String : ");
gets(j);
for(b=0;j[b]!='\0';)
{
b++;
}
printf("The Length of String is : %d.\n",b);
}
SAMPLE OUTPUT:-
#include<stdio.h>
#include<string.h>
int main()
{
char p[1000],q[1000];
printf("Enter the 1st String : ");
gets(p);
printf("Enter the 2nd String : ");
gets(q);
int k=strlen(p),z=strlen(q);
printf("\nThe Length of the String %s is : %d.\nThe Length of the String %s is : %d.\n",p,k,q,z);
}
31
SAMPLE OUTPUT:-
33.Program To Change Lower Case Charcters Into Upper Case Characters And
Upper to Lower:-
#include<stdio.h>
int main()
{
int k;
char e[1000];
printf("Enter the String : ");
gets(e);
for (k=0;e[k]!='\0';k++)
{
if(e[k]>='a' && e[k]<='z')
{ e[k]=e[k]-32;
}
else if(e[k]>='A' && e[k]<='Z')
{ e[k]=e[k]+32;
}
}
printf("The New String is : %s\n",e);
}
SAMPLE OUTPUT:-
#include<stdio.h>
#include<string.h>
int main()
{
int a=0,b,count=0;
char s[100];
printf("Enter string:");
gets(s);
while(s[a]!='\0')
{ a++;
}
printf("length of string is : %d\n",a);
for(b=0;b<=a;b++)
32
{ if(s[b]==' ')
{ count++;
}
else if(s[b]=='\0')
{ count++;
}
}
printf("count of words in string is : %d\n",count);
}
Sample Output: