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

C Programming - Lab-A

Lab

Uploaded by

rathikd143
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

C Programming - Lab-A

Lab

Uploaded by

rathikd143
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Dr.

Murugan K
GFGC, K R Puram

Lab Manual
Part - A
1. Program to read radius of a circle and to find area and circumference.
Program:
/* Program to find area and circumference of the circle */
#include<stdio.h>
#include<conio.h>
void main()
{
int r;
float area, cir;
clrscr();
printf("\n Enter the radius of Circle : ");
scanf("%d", &r);
area = 3.14 * r * r;
cir=2 * 3.14 * r;
printf("\n Area of Circle : %.2f", area);
printf("\n Circumference of Circle : %.2f", cir);
getch();
}
Output:
Trace
Enter the radius of Circle: 5

Area of Circle: 78.50


Circumference of Circle : 31.40
Dr. Murugan K
GFGC, K R Puram
2. Program to read three numbers and find the biggest of three numbers.
Program:
/* Program to find largest of three numbers */
#include <stdio.h>
#include<conio.h>
void main()
{
int a, b, c;
clrscr();
printf(" \n Enter the integer numbers: ");
scanf("%d %d %d", &a, &b, &c);
if (a >= b && a >= c)
printf("%d is the largest number", a);
if (b >= a && b >= c)
printf("%d is the largest number", b);
if (c >= a && c >= b)
printf("%d is the largest number", c);
getch();
}
Output:
Trace 1
Enter the integer numbers: 5 20
10
20 is the largest number
Dr. Murugan K
GFGC, K R Puram
3. Program to generate N primes.
Program:
/* Program to generate N prime numbers */
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,n;
clrscr();
printf(" \n Enter the number till which you want prime
numbers:");
scanf("%d",&n);
printf("\n Prime numbers are:\n");
for(i=2;i<=n;i++)
{
int c=0;
for(j=1;j<=i;j++)
{
if(i%j==0)
c++;
}
if(c==2)
printf ("%d \t",i);
}
getch();
}
Output:
Trace 1
Enter the number till which
you want prime numbers: 10
Dr. Murugan K
GFGC, K R Puram
Prime numbers are:
2 3 5 7

4. Program to read a number, find the sum of the digits, reverse the number and check it
for palindrome.
Program:
/* Program to find sum of the digits, reverse the number and
check it for palindrome */

#include<stdio.h>
#include<conio.h>
void main()
{
long int n,r,x,rnum=0,sum=0;
clrscr();
printf("\n Enter a Number: ");
scanf("%ld",&n);
x=n;
while(n>0)
{
r=n%10;
sum=sum+r;
rnum=rnum*10+r;
n=n/10;
}
printf("\n Sum of digits of %ld is %ld",x,sum);
printf("\n Reverse Number of %ld is %ld",x, rnum);
if(x==rnum)
printf("\n %ld is Palindrome",x);
else
printf("\n %ld is Not a Palindrome",x);
Dr. Murugan K
GFGC, K R Puram
getch();
}
Output:
Trace 1
Enter a Number: 343

Sum of digits of 343 is 10


Reverse Number of 343 is
343
343 is Palindrome
5. Program to read numbers from keyboard continuously till the user presses 999 and to
find the sum of only positive numbers.
Program:

/* Sum of only positive numbers */


#include<stdio.h>
#include<conio.h>
void main()
{
int n,s=0;
clrscr();
printf("Enter numbers to sum (press 999 to exit): \n");
while(1)
{
scanf ("%d",&n);
if(n==999)
break;
if (n>0)
s=s+n;
}
printf("\n Sum of positive numbers are : %d",s);
getch();
}
Dr. Murugan K
GFGC, K R Puram
Output:
Trace 1
Enter numbers to sum: (press 999 to
exit)
2
-3
3
0
5
-5
999

Sum of positive numbers are : 10

6. Program to read percentage of marks and to display appropriate message


(Demonstration of else-if ladder)
Program:
/* Display grade of a student using percentage */
#include<stdio.h>
#include<conio.h>
void main()
{
float p;
clrscr();
printf("\n Enter Percentage:");
scanf("%f",&p);
if(p>=80)
printf("\n A+ Grade");
else if (p>=75)
printf("\n A Grade");
else if(p>=60)
Dr. Murugan K
GFGC, K R Puram
printf("\n B Grade");
else if(p>=45)
printf("\n C Grade");
else if(p>=35)
printf("\n D Grade");
else
printf("\n Fail");
getch();
}
Output:
Trace 1
Enter Percentage:95

A+ Grade

