C Program lab Manual
C Program lab Manual
1.To read the radius of the circle and to find area and circumference.
#include<stdio.h>
void main()
int radius;
float PI=3.14,area,circumference;
clrscr();
scanf("%d",&radius);
area=PI*radius*radius;
circumference=2*PI*radius;
getch();
Output:
#include<stdio.h>
void main()
int num1,num2,num3;
clrscr();
scanf("%d %d %d",&num1,&num2,&num3);
else
getch();
Output:
#include<stdio.h>
void main()
int num,count=0,i;
clrscr();
scanf("%d",&num);
for(i=1;i<=num;i++)
if(num%i==0)
count++;
if(count==2)
else
getch();
}
Output:
#include<stdio.h>
#include<math.h>
void main()
int a,b,c,choice;
double disc,r1,r2,real,img;
clrscr();
scanf("%d%d%d",&a,&b,&c);
disc=(b*b)-(4*a*c);
if(disc>0)
choice=1;
else if(disc<0)
choice=2;
else
choice=3;
switch(choice)
case 1:
r1=(-b+sqrt(disc))/(2.0*a);
r2=(-b-sqrt(disc))/(2.0*a);
printf("\n root1=%0.2lf",r1);
printf("\n root2=%0.2lf",r2);
break;
case 2:
real=-b/(2.0*a);
img=sqrt(abs(disc))/(2.0*a);
printf("\n root1=%0.2lf+i%0.2lf",real,img);
printf("\n root2=%0.2lf-i%0.2lf",real,img);
break;
case 3:
r1=-b/(2.0*a);
r2=-b/(2.0*a);
printf("\n root1=%0.2lf",r1);
printf("\n root1=%0.2lf",r2);
break;
getch();
}
Output:
5. To read a number, find the sum of the digits, reverse the number and check it for palindrome.
#include<stdio.h>
#include<conio.h>
void main()
int num,orgnum;
int sum=0,rev=0,rem;
clrscr();
printf("Enter a number");
scanf("%d",&num);
orgnum=num;
while(num>0)
rem=num%10;
sum=sum+rem;
rev=rev*10+rem;
num=num/10;
else
getch();
Output:
6.To read the numbers from keyboard continuously till the user presses 999 and to find the sum of
only positive numbers.
#include<stdio.h>
#include<conio.h>
void main()
int num,sum=0;
clrscr();
do{
scanf("%d",&num);
sum=sum+num;
printf("\n Sum=%d",sum);
}while(num!=999);
getch();
}
Output:
7. To read percentage of marks and to display appropriate message. If a percentage is 70 and above-
Distinction, 60-69 – First Class, 50-59 – Second Class, 40-49 Pass, below 40 – Fail. (Demonstrate of if-
else ladder)
#include<stdio.h>
#include<conio.h>
void main()
float per;
clrscr();
printf("Enter percentage");
scanf("%f",&per);
if(per>=90)
printf("\n Grade=EXEMPLARY");
else if(per>=80&&per<90)
printf("\n Grade=OUTSTANDING");
else if(per>=70&&per<80)
else if(per>=60&&per<70)
else if(per>=50&&per<60)
printf("\n Grade=SECOND DIVISION");
else if(per>=35&&per<50)
else
printf("\n Grade=FAILS:RE-APPEAR");
getch();
Output:
8. To simulate a simple calculator with addition, subtraction, multiplication, division and it should
display the error message for division of zero using switch case.
#include<stdio.h>
int main()
int a,b,choice;
clrscr();
scanf("%d%d",&a,&b);
printf("Enter choice:\n1.Addition\n2.Subtraction\n3.Multiplication\n4.Division\n");
scanf("%d",&choice);
switch(choice)
case 1:printf("Result:%d",a+b);
break;
case 2:printf("Result:%d",a-b);
break;
case 3:printf("Result:%d",a*b);
break;
case 4:if(b!=0)
printf("Result=%2f",(float)a/b);
else
break;
break;
getch();
Output:
9. To read marks scored by n students and find the average of mark (Demonstration of single
dimensional array)
#include<stdio.h>
#include<conio.h>
void main()
int n,i;
float marks[10],tot_marks=0.0,avg_marks=0.0;
clrscr();
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%f",&marks[i]);
tot_marks=tot_marks+marks[i];
}
avg_marks=tot_marks/n;
getch();
Output:
10.To remove duplicate elements in a single dimensional array
#include<stdio.h>
#include<conio.h>
Void main ()
{
int a[20],n,i,j,ele;
clrscr();
printf(“\n Enter arraylimit(n):”);
scanf(“%d”,&n);
for(i=0;i<n-1;i++)
{
printf(“\n Enter a[%d]:”,i);
scanf(“%d”,&a[i]);
}
//removal of duplicate
for(i=0;i<n-1;i++)
{
ele=a[i];
for(j=j+1;j<n;j++)
{
if(ele==a[j] &&a[j]!=-111)
{
printf(“\n %d duplicate entry!!!”,a[j]);
a[j]=-111;//set -111 for duplicate entry
}
}
}
printf(“\n Final Array List \n”);
for(i=0;i<n;i++)
{
If(a[i]!=-111)
printf(“%5d”,a[i]);
}
getch ();
}
Output:
13.To demonstrate string functions (String Length, String Copy, String Concatenate, String
Comparison)
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char *str1="Bengaluru";
char *str2="Nagara";
clrscr();
printf("\n String1=%s Strin2=%s\n",str1,str2);
printf("\n 1.Length of %s=%d",str1,strlen(str1));
printf("\n 2.String copy in str2=%s",strcpy(str2,"city"));
printf("\n 3.Concatenation =%s",strcat(str1,str2));
printf("\n 4.Compare str1 & str2 %d",strcmp(str1,str2));
printf("\n 5.String in lowercase=%s",strlwr(str1));
printf("\n 6.String in Uppercase=%s",strupr(str1));
printf("\n 7.substring search=%s",strchr(str1,'N'));
printf("\n 8.Duplicate string=%s",strdup(str1));
printf("\n 9.String reverse=%s",strrev(str1));
printf("\n 10.set all characters to #=%s",strset(str1,'#'));
getch();
}
Output:
int main() {
int matrix1[10][10], matrix2[10][10];
int matrix3[10][10], i, j;
clrscr();
/* get the number of rows and columns from user */
printf("Enter the no of rows and columns(<=10):");
scanf("%d%d", &rows, &columns);
if (rows > 10 || columns > 10) {
printf("No of rows/columns is greater than 10\n");
return 0;
}
/* matrix addtion */
matrixAddition(matrix1, matrix2, matrix3);
Output:
19. To demonstrate the difference between structure and union for the following Student
name (String), Student roll no(integer), Student mark(float)
#include<stdio.h>
#include<conio.h>
struct Stud1
{
int regno;
char name[50];
float marks;
};
union Stud2
{
int regno;
char name[50];
float marks;
};
void main()
{
clrscr();
printf("\n size of stucture of Stud1=%d",sizeof(struct Stud1));
printf("\n size of stucture of Stud2=%d",sizeof(union Stud2));
getch();
}
Output: