Sie sind auf Seite 1von 21

C PROGRAMS

/*adding of two numbers*/


#include<stdio.h>
main()
{
int a,b,c;
printf("enter the values of a and b:\n");
scanf("%d%d",&a,&b);
c=a+b;
printf("added number is:%d",c);
return(0);
}

/*program to find even or odd number*/


#include<stdio.h>
#include<math.h>
main()
{
int n;
printf("Enter the number:\n");
scanf("%d",&n);
if(n%2==0)
printf("number is even\n");
else
printf("number is odd\n");
}

/*program for unary operators*/


#include<stdio.h>
main()
{
int n=5;
printf("%d\n",n++);
printf("%d\n",++n);
printf("%d\n",n--);
printf("%d\n",--n);
}

/*example of gets and getchar*/


#include<stdio.h>
main()
{
char s[10],s1;
printf("Enter the string:\n");
gets(s);
printf("Enter a character:\n");
s1=getchar();
printf("string is:%s\n",s);
printf("character is:%c\n",s1);
}

/*if statement*/
#include<stdio.h>
main()
{
int a,b,c;
printf("Enter the values of a,b,c:\n");
scanf("%d%d%d",&a,&b,&c);
if((a>b)&&(a>c))
printf("%d is greatest\n",a);
else if(b>c)
printf("%d is greatest\n",b);
else
printf("%d is greatest\n",c);
}

/*maximum of 3 numbers - ternary operator*/


#include<stdio.h>
main()
{
int a,b,c,max;
printf("Enter 3 numbers:\n");
scanf("%d%d%d",&a,&b,&c);
max=((a>b)&&(a>c))?a:(b>c)?b:c;
printf("greatest is:%d",max);
}

/*switch statement*/
#include<stdio.h>
main()
{
int a,b,c,n;
printf("enter the value of a and b:\n");
scanf("%d%d",&a,&b);
printf("Enter the value:\n");
scanf("%d",&n);
switch(n)
{
case 1:
{
c=a+b;
printf("Added numbers:%d\n",c);
break;
}
case 2:
{
c=a-b;

printf("subtracted numbers:%d\n",c);
break;
}
case 3:
{
c=a*b;
printf("multiplied numbers:%d\n",c);
break;
}
case 4:
{
c=a/b;
printf("quotient is:%d\n",c);
break;
}
default:
{
printf("no operations done\n");
break;
}

}
}

/*for statement*/
#include<stdio.h>
#include<math.h>
main()
{
int n,s=0,i;
printf("Enter the number:\n");
scanf("%d",&n);
for(i=0;i<=n;i++)
{
s=s+i;
}
printf("sum of %d is %d\n",n,s);
}

/*while statement*/
#include<stdio.h>
main()
{
int year,period;
float amount,inrate,value;
printf("Enter amount,interest rate & period\n");
scanf("%f%f%d",&amount,&inrate,&period);
year=1;
while(year<=period)
{
value=amount+inrate*amount;
printf("%d Rs.%f\n",year,value);
amount=value;
year=year+1;
}
}

/*do while statement*/


#include<stdio.h>
main()
{
int year,period;
float amount,inrate,value;
printf("Enter amount,imterest rate period\n");
scanf("%f%f%d",&amount,&inrate,&period);
year=1;
do
{
value=amount+inrate*amount;
printf("%d Rs.%f\n",year,value);
amount=value;
year=year+1;
}while(year<=period);

}
/*multiplication table*/
#include<stdio.h>
main()
{
int row,column,y,m,n;
printf("Enter the values of m and n\n");
scanf("%d%d",&m,&n);
row=1;
printf(" multiplication table \n");
do
{
column=1;
do
{
y=row*column;
printf("%d\t",y);
column=column+1;
}while(column<=n);
printf("\n");
row=row+1;
}while(row<=m);
}

/*sum of digits*/
#include<stdio.h>
#include<math.h>
main()
{
int n,sum=0;
printf("Enter the number:\n");
scanf("%d",&n);
do
{
sum=sum+(n%10);
n=n/10;
}while(n!=0);
printf("sum of digits is:%d\n",sum);
}

