C Lab Manual
C Lab Manual
C Lab Manual
#include<stdio.h>
#include<conio.h>
#define PI 3.142
void main()
{
int r;
double area,circum;
clrscr();
printf("\n Enter radius of a circle : ");
scanf("%d",&r);
area=PI*r*r;
circum=2*PI*r;
printf("\n Area of a circle = %0.2lf",area);
printf("\n Circumference of a circle = %0.2lf",circum);
getch();
# include<stdio.h>
# include<conio.h>
# define MAX(a,b) (a>b ? a : b)
void main()
{
int a,b,c,L1,L2;
clrscr();
printf("\n Enter three numbers(a,b,c) :
");scanf("%d %d %d",&a,&b,&c);
L1=MAX(a,b);
L2=MAX(c,L1);
printf("\n Biggest number = %d",L2);
getch();
}
3. Program to check whether the number is prime or not
#include<stdio.h>
#include<conio.h>
void main()
{
int num,count=0,i;
clrscr();
printf("\n Enter a number : ");
scanf("%d",&num);
for(i=1;i<=num;i++)
{
if(num%i==0)
count++;
}
if(count==2)
printf("\n %d is a Prime number",num);
else
printf("\n %d is not a Prime number",num);
getch();
}
# include<stdio.h>
# include<conio.h>
void main()
{
int num,orgnum;
int sum=0,rev=0,rem;
clrscr();
printf("\n 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 a Palindrome");
else
printf("\n Number is not a Palindrome");
getch();
}
5. Program to read numbers from keyboard continuously till the user presses
999 and find the sum of only positive numbers
# include<stdio.h>
# include<conio.h>
void main()
{
int num,sum=0;
clrscr();
do
{
printf("\n Enter a number : ");
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");
getch();
}
6. Program to read percentage of marks and to display appropriate message
Demonstration of (else-if) ladder
Percentage Grade
> 90% Exemplary
// 80% - 90% Outstanding
// 70% - 79% First Division with Distinction
// 60% - 69% First Division
// 50% - 59% Second Division
// 35% - 49% Pass class
// < 35% Fails : Re-appear
# include<stdio.h>
# include<conio.h>
void main()
{
float per;
clrscr();
printf("\n Enter Percentage : ");
scanf("%f",&per);
if(per > 90.0)
printf("\n Grade = EXEMPLARY");
else if(per >=80.0 && per < 90.0)
printf("\n Grade = OUTSTANDING");
else if(per >=70.0 && per < 80.0)
printf("\n Grade = FIRST DIVISION WITH DISTINCTION");
else if(per >=60.0 && per < 70.0)
printf("\n Grade = FIRST DIVISION");
else if(per >=50.0 && per < 60.0)
printf("\n Grade = SECOND DIVISION");
else if(per >=35.0 && per < 50.0)
printf("\n Grade = PASS CLASS");
else
printf("\n Grade = FAILS : RE-APPEAR");
getch();
}
7. Program to find roots of quadratic equation using Switch-Case
# include<stdio.h>
# include<conio.h>
# include<math.h>
void main()
{
int a,b,c,choice;
double
disc,root1,root2,real,img;
clrscr();
printf("\n Enter a,b,c : ");
scanf("%d %d
%d",&a,&b,&c);
disc=(b*b) - (4.0*a*c);
if(disc > 0)
choice=1;
else if(disc < 0)
choice=2;
else
choice=3;
switch(choice)
{
case 1 :
{
printf("\n Real and Distinct Roots");
root1= ( -b + sqrt(disc) ) / (2.0 * a);
root2= ( -b - sqrt(disc) ) / (2.0 * a);
printf("\n Root1 = %0.2lf",root1);
printf("\n Root2 = %0.2lf",root2);
}
break;
case 2 :
{
printf("\n Roots are complex and imaginary");
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");
root1 = -b/(2.0 * a);
root2 = -b/(2.0 * a);
printf("\n Root1 = %0.2lf",root1);
printf("\n Root2 = %0.2lf",root2);
}
break;
# include<stdio.h>
# include<conio.h>
void main()
{
int a[20],n,i,j,ele;
clrscr();
printf("\n Enter array limit(n) : ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\n Enter a[%d] : ",i);
scanf("%d",&a[i]);
}
// remove duplicate
for(i=0;i<n-1;i++)
{
ele=a[i];
for(j=i+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();
}
10. program to perform 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;
clrscr();
printf("\n Enter order of square matrix(n) : ");
scanf("%d",&n);
getch();
}
# include<stdio.h>
# include<conio.h>
void main()
{
int num,i;
long int fact=1;
clrscr();
printf("\n Enter a number : ");
scanf("%d",&num);
for(i=1;i<=num;i++)
{
fact=fact*i;
}
printf("\n Factorial of %d = %ld",num,fact);
getch();
}
12. Program to generate fibonacci series
# include<stdio.h>
# include<conio.h>
void main()
{
int n,i;
int F=0,S=1,NXT;
clrscr();
printf("\n Enter series limit(n) : ");
scanf("%d",&n);
printf("\n ******* FIBONACCI SERIES *******\n");
printf("%d \t %d",F,S);
for(i=3;i<=n;i++)
{
NXT=F+S;
printf("\t %d",NXT);
F = S;
S = NXT;
}
getch();
}
# include<stdio.h>
# include<conio.h>
void main()
{
int len=0,i=0;
char *str;
clrscr();
printf("\ Enter a string : ");
scanf("%s",str);
while(str[i]!='\0')
{
len++;
i++;
}
printf("\n Length of string = %d",len);
getch();
}
void main()
{
char *str1="Bangalore";
char *str2="University";
clrscr();
printf("\n String 1 = %s String 2 = %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,"bangalore"));
printf("\n 5. String in lower case = %s",strlwr(str1));
printf("\n 6. String in upper case = %s",strupr(str1));
printf("\n 7. Substring search = %s",strchr(str1,'L'));
printf("\n 8. Duplicate string = %s",strdup(str1));
printf("\n 9. String reverse = %s",strrev(str1));
printf("\n 10. Set all character to # = %s",strset(str1,'#'));
getch();
}
17. to read a string and to find the number of alphabets, digits, vowels,
consonants, spaces and special characters.
// C program to read a string and find the number of alphabets, digits,
// vowels, consonants, spaces 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("\n 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 Consonants = %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();
}
18. to Swap Two Numbers using Pointers
// C program to swap two numbers using pointers
# include<stdio.h>
# include<conio.h>
void main()
{
int n1,n2,*ptr1,*ptr2,temp;
clrscr();
printf("\n Enter two numbers : ");
scanf("%d %d",&n1, &n2);
ptr1=&n1;
ptr2=&n2;
//swapping
temp=*ptr1;
*ptr1=*ptr2;
*ptr2=temp;
# include<stdio.h>
# include<conio.h>
struct STUDENT
{
int regno;
char name[50];
char grade;
}BCA[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", &BCA[i].regno, BCA[i].name, &BCA[i].grade);
}
getch();
}
# include<stdio.h>
# include<conio.h>
struct EMP1
{
int empid; //2 bytes
char name[50]; // 50 bytes
float salary; // 4 bytes (Total = 2 + 50 + 4 = 56)
};
union EMP2
{
int empid; // 2 bytes
char name[50]; // 50 bytes
float salary; // 4 bytes (highest = 50 bytes)
};
void main()
{
clrscr();
printf("\n Size of Structure EMP1 = %d",sizeof(struct EMP1));
printf("\n Size of Union EMP2 = %d",sizeof(union EMP2));
getch();
}