C Programs Syallabus
C Programs Syallabus
C Programs Syallabus
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr();
printf(“enter the three numbers\n”);
scanf(“%d%d%d”,&a,&b,&c);
printf(“the greatest number is\n”);
if((a>b)&&(a>c))
printf(“a is greatest %d”,a);
else if(b>c)
printf(“b is greatest %d”,b);
else
printf(“c is greatest %d”,c);
getch();
}
1
printf(“\nthe answer is %d”,c);
break;
case 2:
printf(“\nenter values of a and b\n”);
scanf(“%d%d”,&a,&b);
c=a-b;
printf(“\nthe answer is %d”,c);
break;
case 3:
printf(“\nenter values of a and b\n”);
scanf(“%d%d”,&a,&b);
c=a*b;
printf(“\nthe answer is %d”,c);
break;
case 4:
printf(“\nenter values of a and b\n”);
scanf(“%d%d”,&a,&b);
c=a/b;
printf(“\nthe answer is %d”,c);
break;
case 5:
printf(“\nenter values of a and b\n”);
scanf(“%d%d”,&a,&b);
c=a%b;
printf(“\nthe answer is %d”,c);
break;
default:
printf(“\nenter the correct choice”);
break;
}
getch();
}
2
int a,b,c,d;
float x1,x2;
clrscr();
printf(“\nenter the values of a,b,c\n”);
scanf(“%d%d%d”,&a,&b,&c);
d=b*b-4*a*c;
if(d==0)
{
printf(“\nthe roots are real and equal”);
x1=-b/(2*a);
printf(“%f\n”,x1);
}
else if(d<0)
{
printf(“\nthe roots are imaginary\n”);
x1=-b/(2*a);
x2=sqrt(-d)/(2*a);
printf(“%2f+i%2f\n”,x1,x2);
printf(“%2f-i%2f\n”,x1,x2);
}
else
{
printf(“\n the roots are real and distinct”);
x1=-b+sqrt(d)/(2*a);
x2=-b-sqrt(d)/(2*a);
printf(“\n%f\n%f”,x1,x2);
}
getch();
}
3
while(no>0)
{
r=no%2;
sum=sum+pow(10,i)*r;
no=no/2;
i++;
}
printf(“\nthe binary value is %d”,sum);
getch();
}
4
printf(“enter the readings\n”);
scanf(“%f”,&r);
if(r>=200)
printf(“rupees=%f”,r*b);
else if((r>=100)&&(r<200))
printf(“rupees=%f”,r*a);
else
printf(“rupees=%f”,r*c);
getch();
}
#include<stdio.h>
#include<conio.h>
void main()
{
int a[5],max,i;
clrscr();
printf(“enter elements for the array\n”);
for(i=0;i<5;i++)
scanf(“%d”,&a[i]);
max=a[0];
for(i=1;i<5;i++)
{
if(max<a[i])
max=a[i];
}
printf(“the maximum value is%d”,max);
getch();
}
8.Menu driven program to insert and delete a specified element from the array
#include<stdio.h>
#include<conio.h>
void main()
{
int int,ch,a[15],x,val;
clrscr();
5
printf(“enter the no of values to be done:”);
scanf(“%d”,&n);
printf(“enter the values”);
for(i=0;i<n;i++)
{
scanf(“%d”,&a[i]);
}
For(i=0;i<n;i++)
printf(“\n1.INSERT\n2.DELETE”);
printf(“enter your choice”);
scanf(“%d”,&ch);
switch(ch)
{
case1:
printf(“the position to be inserted:”);
scanf(“%d”,&x);
print(“the value to be insereted:”);
scanf(“%d”,&val);
for(i=n-1;i>=x-1;i--)
a[i+1]=a[i];
a[x-1]=val;
n++;
for(i=0;i<n;i++)
{
printf(“%d”,a[i]);
}
break;
case2:
printf(“enter the position of deletion:”);
scanf(“%d”,&x);
for(i=x-1;i<n;i++)
{
a[i]=a[i+1];
}
for(i=0;i<n-1;i++)
{
printf(“%d”,a[i]);
}
break;
default:
6
printf(“invalid choice”);
}
getch();
}
10.Program to merge given two one dimensional arrays and to remove the
duplicates
#include<stdio.h>
#include<conio.h>
void main()
{
int j,h=0,k=0;
int x[4]={1,2,3,4};
7
int y[4]={5,6,7,8};
int z[8];
clrscr();
printf(“array x:\n”);
for(j=0;j<4;j++)
printf(“%d”,x[j]);
printf(“array y:\n”);
for(j=0;j<4;j++)
printf(“%d”,y[j]);
j=0;
while(j<8)
{
if(j%2==0)
z[j]=x[k++];
else
z[j]=y[h++];
j++;
}
printf(“array z:\n”);
for(j=0;j<8;j++)
printf(“%d”,z[j]);
getch();
}
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10][10],b[10][10],c[10][10],r1,r2,c1,c2,i,j,k;
clrscr();
printf(“enter the no.of rows and columns for 1st matrix:\n”);
scanf(“%d%d”,&r1,&c1);
printf(“enter the values of 1st matrix:\n”);
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
scanf(“%d”,&a[i][j]);
}
8
printf(“enter the no.of rows and columns for 2nd matrix:\n”);
scanf(“%d%d”,&r2,&c2);
printf(“enter the values of 2nd matrix:\n”);
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
scanf(“%d”,&b[i][j]);
}
if(c1==r2)
{
for(i=0;i<r1;i++)
{
for(j=0;j<c2;j++)
{
c[i][j]=0;
for(k=0;k<c1;k++)
{
c[i][j]=c[i][j]+a[i][k]+b[k][j];
}
}
}
printf(“resultant matrix is:\n”);
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
printf(“%d\t”,c[i][j]);
}
printf(“\n”);
}
}
else
printf(“the matrix are not multiplied”);
getch();
}
String manipulations
9
#include<stdio.h>
#include<conio.h>
void main()
{
int m,flag=0,l,I;
char a[30];
printf(“\nenter the strings”);
scanf(“%s”,&a);
l=strlen(a);
m=l-1;
for(i=0;i<l/2;i++)
{
if(a[i]!=a[m])
{
flag=1;
break;
}
m--;
}
if(flag==0)
{
printf(“\ngiven string is palindrome ”);
}
else
{
printf(“\ngiven string is not a palindrome ”);
}
getch();
}
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char str[50],str1[15],str2[15],temp[50];
10
char *ptr;
int cnt;
clrscr();
printf(“enter a line of text..... \n”);
gets(str);
printf(“enter the string to be replaced... \n”);
gets(str1);
printf(“enter the replacing string...”);
gets(str2);
printf(“\n the replaced line of the text....”);
while(1)
{
ptr=strstr(str,str1);
if(ptr==’\0’)
break;
cnt=ptr-str;
strncpy(temp,str,cnt);
temp[cnt]=’10’;
strcat(temp,str+cnt+strlen(srt1));
strcpy(str1,temp);
puts(str);
}
getch();
}
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
int i,j,n,x;
char str[20][20],str1[20][20];
clrscr();
printf(“enter the number of strings:\n”);
scanf(“%d”,&n);
for(i=0;i<n;i++)
{
printf(“\nenter str[%d]”,i+1);
11
scanf(“%s”,&str[i]);
}
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
x=strcmp(str[i],str[j])
if(x>0)
{
strcpy(str[1],str[j]);
strcpy(str[j],str[i]);
strcpy(str[i],str[1]);
}
}
}
printf(“\nthe sorted strings in ascending order is\n”);
for(i=0;i<n;i++)
{
printf(“\n%s”,str[i]);
}
getch();
}
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char str[50];
int i=0,count=0,whitespace=0,consonants=0,specialchar=0;
printf(“enter a string”);
gets(str);
while(str[i]!=’\0’)
{
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++;
12
if(str[i]=’ ‘ )
whitespace++;
if(str[i]=’!’||str[i]=’@’||str[i]=’#’||str[i]=’$’||str[i]=’%’|| str[i]=’^’||str[i]=’&’||
str[i]=’*’||str[i]=’+’||str[i]=’-’)
specialchar++;
else
consonants++;
i++;
}
printf(“\n nos of vowels=%d\n nos of consonants=%d\n nos of whitespace=%d nos
of special characters=%d”,count,consonants,whitespace,specialchar);
getch();
return 0;
}
Functions
16. Program to swap the contents of two variables using functions (Pass
by address and pass by reference)
#include<stdio.h>
#include<conio.h>
int swapval(int,int);
int swapref(int*,int*);
int a,b;
void main()
{
clrscr();
printf(“enter the two values\n”);
scanf(“%d%d”,&a,&b);
printf(“pass by value\n”);
printf(“before function call a=%d b=%d “,a,b);
swapval(a,b);
printf(“after function swapval a=%d b=%d “,a,b);
printf(“pass by reference\n”);
printf(“before function call a=%d b=%d “,a,b);
swapref(&a,&b);
printf(“after function swapref a=%d b=%d “,a,b);
getch();
}
swapval(int x,int y)
13
{
int t;
t=x;
x=y;
y=t;
printf(“\nwith swap val x=%d y=%d”,x,y);
}
swapref(int*x,int*y)
{
int *t;
*t=*x;
*x=*y;
*y=*t;
printf(“\nwith swapref x=%d y=%d “,*x,*y);
}
#include<stdio.h>
#include<conio.h>
int fibo(int,int)
int t1,t2,t3,count;
void main()
{
printf(“enter the number of terms\n”);
scanf(“%d”,&n);
t1=0;
t2=1;
printf(“%d\t%d”,t1,t2);
count=2;
fibo(t1,t2);
getch();
}
int fibo(int t1,int t2)
{
if(count>=n)
return 0;
else
{
t3=t1+t2;
14
printf(“\t%d”,t3);
count++;
t1=t2;
t2=t3;
fibo(t1,t2);
}
}
18. Program to print the average and standard deviation of the elements of
the one- dimensional array using function.
#include<stdio.h>
#include<conio.h>
#include<math.h>
float mean(int a[],int n);
float std(int a[],int n,float m);
void main()
{
float m,sd;
int n,a[10],i;
clrscr();
printf(“\nenter the number of values\n”);
scanf(“%d”,&n);
printf(“\n enter the elements\n”);
for(i=0;i<n;i++)
scanf(“%d”,&n);
m=mean(a,n);
printf(“mean=%f\n”,m);
sd=std(a,n,m);
printf(“\n sd=%f”,sd);
getch();
}
flaot mean (inta[],intn)
{
float f;
int sum=0;
for(i=0;i<n;i++)
sum=sum+ a[i];
f=(float)sum/n;
15
return f;
}
float std(int a[],int n,float m)
{
int i;
float std,sum=0.0,d;
for(i=0;i<n;i++)
{
d=a[i]-m;
a=d*d;
sum=sum+d;
}
sd=sqrt(sum\n);
return sd;
}
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
void trans(int,int,int[10][10]);
int i,j,a,b,m[10][10];
clrcsr();
printf(“enter the rows and columns of matrix”);
scanf(”%d%d”,&a,&b);
printf(“enter the elements”);
for(i=1;i<=a;i++)
{
for(j=1;j<=b;j++)
{
printf(“enter m[%d][%d]...”i,i);
scanf(“%d”,&m[i][j]);
}
}
printf(“\n before transpose\);
for(i=1;i<=a;i++)
{
16
for(j=1;j<=b;j++)
{
printf(“%d\t”,m[i][j]);
}
printf(“\n”);
}
trans(a,b,m);
getch();
}
void trans(inta,intb,intm[10][10])
{
int i,j;
printf(“after transpose”);
for(j=1;j<=b;j++)
{
for(i=1;i<=a;i++)
{
printf(“\t%d”,m[i][j]);
}
printf(“\n”);
}
}
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
struct stud
{
int rno;
char name[15];
17
int marks[5];
int total;
float avg;
char class[15];
}
st[10],temp;
int i,n,j;
clrscr();
printf(“\n enter\n”);
scanf(“%d”,&n);
for(i=1;i<=n;i++)
{
printf(“\n enter the roll no..”);
scanf(“%d”,&st[i].rno);
printf(“name...\n”);
scanf(“%s”,&st[i].name);
printf(“enter three marks..”);
for(j=1;j<=3;j++)
scanf(“%d”,&st[i].marks[j]);
}
for(i=1;i<=n;i++)
{
st[i].total=0;
for(j=1;j<=3;j++)
{
st[i].total=st[i].total+st[i].marks[j];
}
st[i].avg=st[i].total/30;
if(st[i].avg>=75)
strcpy(st[i].grade,”distinction”);
elseif(st[i].avg>=60)
strcpy(st[i].grade,”first”);
elseif(st[i].avg>=50)
strcpy(st[i].grade,”second”);
else
strcpy(st[i].grade,”fail”);
}
for(i=1;i<=n;i++)
{
for(j=j+1;j<=n;j++)
18
{
if(st[i].total<st[j].total)
{
temp=st[i];
st[i]=st[j];
st[j]=temp;
}
}
}
printf(“\n the student details in rankwise\n”);
for(i=1;i<=n;i++)
{
printf(“\n\n roll no:%d”,st[i].rno);
printf(“\n name :%s”,st[i].name);
printf(“\n marks in three subjects”);
for(j=1;j<=3;j++)
{
printf(“\n %d,st[i].marks[j]);
}
printf (“\n total: %d”, st[i].total);
printf(“\n average:%f”,st[i].avg);
printf(“\n grade:%s”,st[i].grade);
}
getch();
}
19
clrscr();
printf("enter the no:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("\n enter the employ name");
fflush(stdin);
gets(e1.empname);
printf("\n enter bp,da,hra,ded");
scanf("%f%f%f%f",&e1.bp,&e1.da,&e1.hra,&e1.ded);
e1.gross=e1.bp+e1.da+e1.hra+e1.ded;
e1.net=(e1.gross)-(e1.ded);
printf("\n empname=%s \n empno=%d\n",e1.empname,e1.empno);
printf(" the employ basic=%f",e1.bp);
printf("\n da=%f",e1.da);
printf("\n hra=%f \n ded=%f",e1.hra,e1.ded);
printf("\n gross=%f \n netpay=%f",e1.gross,e1.net);
}
getch();
}
#include<stdio.h>
#include<conio.h>
#include<process.h>
void main()
{
FILE *ft,*fs;
int c=0;
clrscr();
fs=fopen("a.txt","r");
ft=fopen("b.txt","w");
if(fs==NULL)
{
printf("\n source file opening error");
exit(1);
}
else
if(ft==NULL)
20
{
printf("target file opening error");
exit(1);
}
while(!feof(fs))
{
fputc(fgetc(fs),ft);
c++;
}
printf("%d bytes copied from 'a.txt' to 'b.txt'",c);
c=fcloseall();
printf("%d files closed",c);
getch();
}
24. Program to find the length of the string, copy one string to another and
compare two strings, concatenate two strings without using library functions.
#include<stdio.h>
#include<conio.h>
void main()
{
char a[20];
int i;
clrscr();
gets(a);
i=0;
while(a[i]!=’\0’);
i++;
printf(“string length=%d”,i);
getch();
}
#include<stdio.h>
#include<conio.h>
void main()
21
{
char s1[100],s2[100];
int i;
clrscr();
printf(“\n enter the string:”);
gets(s1);
i=0;
while(s1[i]!=’\0’);
{
s2[i]=s1[i];
i++;
}
s2[i]=’\0’
printf(“copied string is %s”,s2);
getch();
}
#include<stdio.h>
#include<conio.h>
void main()
{
char str1[100],str2[100];
int i;
clrscr();
printf(“\n enter the two strings:”);
gets(str1);
gets(str2);
i=0;
while(str1[i]==str2[i] && str1[i]!=’\0’);
i++;
if(str1[i]>str2[i])
printf(“\n string 1 is greater than string two”);
else if(str1[i]<str2[i])
printf(“\n string 1 is smaller than string two”);
else
printf(“\n string 1 is equal to string two”);
getch();
22
}
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char str1[30],str2[30];
int l1,l2,i;
clrscr();
gets(str1);
gets(str2);
l1=strlen(str1);
l2=strlen(str2);
for(i=0;i<=l2;i++)
{
str1[l1+i]=str2[i];
}
printf("%s",str1);
getch();
}
23