/*conversion of decimal to binary*/


#include<stdio.h>
#include<math.h>
main()
{
int a[10],i=0,j,n;
printf("Enter the number:\n");
scanf("%d",&n);
while(n!=0)
{
a[i++]=n%2;
n=n/2;
}
for(j=i-1;j>=0;j--)
printf("%d",a[j]);
}

/*conversion of decimal to binary*/


#include<stdio.h>
#include<math.h>
main()
{
int a[10],i=0,j,n;
printf("Enter the number:\n");
scanf("%d",&n);
while(n!=0)
{
a[i++]=n%8;
n=n/8;
}
for(j=i-1;j>=0;j--)
printf("%d",a[j]);
}

/*fibonacci series*/
#include<stdio.h>
main()
{
int i=0,f1,f2=-1,f3=1,n;
printf("Enter the number of terms:\n");
scanf("%d",&n);
printf("fibonacci series:\n");
for(i=0;i<n;i++)
{
f1=f2;
f2=f3;
f3=f1+f2;
printf("%d\n",f3);
}
}
/*goto statement*/
#include<stdio.h>
#include<math.h>
main()
{
double x,y;
int count;
count=1;
printf("Enter five real number in a line\n");
read:
scanf("%lf",&x);
if(x<0)
printf("item-%d is negative\n",count);
else
{
y=x*x;
printf("%lf\t%lf\n",x,y);
}
count=count+1;
if(count<=5)
goto read;
}

/*scan and print values in single dimensional array*/


#include<stdio.h>
main()
{
int a[10],i;
printf("Enter 5 values:\n");
for(i=0;i<5;i++)
scanf("%d",&a[i]);
printf("5 values are:\n");
for(i=0;i<5;i++)
printf("%d\n",a[i]);
return(0);

/*sorting an array*/
#include<stdio.h>
main()
{
int i,j,n,a[10],t;
printf("Enter the number of values:\n");
scanf("%d",&n);
printf("Enter the values:\n");
for(i=1;i<=n;i++)
scanf("%d",&a[i]);
for(i=1;i<=n-1;i++)
{
for(j=1;j<=n-i;j++)
{
if(a[j]>a[j+1])
{
t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}}}
printf("ascending order is:\n");
for(i=1;i<=n;i++)
printf("%d\n",a[i]);
for(i=1;i<=n-1;i++)
{
for(j=1;j<=n-i;j++)
{
if(a[j]<a[j+1])
{
t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}}}
printf("descending order is:\n");
for(i=1;i<=n;i++)
printf("%d\n",a[i]);
}

/*matrix multiplication-2 dimensional array*/


#include<stdio.h>
#include<math.h>
main()
{
int a[5][5],b[5][5],c[5][5],i,j,k,m,n;
printf("Enter the no of rows and columns:\n");
scanf("%d",&n);
scanf("%d",&m);
printf("enter matrix A:\n");
for(i=0;i<n;i++)
for(j=0;j<m;j++)
scanf("%d",&a[i][j]);
printf("enter matrix B:\n");
for(i=0;i<n;i++)
for(j=0;j<m;j++)
scanf("%d",&b[i][j]);
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
for(k=0;k<m;k++)
{
c[i][j]=0;
c[i][j]=c[i][j]+(a[i][k]*b[k][j]);
}}
printf("multiplied matrix:\n");
for(i=0;i<n;i++)
for(j=0;j<m;j++)
printf("%d\n",c[i][j]);
return(0);
}

/*string operations*/
#include<stdio.h>
#include<string.h>
main()
{
char s[10]="hello";
char s1[10]="world";
int l;
l=strlen(s);
printf("length of string is:%d\n",l);
strcat(s,s1);
printf("concatinated string is:%s\n",s);
strcpy(s,s1);
printf("copied string of:%s\n",s);
if(strcmp(s,s1))
printf("strings are equal\n");
else
printf("strings are different\n");
return(0);
}

/*functions with no arguments and no return values*/


