Lab Manual C
Lab Manual C
Experiment-1
Title: Write a C program to print “Hello World”
Source Code:
#include<stdio.h>
#include<conio.h>
int main()
{
printf("Hello World");
getch();
return 0;
}
Experiment-2
Title: Write a program to read and display integer value, floating value,
character and string.
Source Code:
#include<stdio.h>
#include<conio.h>
int main()
{
int a;
float b;
char c;
char s[10];
printf("Enter integer number:");
scanf("%d",&a);
printf("Your input integer number=%d\n",a);
printf("Enter floating number:");
scanf("%f",&b);
printf("Your input floating number=%f\n",b);
fflush(stdin);
printf("Enter a single character:");
scanf("%c",&c);
printf("Your input character=%c\n",c);
fflush(stdin);
printf("Enter any string:");
scanf("%s",&s);
printf("Your input string=%s",s);
getch();
return 0;
}
Experiment-3
Title: Write a program to take two input and find the sum, difference,
product, division and reminder.
Source Code:
#include<stdio.h>
#include<conio.h>
int main()
{
int a,b;
printf("Enter two value:");
scanf("%d%d",&a,&b);
printf("Sum=%d\n",a+b);
printf("Difference=%d\n",a-b);
printf("Product=%d\n",a*b);
printf("Quotient=%d\n",a/b);
printf("Remainder=%d\n",a%b);
getch();
return 0;
}
Experiment-4
Title: Write a program to find the Simple Interest.
Source Code:
#include<stdio.h>
#include<conio.h>
int main()
{
float p,t,r,si;
printf("Enter the value of p,t and r:");
scanf("%f%f%f",&p,&t,&r);
si=(p*t*r)/100;
printf("Simple Interest=%.2f",si);
getch();
return 0;
}
Experiment-5
Title: Write a program to find the area and circumference of circle.
Source Code:
#include<stdio.h>
#include<conio.h>
int main()
{
float r,a,c;
printf("Enter radius:");
scanf("%f",&r);
a=3.14*r*r;
c=2*3.14*r;
printf("Area=%.2f Circumference=%.2f",a,c);
getch();
return 0;
}
Experiment-6
Title: Write a program to convert the temperature given in Celsius into
equivalent Fahrenheit. [F=9/5*C+32]
Source Code:
#include<stdio.h>
#include<conio.h>
int main()
{
float c,f;
printf("Enter temperature in celsius:");
scanf("%f",&c);
f=(9/5)*c+32;
printf("Temperature in Fahrenheit=%.2f",f);
getch();
return 0;
}
Experiment-7
Title: Write a program to find the ASCII value of input character.
Source Code:
#include<stdio.h>
#include<conio.h>
int main()
{
char ch;
printf("Enter a character:");
scanf("%c",&ch);
printf("ASCII value of %c=%d",ch,ch);
getch();
return 0;
}
Experiment-8
Title: Write a program to convert kilometer into meter and centimeter.
Source Code:
#include<stdio.h>
#include<conio.h>
int main()
{
float km,m,cm;
printf("Enter the value of km:");
scanf("%f",&km);
m=km*1000;
cm=m*100;
printf("%f Meter and %f Centimeter",m,cm);
getch();
return 0;
}
Experiment-9
Title: Write a program to find area of a rectangle.
Source Code:
#include<stdio.h>
#include<conio.h>
int main()
{
int l,b,a;
printf("Enter length and breadth:");
scanf("%d%d",&l,&b);
a=l*b;
printf("Area=%d",a);
return 0;
}
Experiment-13
Title: Write a program to illustrate Assignment operators.(=)
Source Code:
#include<stdio.h>
#include<conio.h>
int main()
{
int a=10,b=30;
printf("The value of a=%d\n",a);
a=b+20;
printf("The value of a=%d\n",a);
b+=a;
printf("The value of b=%d\n",b);
getch();
return 0;
}
Experiment-14
Title: Write a program to illustrate Conditional operators. (?:)
Source Code:
#include<stdio.h>
#include<conio.h>
int main()
{
int a=10,b=30;
(a>b)?printf("%d is the largest number.",a):printf("%d is the largest
number.",b);
getch();
return 0;
}
Experiment-15
Title: Write a C Program to illustrate logical operators. (&&,||,!)
Source Code:
#include<stdio.h>
#include<conio.h>
int main()
{
int a=20,b=80,c=34;
if(a>b && a>c)
printf("%d is the largest number.\n",a);
if(a%2==0 || a%5==0)
printf("%d is divisible by 2 or 5.\n",a);
if(!(a>b))
printf("%d is smaller number.",a);
getch();
return 0;
}
Experiment-16
Title: Write a program to illustrate Bitwise operators. (&,|,^,~,>>,<<)
Source Code:
#include<stdio.h>
#include<conio.h>
int main()
{
int a=4,b=7;
printf("Bitwise AND of %d and %d=%d\n",a,b,a&b);
printf("Bitwise OR of %d and %d=%d\n",a,b,a|b);
printf("Bitwise XOR of %d and %d=%d\n",a,b,a^b);
int x=a>>2;
int y=b<<2;
printf("x=%d and y=%d",x,y);
getch();
return 0;
}
Experiment-17
Title: Write a program to illustrate the use of size of operator.
Source Code:
#include<stdio.h>
#include<conio.h>
int main()
{
int a;
printf("The size of a is %d bytes.\n",sizeof(a));
printf("The size of int is %d bytes.\n",sizeof(int));
getch();
return 0;
}
Experiment-18
Title: Write a C program to illustrate type casting.
Source Code:
#include<stdio.h>
#include<conio.h>
int main()
{
int a=10,b=3;
float x;
x=a/b; //implicit type casting
printf("The value of x=%f\n",x);
x=(float)a/(float)b; //explicit type casting
printf("The value of x=%f\n",x);
getch();
return 0;
}
Title: Write a program to find the largest number between two input
numbers.
Source Code:
#include<stdio.h>
#include<conio.h>
int main()
{
int x,y;
printf("Enter two numbers:");
scanf("%d%d",&x,&y);
if(x>y)
printf("%d is the largest number.",x);
if(y>x)
printf("%d is the largest number.",y);
getch();
return 0;
}
Experiment-20
Title: Write a program to check the given input number is odd or even.
Source Code:
#include<stdio.h>
#include<conio.h>
int main()
{ int x;
printf("Enter a number:");
scanf("%d",&x);
if(x%2==0)
printf("Even Number");
else
printf("Odd Number");
getch();
return 0;
}
Experiment-21
Title: Write a program to check the given input number is divisible by 7 or 9
or not.
Source Code:
#include<stdio.h>
#include<conio.h>
int main()
{
int x;
printf("Enter a number:");
scanf("%d",&x);
if(x%7==0 || x%9==0)
printf("Divisible by 7 or 9");
else
printf("Not divisible by 7 or not");
getch();
return 0;
}
Experiment-22
Title: Write a C program to check whether a year is leap year or not.
Source Code:
#include<stdio.h>
#include<conio.h>
int main()
{
int year;
printf("Enter a year:");
scanf("%d",&year);
if((year%400==0)||(year%4==0 && year%100!=0))
printf("Leap Year");
else
printf("Not Leap Year");
getch();
return 0;
}
Experiment-23
Title: Write a C program to check whether a character is alphabet or not.
Source Code:
#include<stdio.h>
#include<conio.h>
int main()
{
char ch;
printf("Enter a single character:");
scanf("%c",&ch);
if((ch>='a' && ch<='z')||(ch>='A' && ch<='Z'))
printf("Input character is alphabet");
else
printf("Input character is not alphabet");
getch();
return 0;
}
Experiment-24
Title: Write a program to input a mark in a subject of a student and check if
the student is pass or not. (Pass Marks=32)
Source Code:
#include<stdio.h>
#include<conio.h>
int main()
{
float marks;
printf("Enter a marks:");
scanf("%f",&marks);
if(marks>=32)
printf("Pass");
else
printf("Fail");
getch();
return 0;
}
Experiment-25
Title: Write a program to find the smallest number among three input
numbers using nested if else statement.
Source Code:
#include<stdio.h>
#include<conio.h>
int main()
{
int a,b,c;
printf("Enter three numbers:");
scanf("%d%d%d",&a,&b,&c);
if(a>b){
if(a>c)
printf("%d is the largest number",a);
else
printf("%d is the largest number",c);
}
else
{
if(b>c)
printf("%d is the largest number",b);
else
printf("%d is the largest number",c);
}
getch();
return 0;
}
Experiment-26
Title: Write a program to find the largest number among four input numbers
using ladder of if else statement.
Source Code:
#include<stdio.h>
#include<conio.h>
int main()
{
int a,b,c,d;
printf("Enter four numbers:");
scanf("%d%d%d%d",&a,&b,&c,&d);
if(a>b>c>d)
printf("%d is the largest number",a);
else if(b>a>c>d)
printf("%d is the largest number",b);
else if(c>a>b>d)
printf("%d is the largest number",c);
else
printf("%d is the largest number",c);
getch();
return 0;
}
Experiment-27
Title: Write a program to check whether a person is eligible to vote or not
based on input age using ternary operator. [Age>=18 is eligible or not]
Source Code:
#include<stdio.h>
#include<conio.h>
int main()
{
int age;
printf("Enter a age:");
scanf("%d",&age);
(age>=18)?printf("Eligible for Vote"):printf("Not eligible for vote");
getch();
return 0;
}
Experiment-28
Title: Write a program to check the smallest number among three input
numbers using ternary operator.
Source Code:
#include<stdio.h>
#include<conio.h>
int main()
{
int a,b,c,small;
printf("Enter three numbers:");
scanf("%d%d%d",&a,&b,&c);
small=(a<b)?((a<c)?a:c):((b<c)?b:c);
printf("%d is the smallest number",small);
getch();
return 0;
}
Experiment-29
Title: Write a program to calculate sum, difference, product, division and
reminder using switch case statement.
Source Code:
#include<stdio.h>
#include<conio.h>
int main()
{
int a,b;
char ch;
printf("Enter two values:");
scanf("%d%d",&a,&b);
printf("+ for addition\n");
printf("- for difference\n");
printf("* for product\n");
printf("/ for division\n");
fflush(stdin);
printf("Enter your choice:");
scanf("%c",&ch);
switch(ch)
{
case '+':
printf("Sum=%d",a+b);
break;
case '-':
printf("Difference=%d",a-b);
break;
case '*':
printf("Product=%d",a*b);
break;
case '/':
printf("Division=%d",a/b);
break;
default:
printf("Wrong Choice");
}
getch();
return 0;
}
Title: Write a C program to check input number is odd or even using switch
case statement.
Source Code:
#include<stdio.h>
#include<conio.h>
int main()
{
int a;
printf("Enter a number:");
scanf("%d",&a);
switch(a%2==0)
{
case 1:
printf("Even Number");
break;
case 0:
printf("Odd Number");
}
getch();
return 0;
}
Experiment-32
Title: WAP to print odd numbers from 0 to 100 using for loop.
Source Code:
#include<stdio.h>
#include<conio.h>
int main()
{
int i;
for(i=1;i<=100;i++)
{
if(i%2==1)
printf("%d\t",i);
}
getch();
return 0;
}
Experiment-33
Title: WAP to find sum of 10 natural numbers using for loop.
Source Code:
#include<stdio.h>
#include<conio.h>
int main()
{
int i,sum=0;
for(i=1;i<=10;i++)
{
sum+=i;
}
printf("Sum=%d",sum);
getch();
return 0;
}
Experiment-34
Title: WAP to find sum of even numbers from 0 to 100.
Source Code:
#include<stdio.h>
#include<conio.h>
int main()
{
int i,sum=0;
for(i=0;i<=100;i++)
{
if(i%2==0)
sum+=i;
}
printf("Sum=%d",sum);
getch();
return 0;
}
Experiment-35
Title: WAP to check the given input number is prime or not.
Source Code:
#include<stdio.h>
#include<conio.h>
int main()
{
int i,count=0,n;
printf("Enter a number:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
if(n%i==0)
count++;
}
if(count==2)
printf("%d is prime number.",n);
else
printf("%d is not prime number.",n);
getch();
return 0;
}
Experiment-36
Title: WAP to find the factorial of given input number.
Source Code:
#include<stdio.h>
#include<conio.h>
int main()
{
int i,n,fact=1;
printf("Enter a number:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
fact*=i;
}
printf("Factorial=%d",fact);
getch();
return 0;
}
Experiment-37
Title: WAP to display the Fibonacci series using for loop.
Source Code:
#include<stdio.h>
#include<conio.h>
int main()
{
int n,a=0,b=1,c,i;
printf("Enter number of terms:");
scanf("%d",&n);
for(i=1;i<=n;i++){
printf("%d\t",a);
c=a+b;
a=b;
b=c;
}
getch();
return 0;
}
Experiment-38
Title: WAP to find sum of the cubes of first ten numbers.
Source Code:
#include<stdio.h>
#include<conio.h>
int main()
{
int i,sum=0;
for(i=1;i<=10;i++)
{
sum=sum+(i*i*i);
}
printf("Total Sum=%d",sum);
getch();
return 0;
}
Experiment-39
Title: Write a program to display the multiplication table of all the numbers
from 1 to 5 using nested for loop.
Source Code:
#include<stdio.h>
#include<conio.h>
int main()
{
int i,j;
for(i=1;i<=5;i++)
{
printf("Multiplication Table of %d:\n",i);
for(j=1;j<=10;j++)
{
printf("%d * %d = %d\n",i,j,i*j);
}
}
getch();
return 0;
}
Experiment-40
Title: Write a program to print all the prime numbers from 0 to 100 using
nested for loop.
Source Code:
#include<stdio.h>
#include<conio.h>
int main()
{
int i,j,count;
for(i=0;i<=100;i++)
{
count=0;
for(j=1;j<=i;j++)
{
if(i%j==0)
count++;
}
if(count==2)
printf("%d\t",i);
}
getch();
return 0;
}
While Loop:
Experiment-41
Title: WAP to display all the numbers from 1 to 10.
Source Code:
#include<stdio.h>
#include<conio.h>
int main()
{ int i=1;
while(i<=10)
{
printf("%d\n",i);
i++;
}
getch();
return 0;
}
Experiment-42
Title: WAP to find the sum of even numbers from 0 to 100 using while loop.
Source Code:
#include<stdio.h>
#include<conio.h>
int main()
{
int i=0,sum=0;
while(i<=100)
{
if(i%2==0)
sum+=i;
i++;
}
printf("Total Sum=%d",sum);
getch();
return 0;
}
Experiment-43
Title: WAP to count the total number of digits in the input number.
Source Code:
#include<stdio.h>
#include<conio.h>
int main()
{
int n,count=0;
printf("Enter a number:");
scanf("%d",&n);
while(n>0)
{
n=n/10;
count++;
}
printf("Total Number of digits=%d",count);
getch();
return 0;
}
Experiment-44
Title: WAP to find the reverse of given input number.
Source Code:
#include<stdio.h>
#include<conio.h>
int main()
{
int n,rev=0,r;
printf("Enter a number:");
scanf("%d",&n);
while(n>0)
{
r=n%10;
rev=rev*10+r;
n=n/10;
}
printf("Reverse=%d",rev);
getch();
return 0;
}
Experiment-45
Title: WAP to check the given input number is palindrome or not.
Source Code:
#include<stdio.h>
#include<conio.h>
int main()
{
int n,rev=0,r;
printf("Enter a number:");
scanf("%d",&n);
int x=n;
while(n>0)
{
r=n%10;
rev=rev*10+r;
n=n/10;
}
if(rev==x)
printf("Palindrome Number");
else
printf("Not Palindrome Number");
getch();
return 0;
}
Experiment-46
Title: WAP to find the sum of individual digits from the given input numbers.
Source Code:
#include<stdio.h>
#include<conio.h>
int main()
{
int n,sum=0,r;
printf("Enter a number:");
scanf("%d",&n);
while(n>0)
{
r=n%10;
sum=sum+r;
n=n/10;
}
printf("Total Sum=%d",sum);
getch();
return 0;
}
Experiment-47
Title: WAP to check whether the given input number is Armstrong or not.
Source Code:
#include<stdio.h>
#include<conio.h>
int main()
{
int n,sum=0,r;
printf("Enter a number:");
scanf("%d",&n);
int x=n;
while(n>0)
{
r=n%10;
sum=sum+(r*r*r);
n=n/10;
}
if(sum==x)
printf("Armstrong Number");
else
printf("Not Armstrong Number");
getch();
return 0;
}
Experiment-48
Title: Write a C program to print “C programming” 5 times using do while
loop.
Source Code:
#include<stdio.h>
#include<conio.h>
int main()
{
int i=1;
do{
printf("C Programming\n");
i++;
}while(i<=5);
getch();
return 0;
}
Experiment-49
Title: WAP to find the sum of all the numbers from 1 to 10.
Source Code:
#include<stdio.h>
#include<conio.h>
int main()
{
int i=1;
do{
printf("%d\n",i);
i++;
}while(i<=10);
getch();
return 0;
}
Experiment-50
Title: WAP to find the reverse of given input number using do while loop.
Source Code:
#include<stdio.h>
#include<conio.h>
int main()
{
int n,r,rev=0;
printf("Enter a number:");
scanf("%d",&n);
do{
r=n%10;
rev=rev*10+r;
n=n/10;
}while(n>0);
printf("Reverse Number=%d",rev);
getch();
return 0;
}
Experiment-51
Title: WAP to illustrate the use of break statement.
Source Code:
#include<stdio.h>
#include<conio.h>
int main()
{
int i;
for(i=1;i<=10;i++)
{
if(i==5)
break;
printf("%d\n",i);
}
getch();
return 0;
}
Title: WAP to illustrate the use of continue statement.
Source Code:
#include<stdio.h>
#include<conio.h>
int main()
{
int i;
for(i=1;i<=10;i++)
{
if(i==5)
continue;
printf("%d\n",i);
}
getch();
return 0;
}
Experiment-53
Title: Write a program to find sum and average of an array elements.
Source Code:
#include<stdio.h>
#include<conio.h>
int main()
{
int n,i,average,sum=0;
printf("Enter the total number of elements:");
scanf("%d",&n);
int a[n];
printf("Enter elements:");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
sum=sum+a[i];
}
average=sum/n;
printf("Total sum=%d and Average=%d",sum,average);
getch();
return 0;
}
Experiment-54
Title: Write a program to count the total number of employee whose is getting
salary between 60000 to 80000 using array.
Source Code:
#include<stdio.h>
#include<conio.h>
int main()
{
int salary[5],i,count=0;
printf("Enter the salary of five employee: ");
for(i=0;i<5;i++)
{
scanf("%d",&salary[i]);
}
for(i=0;i<5;i++)
{
if(salary[i]>=60000 && salary[i]<=80000)
count ++;
}
printf("Total number of employee getting salary from 60000 to 80000 is
%d.",count);
getch();
return 0;
}
Experiment-55
Title: Write a program to find the largest element from the array list.
Source Code:
#include<stdio.h>
#include<conio.h>
int main()
{
int num[100],i,large,n;
printf("Enter the number of elements:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter %d number: ",i+1);
scanf("%d",&num[i]);
}
large=num[0];
for(i=0;i<n;i++)
{
if(large<num[i])
{
large=num[i];
}
}
printf("Largest Element=%d",large);
getch();
return 0;
}
Experiment:
Title: Write a program to find the smallest element from the array list.
Source Code:
#include<stdio.h>
#include<conio.h>
int main()
{
int num[100],i,small,n;
printf("Enter the number of elements:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter %d number: ",i+1);
scanf("%d",&num[i]);
}
small=num[0];
for(i=0;i<n;i++)
{
if(small>num[i])
{
small=num[i];
}
}
printf("Smallest Element=%d",small);
getch();
return 0;
}
Experiment-57
Title: Write a program to check the searching element is present or not in an
array list.
Source Code:
#include<stdio.h>
#include<conio.h>
int main()
{
int a[5],i,n,loc=0;
printf("Enter array elements: ");
for(i=0;i<5;i++)
{
scanf("%d",&a[i]);
}
printf("Enter your element to be searched: ");
scanf("%d",&n);
for(i=0;i<5;i++)
{
if(n==a[i])
{
loc=i+1;
break;
}
}
if(loc>0)
{
printf("%d is present at position %d.",n,loc);
}
else
{
printf("%d is not present in your array list.",n);
}
getch();
return 0;
}
Experiment-58
Title: Write a program to read 10 elements from the user and sort them in
ascending order using array.
Source Code:
#include<stdio.h>
#include<conio.h>
#define size 10
int main()
{
int a[size];
int i,j,temp;
printf("Enter 10 elements of an array: ");
for(i=0;i<size;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<size;i++)
{
for(j=i+1;j<size;j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
printf("Elements in ascending order:\n");
for(i=0;i<size;i++)
{
printf("%d\n",a[i]);
}
getch();
return 0;
}
Experiment-59
Title: Write a program to read 10 elements from the user and sort them in
descending order using array.
Source Code:
#include<stdio.h>
#include<conio.h>
#define size 10
int main()
{
int a[size];
int i,j,temp;
printf("Enter 10 elements of an array: ");
for(i=0;i<size;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<size;i++)
{
for(j=i+1;j<size;j++)
{
if(a[i]<a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
printf("Elements in descending order:\n");
for(i=0;i<size;i++)
{
printf("%d\n",a[i]);
}
getch();
return 0;
}
Experiment-60
Title: Write a program to read the elements of 3*3 matrix from the user and
display them in proper order.
Source Code:
#include<stdio.h>
#include<conio.h>
int main()
{
int a[3][3];
int i,j;
printf("Enter the elements of Matrix for(3*3):\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("Your input matrix is:\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d\t",a[i][j]);
}
printf("\n");
}
getch();
return 0;
}
Experiment-61
Title: Write a program to find the transpose of given input matrix.
Source Code:
#include<stdio.h>
#include<conio.h>
int main()
{
int a[2][3],b[3][2];
int i,j;
printf("Enter the elements of Matrix for(2*3):\n");
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("Your entered matrix is:\n");
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
{
printf("%d\t",a[i][j]);
}
printf("\n");
}
printf("Transposed matrix is:\n");
for(i=0;i<3;i++)
{
for(j=0;j<2;j++)
{
b[i][j]=a[j][i];
printf("%d\t",b[i][j]);
}
printf("\n");
}
getch();
return 0;
Experiment-62
Title: Write a program to find the sum of two input matrix.
Source Code:
#include<stdio.h>
#include<conio.h>
int main()
{
int a[2][2],b[2][2],sum[2][2];
int i,j;
printf("Enter the elements of First Matrix for(2*2):\n");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("Enter the elements of Second Matrix for(2*2):\n");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
scanf("%d",&b[i][j]);
}
}
printf("Your first matrix is:\n");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
printf("%d\t",a[i][j]);
}
printf("\n");
}
printf("Your second matrix is:\n");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
printf("%d\t",b[i][j]);
}
printf("\n");
}
printf("Sum of matrix:\n");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
sum[i][j]=a[i][j]+b[i][j];
printf("%d\t",sum[i][j]);
}
printf("\n");
}
getch();
return 0;
}
Experiment-63
Title: Write a program to display the identity matrix.
Source Code:
#include<stdio.h>
#include<conio.h>
int main()
{
int a[3][3],i,j;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
if(i==j)
printf("1\t");
else
printf("0\t");
}
printf("\n");
}
getch();
return 0;
}
Experiment-64
Title: Write a program to check the given input matrix is identity or not.
Source Code:
#include<stdio.h>
#include<conio.h>
int main()
{
int a[2][2]={1,0,0,1},f=0,i,j;
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
if(i==j && a[i][j]!=1)
{
f=1;
break;
}
if(i!=j && a[i][j]!=0)
{
f=1;
break;
}
}
}
if(f==0)
printf("Identity Matrix");
else
printf("Not identity matrix");
getch();
return 0;
}
Experiment-65
Title: Write a program to check the given input matrix is identical matrix or
not.
Source Code:
#include<stdio.h>
#include<conio.h>
int main()
{
int a[2][3]={1,2,3,4,5,6},b[2][3]={1,2,3,4,5,6},i,j,flag=0;
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
{
if(a[i][j]!=b[i][j])
flag=1;
break;
}
}
if(flag==0)
printf("Identical Matrix");
else
printf("Not identical matrix");
getch();
return 0;
}
Experiment-66
Title: Write a program to find the multiplication of two input matrix.
Source Code:
#include<stdio.h>
#include<conio.h>
int main()
{
int a[2][2],b[2][2],mul[2][2];
int i,j,k;
printf("Enter the elements of First Matrix for(2*2):\n");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("Enter the elements of Second Matrix for(2*2):\n");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
scanf("%d",&b[i][j]);
}
}
printf("Your first matrix is:\n");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
printf("%d\t",a[i][j]);
}
printf("\n");
}
printf("Your second matrix is:\n");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
printf("%d\t",b[i][j]);
}
printf("\n");
}
//Multiplication of matrix.
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
mul[i][j]=0;
for(k=0;k<2;k++)
{
mul[i][j]=mul[i][j]+a[i][k]*b[k][j];
}
}
}
printf("Your Resultant Matrix is:\n");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
printf("%d\t",mul[i][j]);
}
printf("\n");
}
getch();
return 0;
}
Experiment-67
Title: Write a program to count the total number of characters in the given
input string.
Source Code:
#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
{
char str[100];
int len=0,i=0;
printf("Enter the string: ");
gets(str);
while(str[i]!='\0')
{
len++;
i++;
}
printf("Total number of character is %d",len);
getch();
return 0;
}
Experiment-68
Title: Write a program to find the reverse of input string.
Source Code:
#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
{
char str[100];
int len,i;
printf("Enter the string: ");
gets(str);
len=strlen(str);
for(i=len;i>=0;i--)
{
printf("%c",str[i]);
}
getch();
return 0;
}
Experiment-69
Title: Write a Program to check the given string is palindrome or not.
Source Code:
#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
{
char str[100];
int len,i;
printf("Enter the string: ");
gets(str);
strlwr(str);
len=strlen(str);
for(i=0;i<len/2;i++){
if(str[i]!=str[len-1-i]){
printf("Not palindrome");
break;
}
}
if(i==len/2){
printf("Palindrome");
}
getch();
return 0;
}
Experiment-70
Title: Write a program to count the total number of vowels containing in the
string.
Source Code:
#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
{
char str[100];
int len,i,count=0;
printf("Enter the string: ");
gets(str);
len=strlen(str);
for(i=0;i<len;i++)
{
if(str[i]=='a' || str[i]=='e' || str[i]=='i' || str[i]=='o' || str[i]=='u'
|| str[i]=='A' || str[i]=='E' || str[i]=='I' || str[i]=='O' || str[i]=='U')
count++;
}
printf("Total number of vowels=%d",count);
getch();
return 0;
}
Experiment-71
Title: Write a program to count the total number of words containing in the
string.
Source Code:
#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
{
char str[100];
int len,i,count=0;
printf("Enter the string: ");
gets(str);
len=strlen(str);
for(i=0;i<len;i++)
{
if(str[i]==' ')
count++;
}
printf("Total number of words=%d",count+1);
getch();
return 0;
}
Experiment-72
Title: Write a program to count the total number of lowercase alphabets,
uppercase alphabets, digits, white space and other symbol in the given input
string.
Source Code:
#include<stdio.h>
#include<conio.h>
int main()
{
int lower=0,upper=0,digit=0,white_space=0,other=0;
char ch[100];
printf("Enter a string:");
gets(ch);
int i;
for(i=0;ch[i]!='\0';i++)
{
if(ch[i]>='a' && ch[i]<='z')
lower++;
else if(ch[i]>='A' && ch[i]<='Z')
upper++;
else if(ch[i]>='0' && ch[i]<='9')
digit++;
else if(ch[i]==' ')
white_space++;
else
other++;
}
printf("Lowercase=%d\nUppercase=%d\nDigits=%d\nWhite_Space=%d\nO
ther=%d",lower,upper,digit,white_space,other);
getch();
return 0;
}
void main()
int n;
scanf("%d",&n);
fibo(n);
}
int a=0;
int b=1;
printf("Fibonacci series for %d terms:-\n",n);
for(i=0;i<n;i++) {
printf("%d ",c);
a=b;
b=c;
c=a+b;
}
}
Experiment-78
Title: WAP to find the total sum of an array elements using function.
Source Code:
#include<stdio.h>
#include<conio.h>
int check(int a[]);
void main()
{
int a[5]={5,10,15,20,25},i,sum=0;
check(a);
for(i=0;i<5;i++)
{
sum=sum+a[i];
}
printf("Total sum=%d",sum);
getch();
}
int check(int x[])
{
int i;
for(i=0;i<5;i++)
{
return x[i];
}
}
Experiment-79
Title: WAP to find the length of string using your own function.
Source Code:
#include<stdio.h>
#include<conio.h>
int string(char str[]);
void main()
{
char str[50];
int l;
printf("Enter the string: ");
gets(str);
l=string(str);
printf("Length of string=%d",l);
getch();
}
int string(char str[])
{
int i,l=0;
for(i=0;str[i]!='\0';i++)
{
l++;
}
return l;
}
Structure
Experiment-80
Title: Write a C program to using structure that reads the record of 40
students with member’s roll-number, name, address and marks and display
the records of students who have obtained greater than 250 marks.
Source Code:
#include<stdio.h>
#include<conio.h>
struct students{
int rollno;
char name[20];
char address[30];
float marks;
}s[40];
int main()
{
int i;
for(i=0;i<40;i++)
{
printf("Enter name, rollnumber, address and marks:");
scanf("%s%d%s%f",s[i].name,&s[i].rollno,s[i].address,&s[i].marks);
}
printf("Records of students whose marks is greater than 250:\n");
for(i=0;i<40;i++)
{
if(s[i].marks>250)
printf("Name=%s\nRollnumber=%d\nAddress=%s\nMarks=%.2f\n",s[i].na
me,s[i].rollno,s[i].address,s[i].marks);
}
getch();
return 0;
}
Experiment-81
Title: Write a program to read employee id, name, post and salary of 100
employee using structure and display the detail of those employee whose post
is “Manager”.
Source Code:
#include<stdio.h>
#include<conio.h>
#include<string.h>
struct emp{
int id;
char name[20];
char post[30];
float salary;
}s[100];
int main()
{
int i;
for(i=0;i<100;i++)
{
printf("Enter id, name, post and salary:");
scanf("%d%s%s%f",&s[i].id,s[i].name,s[i].post,&s[i].salary);
}
printf("Records of employee whose post is Manager:\n");
for(i=0;i<100;i++)
{
if(strcmp(s[i].post,"Manager")==0)
printf("Id=%d\nName=%s\nPost=%s\nSalary=%.2f\n",s[i].id,s[i].name,s[i].
post,s[i].salary);
}
getch();
return 0;
}
Experiment-82
Title: Define a structure of named employee having data members name,
address, age and salary. Take the data for n employees in an array and find
the average salary.
Source Code:
#include<stdio.h>
#include<conio.h>
struct employee{
char name[20];
char address[30];
int age;
float salary;
};
int main()
{
int i,n;
float total=0.0,average;
printf("Enter total number of employees:");
scanf("%d",&n);
struct employee s[n];
for(i=0;i<n;i++)
{
printf("Enter name, address, age and salary:");
scanf("%s%s%d%f",s[i].name,s[i].address,&s[i].age,&s[i].salary);
total+=s[i].salary;
}
average=total/n;
printf("Average salary=%.2f",average);
getch();
return 0;
}
Experiment-83
Title: Write a program to read 10 students record with fields (roll-no, name,
class and marks in 5 subjects) and display their records along with their
percentage of marks obtained.
Source Code:
#include<stdio.h>
#include<conio.h>
struct students
{
int rollno;
char name[20];
int cls;
float marks[5];
float total;
float percentage;
};
int main()
{
struct students s[10];
int i,j;
for(i=0;i<10;i++)
{
printf("Enter rollno,name and class:");
scanf("%d%s%d",&s[i].rollno,s[i].name,&s[i].cls);
s[i].total=0;
for(j=0;j<5;j++)
{
printf("Enter marks:");
scanf("%f",&s[i].marks[j]);
s[i].total+=s[i].marks[j];
}
s[i].percentage=(s[i].total/2.0);
}
printf("Records of students:\n");
for(i=0;i<10;i++)
{
printf("Rollnumber=%d\nName=%s\nClass=%d\nPercentage=%.2f",s[i].roll
no,s[i].name,s[i].cls,s[i].percentage);
}
getch();
return 0;
}
Experiment-84
Title: Write a program using structure to read name, roll and marks in three
subjects of n student asking to user and print the record in the ascending
order of the total marks obtained in three subjects (sorting records total
marks wise and display in ascending order).
Source Code:
#include<stdio.h>
#include<conio.h>
#define N 100
int main()
{
struct student
{
char name[20];
int roll_no;
int marks[3];
};
struct student s[N],temp;
int n;
int i,j,k,total1,total2;
printf("Enter total number of students:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter name:");
scanf("%s",s[i].name);
printf("Enter roll number:");
scanf("%d",&s[i].roll_no);
printf("Enter the marks:");
for(j=0;j<3;j++)
{
scanf("%d",&s[i].marks[j]);
}
}
for(i=0;i<n-1;i++)
{
for(j=0;j<n-1-i;j++)
{
total1=total2=0;
for(k=0;k<3;k++)
{
total1+=s[j].marks[k];
total2+=s[j+1].marks[k];
}
if(total1>total2)
{
temp=s[j];
s[j]=s[j+1];
s[j+1]=temp;
}
}
}
printf("Sorted list is:\n");
for(i=0;i<n;i++)
{
printf("Name=%s\n",s[i].name);
printf("Roll number=%d\n",s[i].roll_no);
printf("Marks in three subject:\n");
for(j=0;j<3;j++)
{
printf("%d\n",s[i].marks[j]);
}
}
getch();
return 0;
}
Experiment-85
Title: Write a program to read the name, address and salary of 5 employees
using array of structure. Display information of each employee in ascending
order of their name.
Source Code:
#include<stdio.h>
#include<conio.h>
#include<string.h>
#define n 5
struct employee
{
char name[20];
char address[50];
float salary;
};
int main()
{
struct employee e[n];
struct employee temp;
int i;
float temp1;
int j;
for(i=0;i<n;i++)
{
printf("Enter name and address:");
scanf("%s%s",e[i].name,e[i].address);
printf("Enter salary:");
scanf("%f",&temp1);
e[i].salary=temp1;
}
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(strcmp(e[i].name,e[j].name)>0)
{
temp=e[i];
e[i]=e[j];
e[j]=temp;
}
}
}
printf("Employee details in ascending order of name:\n");
printf("Emp_Name\tEmp_Address\tEmp_Salary\n");
for(i=0;i<n;i++)
{
printf("%s\t%s\t%.2f\n",e[i].name,e[i].address,e[i].salary);
}
getch();
return 0;
}
Pointers
Experiment-86
Title: Write a program to illustrate the use of pointer in the program.
Source Code:
#include<stdio.h>
#include<conio.h>
int main()
{
int a=90;
int *p;
p=&a;
printf("The value of a=%d",*p);
getch();
return 0;
}
Experiment-87
Title: Write a program to illustrate the use of chain of pointer and pointer
arithmetic.
Source Code:
#include<stdio.h>
#include<conio.h>
int main()
{
int a=90;
int *p,**p1,***p2;
p=&a;
p1=&p;
p2=&p1;
printf("The value of a=%d\n",***p2);
printf("The value of p=%d\n",p);
p++;
printf("The value of p=%d\n",p);
p=p+10;
printf("The value of p=%d\n",p);
p--;
printf("The value of p=%d\n",p);
p=p-2;
printf("The value of p=%d\n",p);
getch();
return 0;
}
Experiment-88
Title: Write a program to show the relationship between 1D array and
pointer.
Source Code:
#include<stdio.h>
#include<conio.h>
int main()
{
int a[4]={10,20,30,40};
int i;
int *p;
p=&a[0];
printf("Array elements are:\n");
for(i=0;i<4;i++)
{
printf("%d\t",*(p+i));
}
getch();
return 0;
}
Experiment-89
Title: Write a program to illustrate call by value and call by reference.
Source Code: (Call by Value)
#include<stdio.h>
#include<conio.h>
int swap(int,int);
void main()
{
int a=100,b=200;
printf("Before Swapping:\n");
printf("a=%d b=%d\n",a,b);
swap(a,b);
printf("a=%d b=%d\n",a,b);
}
int swap(int x,int y)
{
int temp=x;
x=y;
y=temp;
printf("x=%d y=%d\n",x,y);
}
Experiment-90
Title: Write a program to use of different DMA library function.
Source Code: (Example of malloc( ) )
#include<stdio.h>
#include<conio.h>
void main()
{
int i,n;
printf("Enter the number of elements:");
scanf("%d",&n);
int *ptr=(int *)malloc(n*sizeof(int));
if(ptr==NULL)
{
printf("Memory not available.");
}
else
{
for(i=0;i<n;i++)
{
printf("Enter the element:");
scanf("%d",ptr+i);
}
for(i=0;i<n;i++)
{
printf("%d\t",*(ptr+i));
}
}
getch();
}
Source Code: (Example of calloc ( ))
#include<stdio.h>
#include<conio.h>
void main()
{
int i,n,sum=0;
printf("Enter the number of elements:");
scanf("%d",&n);
int *ptr=(int *)calloc(n,sizeof(int));
if(ptr==NULL)
{
printf("Memory not available.");
}
else
{
for(i=0;i<n;i++)
{
printf("Enter the element:");
scanf("%d",ptr+i);
}
for(i=0;i<n;i++)
{
sum+=(*(ptr+i));
}
printf("Sum=%d",sum);
}
getch();
}
Source Code: (Example of realloc ( ))
#include<stdio.h>
#include<conio.h>
void main()
{
int i;
int *ptr=(int *)malloc(2*sizeof(int));
if(ptr==NULL)
{
printf("Memory not available.");
}
else
{
printf("Enter the two numbers:");
for(i=0;i<2;i++)
{
scanf("%d",ptr+i);
}
}
ptr=(int *)realloc(ptr,4*sizeof(int));
if(ptr==NULL)
{
printf("Memory not available.");
}
else
{
printf("Enter two more numbers:");
for(i=2;i<4;i++){
scanf("%d",ptr+i);
}
for(i=0;i<4;i++){
printf("%d\t",*(ptr+i));
}
}
getch();
}
Source Code: (Example of free ( ))
void main()
{
int *ptr, *ptr1;
int n, i;
n = 5;
printf("Enter number of elements: %d\n", n);
ptr = (int*)malloc(n * sizeof(int));
ptr1 = (int*)calloc(n, sizeof(int));
if (ptr == NULL || ptr1 == NULL) {
printf("Memory not allocated.\n");
exit(0);
}
else {
printf("Memory successfully allocated using malloc.\n");
free(ptr);
printf("Malloc Memory successfully freed.\n");
printf("\nMemory successfully allocated using calloc.\n");
free(ptr1);
printf("Calloc Memory successfully freed.\n");
}
}
Experiment-91
Title: Write a program to access the member of structure using pointer.
Source Code:
#include<stdio.h>
#include<conio.h>
struct emp
{
int id;
char name[20];
float salary;
};
void main()
{
struct emp e={101,"Kiran",45000};
struct emp *ptr=&e;
printf("%d\n",ptr->id);
printf("%s\n",ptr->name);
printf("%.2f",ptr->salary);
getch();
}
Experiment-92
Title: Write a program to pass pointer as function argument.
Source Code:
#include<stdio.h>
#include<conio.h>
int swap(int*,int*);
void main()
{
int a=100,b=200;
printf("Before Swapping:\n");
printf("a=%d b=%d\n",a,b);
swap(&a,&b);
printf("a=%d b=%d\n",a,b);
}
int swap(int *x,int *y)
{
int temp=*x;
*x=*y;
*y=temp;
printf("x=%d y=%d\n",*x,*y);
}
File Handling in C:
Experiment-93
Title: Write a program to illustrate the use of different file input/output
functions in C program.
Source Code: (fputc ( ) and fgetc ( ))
//fputc( )
#include<stdio.h>
#include<conio.h>
int main()
{
FILE *fp;
char ch;
fp=fopen("hello.txt","w");
if(fp==NULL)
{
printf("File can not open....");
}
else
{
fputc('A',fp);
}
getch();
return 0;
}
//fgetc( )
#include<stdio.h>
#include<conio.h>
int main()
{
FILE *fp;
char ch;
fp=fopen("hello.txt","r");
if(fp==NULL)
{
printf("File can not open....");
}
else
{
ch=fgetc(fp);
printf("Your character is %c",ch);
}
getch();
return 0;
}
// putw( )
#include<stdio.h>
#include<conio.h>
int main()
{
FILE *fp;
fp=fopen("hello.txt","w");
if(fp==NULL)
{
printf("File can not open....");
}
else
{
putw(97,fp);
printf("Successfully write into the file.........");
}
getch();
return 0;
}
//getw( )
#include<stdio.h>
#include<conio.h>
int main()
{
FILE *fp;
int x;
fp=fopen("hello.txt","r");
if(fp==NULL)
{
printf("File can not open....");
}
else
{
x=getw(fp);
printf("Your integer is %d",x);
}
getch();
return 0;
}
//fputs( )
#include<stdio.h>
#include<conio.h>
int main()
{
FILE *fp;
fp=fopen("hello.txt","w");
if(fp==NULL){
printf("File can not open....");
}
else{
fputs("Hello Everyone",fp);
printf("Successfully write into the file....");
}
getch();
return 0;
}
//fgets( )
#include<stdio.h>
#include<conio.h>
int main()
{
FILE *fp;
char ch[20];
fp=fopen("hello.txt","r");
if(fp==NULL)
{
printf("File can not open....");
}
else
{
fgets(ch,15,fp);
printf("%s",ch);
}
getch();
return 0;
}
Title: Program used to write name, roll no and marks of student using
fprintf ( ) function.
Source Code:
#include<stdio.h>
#include<conio.h>
int main()
{
FILE *fp;
char name[20];
int rollno;
float marks;
fp=fopen("std.txt","w");
if(fp==NULL)
{
printf("File can not open.");
}
printf("Enter Name: ");
gets(name);
printf("Enter roll.no: ");
scanf("%d",&rollno);
printf("Enter marks: ");
scanf("%f",&marks);
printf("Now writing data into file.....");
fprintf(fp,“Name=%s\nRoll.no=%d\nMarks=%.1f",
name,rollno,marks);
fclose(fp);
getch();
return 0;
}
Experiment-100
Title: Write a C program to draw a line.
Source Code:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
int main()
{
int gd=DETECT, gm;
initgraph(&gd,&gm, “C:\\TURBOC3\\BGI”);
line(50,50,200,200);
getch();
closegraph();
return 0;
}
Experiment-101
Title: Write a C program to draw a rectangle.
Source Code:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
int main()
{
int gd=DETECT, gm;
initgraph(&gd,&gm, “C:\\TURBOC3\\BGI”);
rectangle(100,100,400,400);
getch();
closegraph();
return 0;
}
Experiment-102
Title: Write a C program to draw an ellipse.
Source Code:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
int main()
{
int gd=DETECT, gm;
initgraph(&gd,&gm, “C:\\TURBOC3\\BGI”);
ellipse(200,200,0,360,50,70);
getch();
closegraph();
return 0;
}
Experiment-103
Title: Write a C program to draw an arc.
Source Code:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
int main()
{
int gd=DETECT, gm;
initgraph(&gd,&gm, “C:\\TURBOC3\\BGI”);
arc(100,100,40,200,50);
getch();
closegraph();
return 0;
}