Vous êtes sur la page 1sur 19

1.

Fibonacci Series
#include<stdio.h>
void main()
{
int a=0,b=1,c,n,i=3;
printf("Enter limit: ");
scanf("%d",&n);
c=a+b;
printf("%d\n%d\n%d\n",a,b,c);
while (i<n)
{
a=b;
b=c;
c=a+b;
printf("%d\n",c);
i++;
}
}

2. Program to generate the exponential series


#include<stdio.h>
void main()
{
float n,i=1,sum=1,x=1;
printf("Enter the number of terms for the series: ");
scanf("%f",&n);
if(n==0)
printf("\nSUM = 0\n");
else
{
while(i<n)
{
x=x*i;
sum=sum+(1/x);
i++;
}
printf("SUM = %f\n",sum);
}
}

3. Program to generate the sine series


#include<stdio.h>
#include<math.h>
#define PIE 3.14
void main()
{
float x,tsin=0,term,i=1;
printf("Enter value of x: ");
scanf("%f",&x);
x=((x*PIE)/180);
term=x;
while(fabs(term)>0.001)
{
tsin=tsin+term;
i=i+2;
term=-((x*x*term)/(i*(i-1)));
}
printf("%f\n",tsin);
}

4. Program to generate the cosine series


#include<stdio.h>
#include<math.h>
#define PIE 3.14
void main()
{
float x,tcos=0,term,i=0;
printf("Enter value of x: ");
scanf("%f",&x);
x=((x*PIE)/180);
term=1;
while(fabs(term)>0.001)
{
tcos=tcos+term;
i=i+2;
term=-((x*x*term)/(i*(i-1)));
}
printf("%f\n",tcos);
}

5. Program to find the factorial


#include<stdio.h>
void main()
{
int n,i,j;
printf("Enter the number: ");
scanf("%d",&n);
j=n;
i=n-1;
if (n==0)
n=1;
while (i>0)
{
n=n*i;
i--;
}
printf("\nFACTORIAL OF %d = %d\n\n",j,n);
}

6. Program to find the roots of the quadratic equation


#include<stdio.h>
#include<math.h>
void main()
{
float a,b,c,r1,r2,e,g,delta;
printf("Enter the coefficients of the quadratic equation: ");
scanf("%f%f%f",&a,&b,&c);
delta=(b*b)-(4*a*c);
printf("\nDelta = %f\n",delta );
if(delta>0)
{
r1=(-(b)+sqrt(delta))/(2*a);
r2=(-(b)-sqrt(delta))/(2*a);
printf("\nThe roots are: %f and %f\n\n",r1,r2);
}
if(delta==0)
{
r1=-b/(2*a);
printf("The roots are equal");
printf("\nThe root is: %f\n\n",r1);
}
if(delta<0)
{
printf("\n\nThe roots are imaginary & complex\n\n");
e=-b/(2*a);
g=sqrt(-delta)/(2*a);
printf("x= %2.2f + i %2.2f\n",e,g);
printf("x= %2.2f - i %2.2f\n",e,g);
}
}

7. Program to perform matrix calculations


#include<stdio.h>
void main()
{
int i,j,a[10][10],b[10][10],m,n,c[10][10],p,q,k;
printf("Enter the number of rows and columns of 1st matrix: \n");
scanf("%d%d",&m,&n);
printf("\nEnter the values of 1st matrix one by one: ");
for (i=1;i<=m;i++)
{
for (j=1;j<=n;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("\nEnter the number of rows and columns of 2nd matrix: ");
scanf("%d%d",&p,&q);
printf("\nEnter the values of 2nd matrix one by one: ");
for (i=1;i<=p;i++)
{
for (j=1;j<=q;j++)
{
scanf("%d",&b[i][j]);
}
}
for (i=1;i<=m;i++)
{
for(j=1;j<=q;j++)
{
c[i][j]=0;
for (k=1;k<=n;k++)
c[i][j]=c[i][j]+(a[i][k]*b[k][j]);
}
}
printf("\nRESULTANT MATRIX\n\n");
for (i=1;i<=m;i++)
{
for (j=1;j<=q;j++)
{
printf("%4d\t",c[i][j]);
}
printf("\n");
}
printf("\n");
}

8. Menu driven program


#include<stdio.h>
void main()
{
int num;
float x,y,z;
printf("\t\t---------------");
printf("\n\t\t CALCULATION");
printf("\n\t\t---------------");
printf("\n1. Addition");
printf("\n2. Subtraction");
printf("\n3. Multiplication");
printf("\n4. Division");
printf("\nEnter two numbers: ");
scanf("%f%f",&x,&y);
printf("\nEnter your choice: ");
scanf("%d",&num);
switch(num)
{
case 1:
z=x+y;
printf("\nADDITION = %f\n",z);
break;
case 2:
z=x-y;
printf("\nSUBTRACTION = %f\n",z);
break;
case 3:
z=x*y;
printf("\nMULTIPLICATION = %f\n",z);
break;
case 4:
z=x/y;
printf("\nDIVISION = %f\n",z);
break;
default:
printf("\nINVALID SELECTION\n");
break;
}
}

9. Program to sort a set of numbers using bubble sort


#include<stdio.h>
void main()
{
int i,j,n,temp;
int a[20];
printf("Enter the value of n: ");
scanf("%d",&n);
printf("\nEnter the numbers to be sorted: ");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);

}
for(i=0;i<(n-1);i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
printf("\nThe sorted array of numbers are\n");
for(i=0;i<n;i++)
{
printf("\t%d",a[i]);
}
printf("\n\n");

10. Program to sort names alphabetically


#include<stdio.h>
#include<string.h>
void main()
{
char temp[15];
char names[10][15];
int i=1,j,n,sorted=0,k;
printf("Enter the number of names to be sorted: ");
scanf("%d",&n);
printf("\nEnter the names to be sorted: \n");
for(k=0;k<n;k++)
{
scanf("%s",names[k]);
}
while((i<n)&&(!sorted))
{
sorted=1;
for(j=0;j<n-i;j++)
{
if(strcmp(names[j],names[j+1])>0)
{
strcpy(temp,names[j]);
strcpy(names[j],names[j+1]);
strcpy(names[j+1],temp);
sorted=0;
}
}
i++;
}
printf("\nThe sorted names are \n");
for(j=0;j<n;j++)
{
printf("%s\n",names[j]);
}
}

11. Program to generate the fibonacci series using recursion


#include<stdio.h>
void fib(int a,int b,int n)
{
int c,i=2;
c=a+b;
printf("%d\t",c);
a=b;
b=c;
n=n-1;
if(n>2)
{
fib(a,b,n);
}
}
void main()
{
int n,a=0,b=1;
printf("Enter the number of terms: ");
scanf("%d",&n);
if(n==0)
{
printf("\nInvalid Entry\n\n");
}

if(n==1)
{
printf("\nThe Fibonacci series\n\n");
printf("%d\t",a);
}
if(n==2)
{
printf("\nThe Fibonacci series\n\n");
printf("%d\t%d\t",a,b);
}
if(n>2)
{
printf("\nThe Fibonacci series\n\n");
printf("%d\t%d\t",a,b);
fib(a,b,n);
}

12. Program to find the factorial of a number using recursion


#include<stdio.h>
long int factorial (int n);
void main()
{
long int fact;
int n;
printf("Enter the number: ");
scanf("%d",&n);
fact=factorial(n);
printf("\nFactorial of %d = %d\n",n,fact);
}
long int factorial (int n)
{
if(n<=1)
{
return(1);
}
else
{
return(n*factorial(n-1));
}
}

13. Program to sort a set of strings using function named bsort


#include<stdio.h>
#include<string.h>
void bsort(char a[50][50],int n)
{
int i,j;
char temp[50];
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(strcmp(a[i],a[j])>0)
{
strcpy(temp,a[i]);
strcpy(a[i],a[j]);
strcpy(a[j],temp);
}
}
}
}
void main()
{
char a[50][50];
int n,i;
printf("Enter the number of strings to be sorted: ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter name %d: ",i+1);
scanf("%s",a[i]);
}
bsort(a,n);
printf("\nThe sorted strings are\n");
for(i=0;i<n;i++)
{
printf("%d. %s\n",i+1,a[i]);
}
}

14. Program to implement functions


#include<stdio.h>
void strlength(char[]);
void strcatin(char[],char[]);
void strcomp(char[],char[]);
void strcopy(char[],char[]);
void strlength(char a[])
{
int i=0;
while(a[i]!='\0')
{
i++;
}
printf("Length of string = %d\n\n\n",i);
}
void strcatin(char a[],char b[])
{
int i,j=0;
while(a[j]!='\0')
{
j++;
}

for(i=0;b[i]!='\0';i++)
{
a[j]=b[i];
j++;
}
a[j]='\0';
printf("Joined string is: %s\n\n",a);

void strcomp(char a[],char b[])


{
int flag,i,j;
for( i=0,j=0;a[i]!='\0'|| b[j]!='\0';i++,j++)
{
if(a[i]<b[j])
{
flag=-1;
}
else
if(a[i]>b[j])
{
flag=1;
}
}
if(flag<0)
{
printf("String %s has higher precedence than string
%s\n\n",a,b);

}
else
{

printf("String %s has higher precedence than string


%s\n\n",b,a);

void strcopy(char a[],char b[])


{

int i,j=0;
while(b[j]!='\0')
{
j++;
}
for(i=0;i<j;i++)
{
a[i]=b[i];
}
a[i]='\0';
printf("\n\n\nSecond string will be copied to the first
\n\n\n");
printf("The two strings become %s and %s\n\n",a,b);

void main()
{
char a[20],b[20];
int n;
printf("\n1.STRLEN\n2.STRCAT\n3.STRCMP\n4.STRCOPY\n");
printf("\nEnter your choice: ");
scanf("%d",&n);
switch(n)
{
case 1:
printf("\nEnter string: ");
scanf("%s",&a);
strlength(a);
break;
case 2:
printf("\n\nEnter two strings:\n");
printf("\n\n1. ");
scanf("%s",&a);
printf("2. ");
scanf("%s",&b);
strcatin(a,b);
break;
case 3:
printf("\n\nEnter two strings:\n");
printf("\n\n1. ");
scanf("%s",&a);
printf("2. ");

scanf("%s",&b);
strcomp(a,b);
break;
case 4:
printf("\n\nEnter two strings\n");
printf("\n\n1. ");
scanf("%s",&a);
printf("2. ");
scanf("%s",&b);
strcopy(a,b);
break;
default :
printf("\nINVALID SELECTION!!!\n\n");
break;
}
}

15. Program to sort the elements of an array using pointers


#include<stdio.h>
#include<stdlib.h>
void main()
{
int *p;
int i,n,j=0,temp;
printf("Enter the number of elements: ");
scanf("%d",&n);
p=(int*)malloc(n*2);
printf("\nEnter the numbers: ");
for(i=0;i<n;i++)
{
scanf("%d",(p+i));
}
for(j=0;j<n-1;j++)
{
for(i=j+1;i<n;i++)
{
if(*(p+j)>*(p+i))
{
temp=*(p+j);
*(p+j)=*(p+i);
*(p+i)=temp;
}
}
}
printf("\nTHE SORTED ARRAY IS\n\n");
for(j=0;j<n;j++)
{
printf("%d\t",*(p+j));
}
}

16. Program to sort a set of strings using pointers


#include<stdio.h>
#include<string.h>
#include<stdlib.h>
void main()
{
char *name[10],*temp;
int n,i,j;
printf("Enter the number of elements: ");
scanf("%d",&n);
printf("\nEnter the names:\n");
for(i=0;i<n;i++)
{
name[i]=(char*)malloc(20);
scanf("%s",name[i]);
}
for(j=0;j<n-1;j++)
{
for(i=j+1;i<n;i++)
{
if(strcmp(name[j],name[i])>0)
{
temp=name[j];
name[j]=name[i];
name[i]=temp;
}
}
}
printf("\n\nTHE SORTED LIST IS \n");
for(i=0;i<n;i++)
{
printf("%s\n",name[i]);
}
}

17. Program to prepare the ranklist of 'n' students using pointers


#include<stdio.h>
#include<stdlib.h>
struct student
{
char name[20];
int rollno;
int mark1,mark2,mark3;
int total;
};
void main()
{
int i,j,n;
struct student *s,temp;
printf("enter number of students: ");
scanf("%d",&n);
s=(struct student*)malloc(n*sizeof(struct student));
for(i=0;i<n;i++)
{
printf("\n\nEnter name: ");
scanf("%s",(s+i)->name);
printf("\nEnter roll number: ");
scanf("%d",&(s+i)->rollno);
printf("\nEnter marks of 3 subjucts: ");
scanf("%d%d%d",&(s+i)->mark1,&(s+i)->mark2,&(s+i)->mark3);
(s+i)->total=(s+i)->mark1+(s+i)->mark2+(s+i)->mark3;
}
for(i=1;i<n;i++)
{
for(j=0;j<n-i;j++)
{
if((s+j)->total<(s+j+1)->total)
{
temp=*(s+j);
*(s+j)=*(s+j+1);
*(s+j+1)=temp;
}
}
}
printf("\nRANK LIST\n\n");
for(i=0;i<n;i++)
{
printf("\nRANK %d\t%s",i+1,(s+i)->name);
printf("\tTOTAL MARK = %d",(s+i)->total);
}
printf("\n\n");
}

18. Program to display the prime numbers upto 1000


#include<stdio.h>
void main()
{
int i,x=0,j;
printf("The prime numbers upto 1000 are\n");
for(i=2;i<=1000;i++)
{
x=0;
for(j=2;j<i;j++)
{
if(i%j==0)
{
x=1;
break;
}
}
if(x==0)
{
printf("%d\t",i);
}
}
printf("\n");
}

19. Program to copy one file to another


#include<stdio.h>
#include<ctype.h>
void main(int argc,char *argv[])
{
FILE *fp1,*fp2;
char c;
fp1=fopen(argv[1],"r");
fp2=fopen(argv[2],"w");
while(!feof(fp1))
{
c=getc(fp1);
putc(c,fp2);
}
fclose(fp1);
fclose(fp2);
}

20. Program to sort numbers and write to a file


#include<stdio.h>
#include<string.h>
void main(int argc,char *argv[])
{
int i,j;
char *temp;
FILE *fp;
for(i=1;i<argc;i++)
{
for(j=1;j<argc-i;j++)
{
if(strcmp(argv[j],argv[j+1])>0)
{
temp=argv[j];
argv[j]=argv[j+1];
argv[j+1]=temp;
}
}
}
printf("THE SORTED NUMBERS\n\n");
fp=fopen("sorted.dat","w");
for(i=1;i<argc;i++)
{
fprintf(fp,"%s\n",argv[i]);
}
fclose(fp);
fp=fopen("sorted.dat","r");
while(!feof(fp))
{
fscanf(fp,"%s",temp);
printf("%s\n",temp);
}
fclose(fp);
}

21. Program to write the sorted ranklist to a file


#include<stdio.h>
#include<ctype.h>
#include<string.h>
struct student
{
char name[20];
int rollno;
int mark1,mark2,mark3;
};
void main()
{
int i,j,n;
struct student s[20],temp;
FILE *fptr;
fptr=fopen("sorted details.dat","w");
printf("Entet the number of students: ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\n\nEnter name: ");
scanf("%s",s[i].name);
printf("Enter roll number: ");
scanf("%d",&s[i].rollno);
printf("Enter marks of 3 subjects:");
scanf("%d%d%d",&s[i].mark1,&s[i].mark2,&s[i].mark3);
}
for(i=1;i<n;i++)
{
for(j=0;j<n-i;j++)
{
if(strcmp(s[j].name,s[j+1].name)>0)
{
temp=s[j];
s[j]=s[j+1];
s[j+1]=temp;
}
}
}
fwrite(&s,sizeof(struct student),n,fptr);
fclose(fptr);
fptr=fopen("sorted details.dat","r");
fread(&s,sizeof(struct student),n,fptr);
printf("THE SORTED LIST IS\n\n");
for(i=0;i<n;i++)
{
printf("\n\nName: ");
printf("%s",s[i].name);
printf(" \nRoll number: ");
printf("%d",s[i].rollno);
printf(" \nThe 3 marks\n");
printf("%d\n%d\n%d\n",s[i].mark1,s[i].mark2,s[i].mark3);
}
}

22. Program to generate the armstrong numbers upto 1000


#include<stdio.h>
#include<ctype.h>
void main()
{
int s,i,a;
long int sum=0;
FILE *fp;
fp=fopen("armstrong.dat","w");
for(i=1;i<=1000;i++)
{
s=i;sum=0;
while(s>0)
{
a=s%10;
sum=sum+(a*a*a);
s=s/10;
}
if(i==sum)
{
putw(sum,fp);
}
}
fclose(fp);
fp=fopen("armstrong.dat","r");
printf("THE ARMSTONG NUMBERS UPTO 1000 ARE......\n\n");
while(!feof(fp))
{
sum=getw(fp);
if(sum==-1) break;
printf("%d\t",sum);
}
fclose(fp);
}

Vous aimerez peut-être aussi