#include<stdio.h>
main()
{
printline();
value();
printline();
}
printline()
{
int i;
for(i=0;i<=35;i++)
printf("%c",'-');
printf("\n");
}
value()
{
int year,period;
float inrate,sum,principal;
printf("enter the principal amount:\n");
scanf("%f",&principal);
printf("Interest rate:\n");
scanf("%f",&inrate);
printf("Enter the period:\n");
scanf("%d",&period);
sum=principal;
year=1;
while(year<=period)
{
sum=sum*(1+inrate);
year=year+1;
}
printf("%8.2f\n",sum);
}

/*functions with arguments and no return values*/


#include<stdio.h>
#include<math.h>
main()
{
float principal,inrate;
int period;
printf("Enter the principal amount,interest rate and period\n");
scanf("%f%f%d",&principal,&inrate,&period);
printline('*');
value(principal,inrate,period);
printline('-');
}
printline(ch)
char ch;
{
int i;
for(i=1;i<=52;i++)
printf("%c",ch);
printf("\n");
}
value(p,r,n)
int n;
float p,r;
{
int year;
float sum;
sum=p;
year=1;
while(year<=n)
{
sum=sum*(1+r);
year=year+1;
}
printf("%f\n",sum);
}

/*function with arguments and return values*/


#include<stdio.h>
#include<math.h>
main()
{
float principal,amount,inrate;
int period;
printf("Enter the principal amount,interest rate and period:\n");
scanf("%f%f%d",&principal,&inrate,&period);
printline('*',52);
amount=value(principal,inrate,period);
printf("%f\n",amount);
printline('=',52);
}
printline(ch,len)
int len;
char ch;
{
int i;
for(i=0;i<=len;i++)
printf("%c",ch);
printf("\n");
}
value(p,r,n)
int n;
float p,r;
{
int year=1;
float sum;
sum=p;
while(year<=n)
{
sum=sum*(1+r);
year=year+1;
}
return(sum);
}

/*defining and assigning values to structure variables*/


#include<stdio.h>
struct p1
{
char name[20],month[10];
int day,year;
float salary;
};
main()
{
struct p1 p;
printf("Enter the name,day,month,year and salary\n");
scanf("%s%d%s%d%f",p.name,&p.day,p.month,&p.year,&p.salary);
printf("%s\t%d\t%s\t%d\t%f\n",p.name,p.day,p.month,p.salary);
}

/*comparison of structure variables*/


#include<stdio.h>
struct class
{
int number;
char name[20];
float marks;
};
main()
{
int x;
static struct class s1={11,"raja",76};
static struct class s2={22,"arun",85};
struct class s3;
s3=s2;
x=(s2.number=s3.number)?1:0;
if(x==1)
{
printf("s2 and s3 are same\n");
printf("%d\t%s\t%f\n",s3.number,s3.name,s3.marks);
}
else
{
printf("s2 and s3 have different values:\n");
}
}

/*array of structures*/
#include<stdio.h>
struct marks
{
int sub1,sub2,sub3,total;
};
main()
{
int i;
static struct marks s[3]={{66,83,68,0},{60,87,81,0},{88,63,65,0}};
static struct marks total;
for(i=0;i<=2;i++)
{
s[i].total=s[i].sub1+s[i].sub2+s[i].sub3;
}
printf("student total\n\n");
for(i=0;i<=2;i++)
printf("student[%d] %d\n",i+1,s[i].total);
}

/*structure as function parameters*/


#include<stdio.h>
struct stores
{
char name[20];
float price;
int quantity;
};
main()
{
struct stores update();
float mul(),pinc,value;
int qinc;
static struct stores item={"xyz",25.55,12};
printf("Enter price increment and quantity increment\n");
scanf("%f%d",&pinc,&qinc);
item=update(item,pinc,qinc);
printf("update values of items\n\n");
printf("name :%s\n",item.name);
printf("price :%f\n",item.price);
printf("quantity:%d\n",item.quantity);
value=mul(item);
printf("value of item=%f\n",value);

}
struct stores update(product,p,q)
struct stores product;
float p;
int q;
{
product.price+=p;
product.quantity+=q;
return(product);
}
float mul(stock)
struct stores stock;
{
return(stock.price*stock.quantity);
}