7. Program to find the roots of quadratic equation (demonstration of switch Case


statement).
Program:
/* Program for Quadratic Equation */
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
double a,b,c,d,r1,r2;
clrscr();
printf("Enter a b c: \n");
scanf("%lf%lf%lf",&a,&b,&c);
d=b*b-4*a*c;
switch(d>0)
{
Dr. Murugan K
GFGC, K R Puram
case 1:
printf("Roots are real and unequal \n");
r1=(-b+sqrt(d))/(2*a);
r2=(-b-sqrt(d))/(2*a);
printf("Root1=%.2f \t Root2=%.2f",r1,r2);
break;
case 0:
switch(d<0)
{
case 1:
printf("Roots are Complex and different \n");
r1=-b/(2*a);
r2=sqrt(abs(d))/(2*a);
printf("Root1=%.2f+%.2fi \t Root2=%.2f-%.2fi",r1,r2,r1,r2);
break;
case 0:
printf("Roots are real and equal \n");
r1=-b/(2*a);
r2=r1;
printf("Root1=%.2f and \t Root2=%.2f",r1,r2);
}
}
getch();
}
Output:
Trace 1
Enter a , b , c:
2.3
4
5.6
Roots are Complex and different
Root1=-0.87+1.29i Root2=-0.87-1.29i
Dr. Murugan K
GFGC, K R Puram
8. Program to read marks scored by N students and find the average of marks
(Demonstration of single dimensional array)
Program:
/* Program to find the average of marks */
#include <stdio.h>
#include<conio.h>
void main()
{
int n, i;
char name[20][20], regno[50][50];
int m1[50],m2[50],m3[50],tot[50];
float avg[100];
clrscr();
printf("Enter numbers of students: ");
scanf("%d", &n);
for (i = 0; i < n; i++)
{
printf("\n Enter student %d details :", i + 1);
printf("\n Enter Name:");
scanf("%s",name[i]);
printf("Enter Register Number:");
scanf("%s", &regno[i]);
printf("Enter Mark 1 :");
scanf("%d",&m1[i]);
printf("Enter Mark 2 :");
scanf("%d",&m2[i]);
printf("Enter Mark 3 :");
scanf("%d",&m3[i]);
}
for(i=0;i<n;i++)
{
Dr. Murugan K
GFGC, K R Puram
tot[i]=m1[i]+m2[i]+m3[i];
avg[i]= (float)tot[i]/3;
}
printf("\n Name Register Number Mark 1 Mark 2 Mark 3 Total Average");
printf("\n ******************************************************");
for(i=0;i<n;i++)
printf("\n %s \t %s \t %d \t %d \t %d \t %d \t %.2f
",name[i],regno[i],m1[i],m2[i],m3[i],tot[i],avg[i]);
getch();
}
Output:
Trace 1
Enter numbers of students: 2

Enter student 1 details :


Enter Name: Charles
Enter Register Number: S10001
Enter Mark 1 :99
Enter Mark 2 :100
Enter Mark 3 :98
Enter student 2 details:
Enter Name: James
Enter Register Number: S10002
Enter Mark 1 :98
Enter Mark 2 :98
Enter Mark 3 :100

Name Register Number Mark 1 Mark 2 Mark 3 Total Average


******************************************************
Charles S10001 99 100 98 297 99.00
James S10002 98 98 100 296 98.67
Dr. Murugan K
GFGC, K R Puram
9. Program to remove Duplicate Element in a single dimensional Array.
Program:
/* Remove duplicate element in a single dimensional Array */
#include <stdio.h>
#include<conio.h>
void main()
{
int a[100], n, i, j, k;
clrscr();
printf("Enter size of the array: ");
scanf("%d", &n);
printf("Enter elements in array : ");
for(i=0; i<n; i++)
scanf("%d", &a[i]);
for(i=0; i<n; i++)
{
for(j=i+1; j<n; j++)
{
if(a[i] == a[j])
{
for(k=j; k<n - 1; k++)
{
a[k] = a[k + 1];
}
n--;
j--;
}
}
}
printf("\n Array elements after deleting duplicates : \n ");
for(i=0; i<n; i++)
printf("%d\t",a[i]);
getch();
}
Dr. Murugan K
GFGC, K R Puram
Output:
Trace 1
Enter size of the array: 5
Enter elements in array: 10 20 10 30 50

Array elements after deleting duplicates:


10 20 30 50

10. Program to perform addition and subtraction of Matrices.


Program:
/* Addition and Subtraction of Matrices */
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10][10],b[10][10],c[10][10],d[10][10];
int i,j,n,m;
clrscr();
printf("Enter the Row and Column : \n");
scanf("%d%d",&n,&m);
printf("Enter the A matrix :\n");
for(i=0;i<n;i++)
for(j=0;j<m;j++)
scanf("%d",&a[i][j]);
printf("Enter the B matrix :\n");
for(i=0;i<n;i++)
for(j=0;j<m;j++)
scanf("%d",&b[i][j]);
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
Dr. Murugan K
GFGC, K R Puram
{
c[i][j]=a[i][j]+b[i][j];
d[i][j]=a[i][j]-b[i][j];
}
}
printf("Matrix Addition:\n");
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
printf("\t %d ",c[i][j]);
}
printf("\n");
}
printf("Matrix Subtraction:\n");
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
printf("\t %d ",d[i][j]);
}
printf("\n");
}
getch();
}
Output:
Trace 1
Enter the Row and Column:
33
Enter the A matrix:
3 4 5
Dr. Murugan K
GFGC, K R Puram
6 7 8
9 10 11
Enter the B matrix:
1 2 3
4 5 6
7 8 9
Matrix Addition:
4 6 8
10 12 14
16 18 20
Matrix Subtraction:
2 2 2
2 2 2
2 2 2

You might also like