Vous êtes sur la page 1sur 12

Prg.

to convert upper case to lower case or lower case to upper case depending on the name it is invoked with as found in argument. #include <stdio.h> #include <conio.h> void lower_to_upper(); void upper_to_lower(); void main() { int n; clrscr(); printf(" Please enter your choice."); printf(" (1) for upper to lower conversion."); printf(" (2) for lower to upper conversion."); printf(" CHOICE:- "); scanf("%d",&n); switch (n) { case 1: { printf("Please enter a string in upper case."); printf(" String will be terminated if you press Ctrl-Z."); printf(" STRING:- "); upper_to_lower(); break; } case 2: { printf("Please enter a string in lower case."); printf(" String will be terminated if you press Ctrl-Z."); printf(" STRING:- "); lower_to_upper(); break; } default: printf("ERROR"); } printf("

HAVE A NICE DAY! BYE."); getch(); } void upper_to_lower() { int i,j; char c4[80],c3; for (i=0;(c3=getchar())!=EOF;i++) c4[i]=(c3>='A' && c3<='Z')?('a' + c3 -'A'):c3; printf(" The lower case equivalent is "); for (j=0;j<i;j++) putchar(c4[j]); return; } void lower_to_upper() { int i,j; char c2[80],c1; for (i=0;(c1=getchar())!=EOF;i++) c2[i]=(c1>='a' && c1<='z')?('A' + c1 -'a'):c1; printf(" The upper case equivalent is "); for (j=0;j<i;j++) putchar(c2[j]); return; } Prg. to count no. of characters,no. of blanks,no. of words & no. of lines in a multi line string #include <stdio.h> #include <conio.h> void main() { char c,choice; int nc=0,nb=0,nw=1,nl=1,count,i/*,flag=1*/; clrscr(); scanf("%c",&choice); printf("ENTER STRING:- "); printf(" String will be terminated when you press Ctrl-Z."); printf(" STRING:- "); while ((c=getchar())!=EOF) { switch(1) { case 1:

if (c==EOF||c==' '||c==' ') ; else nc=nc+1; case 2: if (c==' ') { nc=nc+1; nb=nb+1; while((c=getchar())==' ') { nb=nb+1; nc=nc+1; } if (c!=' ') { nc=nc+1; nw=nw+1; } } case 3: if(c==' ') { nc=nc+1; nb=nb+1; nw=nw+1; nl=nl+1; } } } printf(" no. of characters is %d",nc); printf(" no. of blanks is %d",nb); printf(" no. of words is %d",nw); printf(" no. of lines is %d",nl); fflush(stdin); printf (" Do you want to continue?(y/n):- "); scanf("%c",&choice); getch();

} Prg. to sort any no. of numeral i-p in ascending or descending order. void sort(void); int c,a[20],l; void main() { clrscr(); printf("Enter no. of elements in the list:- "); scanf ("%d",&l); printf(" CHOICE:-"); printf(" (1) Sort in ascending order."); printf(" (2) Sort in descending order."); printf(" CHOICE:- "); scanf("%d",&c); if (c!=1 && c!=2) { printf(" ERROR"); getch(); exit(0); } sort(); getch(); } void sort(void) { int n,i,j,temp=0,min,k; for (i=1;i<=l;i++) { printf(" Enter no.:- "); scanf("%d",&a[i]); } for (i=1;i<=(l-1);i++) { min=a[i]; k=i; for (j=(i+1);j<=l;j++) { if (a[j]<min) { min=a[j]; k=j; }

} temp=a[k]; a[k]=a[i]; a[i]=temp; } switch(c) { case 1: printf(" Elements in ascending order are:-"); for (i=1;i<=l;i++) printf(" %d",a[i]); break; case 2: printf(" Elements in descending order are:-"); for (i=l;i>=1;i--) printf(" %d",a[i]); break; default: printf(" ERROR"); } return; } Printint a double pyramid #include<stdio.h> #include<conio.h> void main(void) { clrscr(); int i,j,k,l,b,n; printf("Enter the value of N:"); scanf("%d",&n); for(i=0;i<n;i++) { printf(" "); for(l=0;l<i;l++) printf(" "); for(j=i+1;j<=n;j++) printf("%d",j); for(k=n-1;k>i;k--) printf("%d",k); } b=n-1; for(i=0;i<n-1;i++)

{ printf(" "); for(l=n-2;l>i;l--) printf(" "); for(j=b;j<=n;j++) printf("%d",j); for(k=n-1;k>=b;k--) printf("%d",k); b--; } getch(); } Program for computing Area, Volume and Perimeter of Rrectangle using function with looping #include <stdio.h> void inputoutput () { int comp,ans; clrscr (); printf ("choose please: 1=perimeter,2=area,3=volume] ?: "); scanf ("%d",&comp); if (comp==1) { int le, wi; printf ("Enter the length: "); scanf ("%d",&le); printf ("Enter the width: "); scanf ("%d",&wi); printf ("P=%d",perimeter(le,wi)); } else if (comp==2) { int le, wi; printf ("Enter the length: "); scanf ("%d",&le); printf ("Enter the width: "); scanf ("%d",&wi); printf ("A=%d", area(le,wi)); } else if (comp==3) { int length,width,height; printf ("Enter lenght: "); scanf ("%d",&length); printf ("Enter width: "); scanf ("%d",&width); printf ("Enter height: "); scanf ("%d",&height);

printf ("V=%d",volume (length,width,height)); } else inputoutput (); printf (" Do you want to continue? [Yes=1/No=0]: "); scanf ("%d",&ans); if (ans==1) inputoutput (); else printf ("GOOD BYE"); } int perimeter (int l, int w) { int per; per=(l*2)+(w*2); return (per); } int area (int le, int wi) { int are; are=le*wi; return (are); } int volume (int length, int width, int height) { int vol; vol=(length*width*height); return (vol); }

main () { clrscr (); inputoutput (); getch (); } Program for finding the prime numbers #include <stdio.h> #include <conio.h> void main() { int n,m,k,i,max; char c; clrscr(); repeat: max=0; k=2;

n=1; printf("You want prime numbers upto:- "); scanf("%d",&max); printf(" "); for (i=1;i<=max;i++) { again: m=(n/k)*k; if (m!=n) k=k+1; else goto try1; if (k < n/2) goto again; else printf("%d",n); printf(" "); try1: n=n+1; k=2; } fflush(stdin); printf (" Do you want to continue?(y/n):- "); scanf("%c",&c); if (c=='y') goto repeat; getch(); } Program for finding the sum of digits of a five digit number #include <stdio.h> #include <conio.h> void main() { int n,o,p,q,r,s; char c; clrscr(); repeat: s=0; printf(" Enter a five digit no.:- "); scanf("%d",&n); o=n%10000; p=o%1000; q=p%100; r=q%10; s=(n/10000)+(o/1000)+(p/100)+(q/10)+r; printf("The sum of its digits is %d.",s);

fflush(stdin); printf (" Do you want to continue?(y/n):- "); scanf("%c",&c); if (c=='y') goto repeat; getch(); } Program for Prime Number Generation #include <stdio.h> main() { int n,i=1,j,c; clrscr(); printf("Enter Number Of Terms "); printf("Prime Numbers Are Follwing "); scanf("%d",&n); while(i<=n) { c=0; for(j=1;j<=i;j++) { if(i%j==0) c++; } if(c==2) printf("%d ",i) i++; } getch(); } Program to Calculate the Pascal triangle #include<stdio.h> #include<conio.h> void main() { int a[10][10]; int i,j,c,n; clrscr(); printf("Enter how many lines do you want"); scanf("%d",&n); a[1][1]=1; printf("%5d",a[1][1]); a[2][1]=1;a[2][2]=2;a[2][3]=1; printf("%d %d %d",a[2][1],a[2][2],a[2][3]);

for(i=3;i<=n;i++) { a[i][1]=1; printf("%d",a[i][1]); j=2;c=3; while(j<=i) { a[i][j]=a[i-1][c-1]+a[i-1][c-2]; printf("%5d",a[i][j]); c=c+1; j=j+1; } a[i][j]=1; printf("%d",a[i][j]); } } Program to calculate the sum of series #include <stdio.h> #include <conio.h> long int factorial(int n); void main() { int n,i; float s,r; char c; clrscr(); repeat : printf(" You have this series:- 1/1! + 2/2! + 3/3! + 4/4! ..."); printf(" To which term you want its sum? "); scanf("%d",&n); s=0; for (i=1;i<=n;i++) { s=s+((float)i/(float)factorial(i)); } printf(" The sum of %d terms is %f",n,s); fflush(stdin); printf (" Do you want to continue?(y/n):- "); scanf("%c",&c); if (c=='y') goto repeat; getch(); } long int factorial(int n) {

if (n<=1) return(1); else n=n*factorial(n-1); return(n); } Program to calculate frequency of vowels in a string #include <stdio.h> #include <conio.h> void main() { int a=0,e=0,i=0,o=0,u=0,sum=0; char c; clrscr(); printf("Enter string:- "); printf(" String will be terminated if you press Ctrl-Z & then ENTER."); printf(" STRING:- "); while ((c=getchar())!=EOF) { if (c=='a'||c=='A') a=a+1; if (c=='e'||c=='E') e=e+1; if (c=='i'||c=='I') i=i+1; if (c=='o'||c=='O') o=o+1; if (c=='u'||c=='U') u=u+1; } sum=a+e+i+o+u; printf(" Frequency of vowel 'a' is %d.",a); printf(" Frequency of vowel 'e' is %d.",e); printf(" Frequency of vowel 'i' is %d.",i); printf(" Frequency of vowel 'o' is %d.",o); printf(" Frequency of vowel 'u' is %d.",u); printf(" Total no. of vowels in the text is %d.",sum); printf("

HAVE A NICE DAY! BYE."); getch(); } Program of Falling Characters #include<stdio.h> #include<conio.h> #include<dos.h> #include<string.h> void main() { int i,j,l,t; char y[20]; clrscr(); printf("Enter a string "); gotoxy(2,2); gets(y); l=strlen(y); t=1; for(j=0;j<l;j++,t++) { for(i=3;i<=24;i++) { clrscr(); puts(y); gotoxy(t,i); printf("%c",y[j]); delay(100); } } clrscr(); getch(); }

Vous aimerez peut-être aussi