/*accessing addresses of variables*/


#include<stdio.h>
main()
{
char a='A';
int x=10;
float p=10.25,q=22.30;
printf("%c is stored at address %u\n",a,&a);
printf("%d is stored at address %u\n",x,&x);
printf("%f is stored at address %u\n",p,&p);
printf("%f is stored at address %u\n",q,&q);
}

/*accessing variables using pointers*/


main()
{
int x,y;
int *ptr;
x=10;
ptr=&x;
y=*ptr;
printf("value of x is%d\n\n",x);
printf("%d is stored at address %u\n",x,&x);
printf("%d is stored at address %u\n",*&x,&x);
printf("%d is stored at address %u\n",*ptr,ptr);
printf("%d is stored at address %u\n",y,&*ptr);
printf("%d is stored at address %u\n",ptr,&ptr);
printf("%d is stored at address %u\n",y,&y);
}

/*illustration of pointer expression*/


#include<stdio.h>
#include<math.h>
main()
{
int a,b,*p1,*p2,x,y,z;
a=12;
b=4;
p1=&a;
p2=&b;
x=*p1 * *p2 - 6;
y=4* - *p2 / *p1 + 10;
printf("address of a=%u\n",p1);
printf("address of b=%u\n",p2);
printf("\n");
printf("a=%d,b=%d\n",a,b);
printf("x=%d,y=%d\n",x,y);
*p2=*p2+3;
*p1=*p2-5;
z=*p1 * *p2 - 6;
printf("\na=%d,b=%d,z=%d\n",a,b,z);
}

/*pointers in one dimension array*/


#include<stdio.h>
#include<math.h>
main()
{
int *p,sum,i=0;
static int x[5]={5,9,6,3,7};
p=x;
sum=0;
printf("element\tvalue\taddress\n");
while(i<5)
{
printf("x[%d]\t%d\t%u\n",i,*p,p);
sum=sum + *p;
i++,p++;
}
printf("\nsum=%d\n",sum);
printf("\n&x[0]=%u\n",&x[0]);
printf("\np=%u\n",p);
}

/*pointers and character strings*/


#include<stdio.h>
main()
{
char *name;
int length;
char *cptr=name;
name="DELHI";
while(*cptr!='\0')
{
printf("%c is stored at address %u\n",*cptr,cptr);
cptr++;
}
length=cptr-name;
printf("\nlength of string=%d\n",length);
}

/*pointer as function parameters*/


#include<stdio.h>
main()
{
int x,y;
printf("Enter the values of x & y:\n");
scanf("%d%d",&x,&y);
printf("before swapping :x=%d,y=%d\n\n",x,y);
swap(&x,&y);
printf("after swapping :x=%d,y=%d\n\n",x,y);
}
swap(a,b)
int *a,*b;
{
int t;
t=*a;
*a=*b;
*b=t;
}

/*pointers to structure variables*/


struct invent
{
char *name[20];
int number;
float price;
};
main()
{
struct invent product[3],*ptr;
printf("INPUT\n");
for(ptr=product;ptr<product+3;ptr++)
scanf("%s%d%f",ptr->name,&ptr->number,&ptr->price);
printf("OUPUT\n");
ptr=product;
while(ptr<product+3)
{
printf("%s\t%d\t%f\n",ptr->name,ptr->number,ptr->price);
ptr++;
}
}
}

/*writing to and reading from a file*/


#include<stdio.h>
main()
{
FILE *f1;
char c;
printf("Data Input\n");
f1=fopen("input.dat","w");
while((c=getchar())!=EOF)
putc(c,f1);
fclose(f1);
printf("\nData output\n");
f1=fopen("input.dat","r");
while((c=getc(f1))!=EOF)
printf("%c",c);
fclose(f1);
}

/*handling of integer data files*/


