Cprogram Final Lab Manual
Cprogram Final Lab Manual
Cprogram Final 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();
printf("\n Enter the radius of circle");
scanf("%d",&radius);
area=PI*radius*radius;
circumference=2*PI*radius;
printf("\n Area of a circle is:%f",area);
printf("\n Circumference of a circle is:%f",Circumference);
getch();
}
Output:
2. To read the numbers and find the biggest of three.
#include<stdio.h>
void main()
{
int num1,num2,num3;
clrscr();
printf("\n Enter any three numbers");
scanf("%d %d %d",&num1,&num2,&num3);
if(num1>=num2 && num1>=num3)
printf("\n %d is the largest number",num1);
else if(num2>=num3)
printf("\n %d is the largest number",num2);
else
printf("\n %d is the largest number",num3);
getch();
}
Output:
case 2:
{
printf("\n Roots are Complex and Distict Roots \n");
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:
{
printf("\n Roots are Equal \n");
r1=-b/(2.0*a);
r2=-b/(2.0*a);
printf("\n root1=%0.2lf",r1);
printf("\n root1=%0.2lf",r2);
}
break;
default:printf("\n invalid inputs");
}
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;
}
printf("\n Sum of digits=%d",sum);
printf("\n Reversed number=%d",rev);
if(orgnum==rev)
printf("\n Number is palindrome");
else
printf("\n Number is not a palindrome");
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{
printf("\n Enter the number:\n");
scanf("%d",&num);
if(num>0 && num!=999)
sum=sum+num;
printf("\n Sum=%d",sum);
}while(num!=999);
printf("\n you have pressed 999:STOP\n");
getch();
}
Output:
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);
/* matrix addtion */
matrixAddition(matrix1, matrix2, matrix3);
Output:
16.To read a string and to find the number of
alphabets,digits,vowels,consonants,space and special characters.
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
void main()
{
char str[100],ch;
int acount=0,dcount=0,vcount=0,ccount=0,scount=0,spcount=0,i=0;
clrscr();
printf("Enter a string");
gets(str);
while(str[i]!='\0')
{
if(isalpha(str[i])
{
acount++;
ch=tolower(str[i]);
switch(ch)
{
case 'a':
case 'e':
case 'i':
case 'o';
case 'u': vcount++;
break;
default:ccount++;
}
}
else if(isdigit(str[i]))
dcount++;
else if(isspace(str[i]))
scount++;
else
spcount++;
i=i+1;
}
printf("\n no of Alphabets=%d",acount);
printf("\n no of vowels=%d",vcount);
printf("\n no of constants=%d",ccount);
printf("\n no of spaces=%d",scount);
printf("\n no of digits=%d",dcount);
printf("\n no of special symbols=%d",spcount);
getch();
}
Output:
17.To swap two numbers using pointers
#include<stdio.h>
#include<conio.h>
void main()
{
int n1,n2,*ptr1,*ptr2,temp;
clrscr();
printf("Enter two numbers");
scanf("%d%d",&n1,&n2);
printf("\n Before swapping n1=%d n2=%d\n",n1,n2);
ptr1=&n1;
ptr2=&n2;
temp=*ptr1;
*ptr1=*ptr2;
*ptr2=temp;
printf("\n After swapping n1=%d n2=%d\n",n1,n2);
getch();
}
Output:
18.To demonstrate student structure to read & display records of n
students
#include<stdio.h>
struct student{
char name[50];
int regno;
char grade;
} S[50];
void main()
{
int n,i;
clrscr();
printf("\n Enter student count \n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\n enter %d student(regno,name,grade):",i+1);
scanf("%d %s %c",&S[i].regno,&S[i].name,&S[i].grade);
}
printf("\n Regno \t Student name \t Grade);
for(i=0;i<n;i++)
{
printf("\n %d \t %s \t %c",S[i].regno,S[i].name,S[i].grade);
}
getch();
}
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: