C Lab Manual A and B
C Lab Manual A and B
INDEX
SL.NO PROGRAM NAME PAGE NO
PART - A
3
PART - B
Page 1 of 32
LAB ON PROBLEM SOLVING USING C
THEJAS. A
Page 2 of 32
LAB ON PROBLEM SOLVING USING C
THEJAS. A
PART - A
Page 3 of 32
LAB ON PROBLEM SOLVING USING C
THEJAS. A
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c,large;
clrscr();
printf("enter 3 numbers\n");
scanf("%d %d %d",&a,&b,&c);
large=a;
if(b>large)
large=b;
if(c>large)
large=c;
printf("largest number:%d",large);
getch();
}
Page 4 of 32
LAB ON PROBLEM SOLVING USING C
THEJAS. A
OUTPUT :
Page 5 of 32
LAB ON PROBLEM SOLVING USING C
THEJAS. A
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i=0,fib1=0,fib2=1,fib3;
clrscr();
printf("Enter the upper limit\n");
scanf("%d",&n);
printf("Fibonacci numbers are:\n");
while(i<n)
{
if(i==0)
printf("%d\n",fib1);
else if(i==1)
{
printf("%d\n",fib2);
}
else
{
fib3=fib1+fib2;
fib1=fib2;
fib2=fib3;
printf("%d\n",fib3);
}
i++;
}
getch();
return 0;
}
Page 6 of 32
LAB ON PROBLEM SOLVING USING C
THEJAS. A
OUTPUT :
Page 7 of 32
LAB ON PROBLEM SOLVING USING C
THEJAS. A
Q3. C PROGRAM TO FIND THE TAX RATE FOR THE GROSS SALARY OF ANY
EMPLOY BASED ON THE GIVEN CONDITION ?
GROSS<200 – NO TAX
GROSS IS BETWEEN 2000 AND 4000 - 3%
GROSS IS BETWEEN 4000 AND 5000 - 5%
GROSS >=5000 – 8%
IMPLEMENT THE ABOVE PROGRAM USING SWITCH STATEMENT.
#include<stdio.h>
#include<conio.h>
void main()
{
long int gross;
int tax=0,index;
clrscr();
printf("enter the gross salary:");
scanf("%ld",&gross);
index=gross/1000;
switch(index)
{
case 0:
case 1: tax=0;
break;
case 2:
case 3: tax=gross*3/100;
break;
case 4:
case 5: tax=gross*5/100;
break;
default: tax=gross*8/100;
break;
}
printf("gross pay=%ld\n tax=%d",gross,tax);
getch();
}
Page 8 of 32
LAB ON PROBLEM SOLVING USING C
THEJAS. A
OUTPUT :
Page 9 of 32
LAB ON PROBLEM SOLVING USING C
THEJAS. A
#include<stdio.h>
#include<conio.h>
void main()
{
int n,rem,temp,rev=0,sum=0;
clrscr();
printf("enter the number:");
scanf("%d",&n);
temp=n;
while(n>0)
{
rem=n%10;
sum=sum+rem;
rev=rev*10+rem;
n=n/10;
}
printf("the reverse of%dis%d\n",temp,rev);
if(temp=rev)
printf("%d is palindrome",temp);
else
printf("%d is not palindrome",temp);
printf("\n the sum of digits is %d\n",sum);
getch();
}
Page 10 of 32
LAB ON PROBLEM SOLVING USING C
THEJAS. A
OUTPUT:
Page 11 of 32
LAB ON PROBLEM SOLVING USING C
THEJAS. A
Q5. C PROGRAM TO FIND GCD OF TWO NUMBERS. ALSO FIND THE LCM?
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,x,y,t,gcd,lcm;
clrscr();
printf("enter two integers\n");
scanf("%d%d",&x,&y);
a=x;
b=y;
while(b!=0)
{
t=b;
b=a%b;
a=t;
}
gcd=a;
lcm=(x*y)/gcd;
printf("greatest common divisor of %d and %d=%d\n",x,y,gcd);
printf("least common multiple of %d and %d=%d\n",x,y,lcm);
getch();
}
Page 12 of 32
LAB ON PROBLEM SOLVING USING C
THEJAS. A
OUT PUT:
Page 13 of 32
LAB ON PROBLEM SOLVING USING C
THEJAS. A
#include<stdio.h>
#include<conio.h>
Void main()
{
int n,i,c=0;
clrscr();
printf("enter any number n:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
if(n%i==0)
{
c++;
}
}
if(c==2)
{
printf("%d is a prime number",n);
}
else
{
printf("%d is not a prime number",n);
}
getch();
return 0;
}
Page 14 of 32
LAB ON PROBLEM SOLVING USING C
THEJAS. A
OUTPUT:
Page 15 of 32
LAB ON PROBLEM SOLVING USING C
THEJAS. A
#include<stdio.h>
#include<conio.h>
Void main()
{
int a[10],n,i;
clrscr();
printf(“enter decimal number to convert:\n”);
scanf(“%d”,&n);
for(i=0;n>0;i++)
{
a[i]=n%2;
n=n/2;
}
Printf(“\n binary equivalent to given numbers is=”);
for(i=i-1;i>=0;i--)
{
Printf(“%d”,a[i]);
}
getch();
}
Page 16 of 32
LAB ON PROBLEM SOLVING USING C
THEJAS. A
OUTPUT :
Page 17 of 32
LAB ON PROBLEM SOLVING USING C
THEJAS. A
PART-B
Page 18 of 32
LAB ON PROBLEM SOLVING USING C
THEJAS. A
#include<stdio.h>
#include<conio.h>
void main()
{
int a[20],i,sum=0,n;
float avg;
clrscr();
printf("enter the size of array:\n");
scanf("%d",&n);
printf("enter %d integer number:\n",n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++)
{
sum=sum+a[i];
}
avg=(float)sum/n;
printf("\n sum%d",sum);
printf("\n avg=%f",avg);
getch();
}
Page 19 of 32
LAB ON PROBLEM SOLVING USING C
THEJAS. A
OUTPUT:
Page 20 of 32
LAB ON PROBLEM SOLVING USING C
THEJAS. A
#include<stdio.h>
#include<conio.h>
void main()
{
int arr[10],n,i,big,small,bpos,spos;
clrscr();
printf("enter the number of elements:\n");
scanf("%d",&n);
printf("enter the elements:");
for(i=0;i<n;i++)
{
scanf("%d",& arr[i]);
}
big=arr[0];
small=arr[0];
bpos=1;
spos=1;
for(i=0;i<n;i++)
{
if(big<arr[i])
{
big=arr[i];
bpos=i+1;
}
if(small> arr[i])
{
small=arr[i];
spos=i+1;
}
}
printf("the largest number is %d is at position %d\n",big,bpos);
printf("the smallest number is %d is at position%d",small,spos);
getch();
}
Page 21 of 32
LAB ON PROBLEM SOLVING USING C
THEJAS. A
OUTPUT:
Page 22 of 32
LAB ON PROBLEM SOLVING USING C
THEJAS. A
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10][10],m,n,i,j,flag;
clrscr();
flag=0;
printf("enter rows and coloumns:\n");
scanf("%d%d",&m,&n);
printf("enter the matrix element:\n");
for(i=0;i<m;i++)
{
for(i=0;j<n;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("entered matrix is:\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("%d\t",a[i][j]);
}
printf("\n");
}
printf("transpose of the matrix is:\n");
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
printf(“%d\t”,a[j][i]);
}
printf(“\n”);
}
if(m!=n)
{
printf("given matrix is not symmetric");
getch();
exit();
}
for(i=0;i<m;i++)
{
Page 23 of 32
LAB ON PROBLEM SOLVING USING C
THEJAS. A
for(j=0;j<n;j++)
{
if(a[i][j]!=a[j][i])
{
flag=1;
break;
}
}
}
if(flag==0)
printf("\n given matrix is symmetric");
else
printf("\n given matrix is not symmetric");
getch();
}
Page 24 of 32
LAB ON PROBLEM SOLVING USING C
THEJAS. A
OUTPUT:
Page 25 of 32
LAB ON PROBLEM SOLVING USING C
THEJAS. A
#include<stdio.h>
#include<conio.h>
void main()
{
int m,n,p,q,j,i,k,sum=0;
int mat1[10][10],mat2[10][10],mat3[10][10];
clrscr();
printf("enter the number of rows and columns of first matrix \n");
scanf("%d%d",&n,&m);
printf("enter the element of first matrix \n");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&mat1[i][j]);
printf("enter the number of rows and coloumns of second matrix \n");
scanf("%d%d",&p,&q);
if(n!=p)
printf("matrices with entered order cant be multiplied with each other \n");
else
{
Printf(“enter the element of second matrix \n”);
for(i=0;i<p;i++)
for(j=0;j<q;j++)
scanf("%d",&mat2[i][j]);`
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
for(k=0;k<p;k++)
{
sum=sum+mat1[i][k]*mat2[k][i];
}
mat3[i][j]=sum;
sum=0;
}
}
printf("product of entered matrices:\n");
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
printf("%d\t",mat3[i][j]);
printf("\n");
}
Page 26 of 32
LAB ON PROBLEM SOLVING USING C
THEJAS. A
}
getch();
}
Page 27 of 32
LAB ON PROBLEM SOLVING USING C
THEJAS. A
OUTPUT:
Page 28 of 32
LAB ON PROBLEM SOLVING USING C
THEJAS. A
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char str[20];
int i,len;
int flag=0;
clrscr();
printf("enter the string:\n");
gets(str);
len=strlen(str);
for(i=0;i<len;i++)
{
if(str[i]!=str[len-i-1])
{
flag=1;
break;
}
}
if(flag)
{
printf("%s is not a palindrome",str);
}
else
{
printf("%s is a palindrome",str);
}
getch();
}
Page 29 of 32
LAB ON PROBLEM SOLVING USING C
THEJAS. A
OUTPUT:
Page 30 of 32
LAB ON PROBLEM SOLVING USING C
THEJAS. A
#include<stdio.h>
#include<conio.h>
long factorial(int n)
{
if(n==0)
return 1;
else
return(n*factorial(n-1));
}
void main()
{
int num;
long fact;
clrscr();
printf("enter a number to find factorial:\n");
scanf("%d",&num);
fact=factorial(num);
printf("factorial of %d is %ld",num,fact);
getch();
}
Page 31 of 32
LAB ON PROBLEM SOLVING USING C
THEJAS. A
OUTPUT:
Page 32 of 32
LAB ON PROBLEM SOLVING USING C