#include<stdio.h>
main()
{
FILE *f1,*f2,*f3;
int number,i;
printf("contents of data file\n\n");
f1=fopen("DATA.dat","w");
scanf("%d",&number);
putw(number,f1);
fclose(f1);
f1=fopen("DATA.dat","r");
f2=fopen("ODD.dat","w");
f3=fopen("EVEN.dat","w");
while((number=getw(f1))!=EOF)
{
if(number%2==0)
putw(number,f3);
else
putw(number,f2);

}
fclose(f1);
fclose(f2);
fclose(f3);
f2=fopen("ODD.dat","r");
f3=fopen("EVEN.dat","r");
printf("\n\ncontents of odd file\n");
while((number=getw(f2))!=EOF)
printf("%4d",number);
printf("\n\ncontents of even file\n");
while((number=getw(f3))!=EOF)
printf("%4d",number);
fclose(f2);
fclose(f3);
return 0;
}
/*handling mixed data types using fscanf and fprintf*/
#include<stdio.h>
main()
{
FILE *fp;
int number,quantity,i;
float price,value;
char item[10],filename[10];
printf("Enter the input file name:\n");
scanf("%s",filename);
fp=fopen(filename,"w");
printf("input inventory data\n\n");
printf("itemname\tnumber\tprice\tquantity\n");
for(i=0;i<3;i++)
{
fscanf(stdin,"%s%d%f%d\n",item,&number,&price,&quantity);
fprintf(fp,"%s\t%d\t%\t%d\n",item,number,price,quantity);
}
fclose(fp);
fprintf(stdout,"\n\n");
fp=fopen(filename,"r");
printf("itemname\tnumber\tprice\tquanity\tvalue\n");
for(i=0;i<3;i++)
{
fscanf(fp,"%s%d%f%d\n",item,&number,&price,&quantity);
value=price*quantity;
fprintf(stdout,"%s\t%d\t%f\t%d\t%f\n",item,number,price,quantity);
}
fclose(fp);
}

/*random access using fseek and ftell*/


#include<stdio.h>
main()
{
FILE *fp;
long n;
char c;
fp=fopen("random.dat","w");
while(c=getchar()!=EOF)
putc(c,fp);
printf("Number of characters entered=%ld\n",ftell(fp));
fclose(fp);
fp=fopen("random.dat","r");
n=0L;
while(!feof(fp)==0)
{
fseek(fp,n,0);
printf("position of %c is %ld\n",getc(fp),ftell(fp));
n=n+5L;
}
putchar('\n');
fseek(fp,-1L,2);
do
{
putchar(getc(fp));
}
while(!fseek(fp,-2L,1));
fclose(fp);
}

/*error handling in files*/


#include<stdio.h>
main()
{
char *filename;
FILE *fp1,*fp2;
int i,number;
fp1=fopen("test.dat","w");
for(i=10;i<=100;i+=10)
putw(i,fp1);
fclose(fp1);
printf("\ninput filename\n");
open_file:
scanf("%s",filename);
if((fp2=fopen(filename,"r"))==NULL)
{
printf("cannot open the file\n");
printf("type the correct file name\n");
goto open_file;
}
else
{
for(i=1;i<=20;i++)
{
number=getw(fp2);
if(feof(fp2))
{
printf("\nran out of data\n");
break;
}
else
printf("%d\n",number);
}
}
fclose(fp2);
}

/*command line arguments*/


#include<stdio.h>
main(argc,argv)
int argc;
char *argv[];
{
FILE *fp;
int i;
char word[15];
fp=fopen(argv[1],"w");
printf("\nno of arguments in command line=%d\n\n",argc);
for(i=2;i<argc;i++)
fprintf(fp,"%s",argv[i]);
fclose(fp);
printf("contents of %s file\n\n",argv[1]);
fp=fopen(argv[1],"r");
for(i=2;i<argc;i++)
{
fscanf(fp,"%s",word);
printf("%s",word);
}
fclose(fp);
printf("\n\n");
for(i=0;i<argc;i++)
printf("%*s\n",argv[i]);
}

Das könnte Ihnen auch gefallen