Академический Документы
Профессиональный Документы
Культура Документы
A
PRACTICAL FILE ON
C Language
Submitted By:
to:
Submitted
Kulwinder Singh
Vinod Beniwal
Roll No:1211253018
Proffessor,
Asst.
B.Tech-Ist sem(ME)
CSE, Deptt.
INDEX
S.n Program Name
o
1
2
3
4
5
6
Date
Teachers
remarks(si
gn)
8
9
10
11
12
13
14
15
16
average marks
PROGRAM 1:
WAP to print a line of text
#include<stdio.h>
#include<conio.h>
void main()
{
char name[20];
clrscr();
printf("enter your name::");
gets(name);
printf("your name is::");
puts(name);
getch();
}
OUTPUT:
PROGRAM 2:
WAP to find largest of three numbers
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c,sum;
float avg;
printf("enter three numbers");
scanf("%d%d%d",&a,&b,&c);
if(a>b)
{ if(a>c)
printf(" a =%d is largest",a);
else
printf(" c=%d is largest",c); }
else {
if(b>c)
printf("b =%d is largest",b);
else
printf("c=%d is largest",c);
getch();
}
OUTPUT:
PROGRAM 3
WAP to calculate sum and average of three numbers
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c,sum;
float avg;
clrscr();
printf("enter three numbers");
scanf("%d%d%d",&a,&b,&c);
sum=a+b+c;
avg=(sum)/3;
printf("sum of three numbers is=%d\n and average of three numbers is=
%f",sum,avg);
getch();
}
OUTPUT:
PROGRAM 4:
WAP to find whether a given number is even or odd
#include<stdio.h>
#include<conio.h>
void main()
{
int n;
clrscr();
printf("enter the number");
scanf("%d",&n);
if(n%2==0)
printf("number=%d is even");
else
printf("number=%d is odd");
getch();
}
OUTPUT:
PROGRAM 5
WAP to find sum of n natural numbers
#include<stdio.h>
#include<conio.h>
void main()
{
int i,n,sum=0;
clrscr();
printf("enter the value of n");
scanf("%d",&n);
for(i=0;i<=n;i++)
sum=sum+i;
printf("sum of %d natural numbers is=%d\n",n,sum);
getch();
}
OUTPUT:
PROGRAM 6(i)
WAP to swap the value of two variable using third variable
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,temp;
clrscr();
printf("enter two numbers you want to swap");
scanf("%d%d",&a,&b);
printf("before swap value of a=%d and b=%d ",a,b);
temp=a;
a=b;
b=temp;
printf("\nafter swap value of a=%d and b=%d ",a,b);
getch();
OUTPUT:
PROGRAM 6(ii)
WAP to swap the value of two variables without using third variable
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,temp;
clrscr();
printf("enter two numbers you want to swap");
scanf("%d%d",&a,&b);
printf("before swap value of a=%d and b=%d ",a,b);
a=a+b;
b=a-b;
a=a-b;
printf("\nafter swap value of a=%d and b=%d ",a,b);
getch();
OUTPUT:
PROGRAM 7
WAP to find sum of digits and reverse of a number
#include<stdio.h>
#include<conio.h>
void main()
{
int i,n,digit,sum=0;
clrscr();
printf("enter the value of n");
scanf("%d",&n);
printf("reverse of %d=",n);
for(n;n>0;n=n/10)
{
digit=n%10;
printf("%d",digit);
sum=sum+digit;
}
printf("\nsum of digits of a number is=%d",sum);
getch();}
OUTPUT:
PROGRAM 8:
WAP to find factorial of a given number
#include<stdio.h>
#include<conio.h>
void main()
{
int i,n,fact=1;
clrscr();
printf("enter the value of n");
scanf("%d",&n);
for(i=n;n>0;n--)
fact=fact*i;
printf("factorial of a number is=%d",fact);
printf("sum of digits of a number is=%d",sum);
getch();
}
OUTPUT:
PROGRAM 9:
WAP to find sum of two given matrices
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10][10],b[10][10],c[10][10],i,j,r1,c1;
clrscr();
printf("enter the order of matrix\n");
scanf("%d%d",&r1,&c1);
printf("enter the elements of the first matrix\n");
for(i=1;i<=r1;i++)
for(j=1;j<=c1;j++)
scanf("%d",&a[i][j]);
printf("enter the elements of the second matrix\n");
for(i=1;i<=r1;i++)
for(j=1;j<=c1;j++)
scanf("%d",&b[i][j]);
for(i=1;i<=r1;i++)
for(j=1;j<=c1;j++)
c[i][j]=a[i][j]+b[i][j];
printf("addition ofmatrices is\n");
for(i=1;i<=r1;i++)
{
for(j=1;j<=c1;j++)
printf("%4d",c[i][j]);
printf("\n");
}
getch();
}
OUTPUT:
PROGRAM 10.
WAP to convert a decimal number to binary
#include<stdio.h>
#include<conio.h>
void main()
{
int i,n,a[10];
clrscr();
printf("enter the number");
scanf("%d",&n);
for(i=0;n>0;n=n/2,i++)
a[i]=n%2;
i--;
for(i;i>=0;i--)
printf("%d",a[i]);
getch();}
OUTPUT:
PROGRAM 11:
WAP to transpose a matrix using array
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10][10],b[10][10],i,j,r,c;
printf("enter the order of matrix\n");
scanf("%d%d",&r,&c);
printf("enter the elements of the matrix\n");
for(i=1;i<=r;i++)
for(j=1;j<=c;j++)
scanf("%d",&a[i][j]);
for(i=1;i<=r;i++)
for(j=1;j<=c;j++)
b[j][i]=a[i][j];
printf("transposed matrix is\n");
for(i=1;i<=c;i++) {
for(j=1;j<=r;j++)
printf("%4d",b[i][j]);
printf("\n");}
getch();}
OUTPUT:
PROGRAM 12:
WAP to multiply two given matrices
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10][10],b[10][10],c[10][10],i,j,k,r1,c1,r2,c2;
clrscr();
printf("enter the order of first matrix\n");
scanf("%d%d",&r1,&c1);
printf("enter the order of second matrix\n");
scanf("%d%d",&r2,&c2);
if(c1==r2)
{
printf("enter the elements of the first matrix\n");
for(i=1;i<=r1;i++)
for(j=1;j<=c1;j++)
scanf("%d",&a[i][j]);
getch();
}
OUTPUT:
PROGRAM 13:
WAP to use call by value
#include<stdio.h>
#include<conio.h>
void fun(int,int);
void main()
{
int a=12,b=8;
clrscr();
printf("in main function values of a=%d & b=%d",a,b);
fun(a,b);
printf("\nafter calling function values of a=%d & b=%d",a,b);
getch();
}
void fun(int p,int q)
{
p=p*2;
q=q*2;
OUTPUT:
PROGRAM 14:
WAP to use call by reference
#include<stdio.h>
#include<conio.h>
void fun(int*,int*);
void main()
{
int a=12,b=8;
clrscr();
printf("in main function values of a=%d & b=%d",a,b);
fun(&a,&b);
printf("\nafter calling function values of a=%d & b=%d",a,b);
getch();
}
void fun(int * p,int * q)
{
*p=*p*2;
*q=*q*2;
OUTPUT:
PROGRAM 15:
WAP to check whether a given string is pallindrom or not
#include<stdio.h>
#include<conio.h>
void main()
{
char str[20];
int i,j;
clrscr();
printf("enter string");
gets(str);
for(j=0;str[j]!='\0';j++);
for(i=0,j--;i!=j;i++,j--)
{
if(str[i]!=str[j])
break;
}
if(i==j)
OUTPUT:
PROGRAM 16:
WAP using structure to read name,roll no and marks of a class and also
calculate the average marks
#include<stdio.h>
#include<conio.h>
void main()
{ struct student
{
int roll;
char name[20];
int marks;
};
struct student s[10];
int n,i,sum=0;
float avg;
clrscr();
printf("enter the number of students");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("enter the record of student%d\n",i);
printf("Roll no=");
scanf("%d",&s[i].roll);
printf("name=");
scanf("%s",&s[i].name);
printf("marks=");
scanf("%d",&s[i].marks);
sum+=s[i].marks;
}
avg=sum/n;
printf("the average marks of a class is:%5.2f",avg);
getch();
}
OUTPUT: