Sie sind auf Seite 1von 12

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 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(); } ");

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(); } ");

Program to display all vowels in a i/p line of text.

#include <stdio.h> #include <conio.h> void main() { char line[80],c; int i; clrscr(); printf(" Enter string:-"); printf(" String will be terminated if you press ENTER"); printf("

STRING:-"); gets(line); for (i=0;(i<=80 && line[i]!='

Prg. to convert upper case to lower case or lower case to upper case depending on the name it is inv

#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; }

Program to reverse the input string.

#include <stdio.h> #include <conio.h> # define EOLN ' ' void reverse(void);

/* function prototype */

void main() { char c; clrscr(); printf(" Please enter a line of text below<BR>); reverse(); printf(" is the reversed form."); getch(); } void reverse(void) /* read a line of characters & write it out backwards */ { char c;

if ((c=getchar())!=EOLN) reverse(); putchar(c); return; }

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 to display hexadecimal equivalent of a input binary number.

#include <stdio.h> #include <conio.h> #include <math.h> int btod(int b[]); void dtoh(int s); int c; void main() { int a[5],d,i,e,f,g,s; clrscr(); printf(" What will be the length of input no.? "); scanf("%d",&c); printf("Don't exceed input from your input limit or 5 digits in any case"); printf(" Enter no.:- "); scanf("%d",&d); for (i=c-1;i>=0;--i) { e = pow(10,i); a[i] = d/e; d = d%e; } s=btod(a); dtoh(s); printf("

HAVE A NICE DAY! BYE."); getch(); } int btod(int b[]) { int k,s=0,i,n; n=*b; for (i=c-1;i>=0;--i) { if (i>0) *b=*(b+i); else *b=n; if (*b==1) { k=pow(2,i); s=s+k; } } return(s); }

void dtoh(int s) { int b,c=0,a[5],i=0; b=s; while (b>15) { a[i]=b%16; b=b/16; i++; c++; } a[i]=b; printf(" Its hexadecimal equivalent is for (i=c;i>=0;--i) { if (a[i]==10) printf("A"); else if (a[i]==11) printf("B"); else if (a[i]==12) printf("C"); else if (a[i]==13) printf("D"); else if (a[i]==14) printf("E"); else if (a[i]==15) printf("F"); else printf("%d",a[i]); } return; }

");

Program to display the decimal equivalent of an input hexadecimal number. (Hexadecimal to Decimal)

#include <stdio.h> #include <stdlib.h> #include <conio.h> #include <math.h> void htod(char b[]); int c; void main() { char a[3]; int j; clrscr(); printf("How many digits in input? scanf("%d",&c); printf("

");

Digits of your i/p no. must not exceed entered limit or 3(max)."); printf(" Enter a hexadecimal no."); printf(" No. will be accepted if you press ENTER after inputting no."); printf(" NUMBER:- "); scanf("%s",a); htod (a); getch(); } void htod(char b[]) { int i,e,n,k=0,t=0,s=0; n=*b; for (i=c-1;i>=0;--i) { e =(c-1)-i; if (i>0) *b=*(b+i); else *b=n; k = pow(16,e); if (*b =='1') { t=1*k; s=s+t; } else if (*b=='2') { t=2*k; s=s+t; } else if (*b=='3') { t=3*k; s=s+t; } else if (*b=='4') { t=4*k; s=s+t; } else if (*b=='5') { t=5*k; s=s+t; } else if (*b=='6') { t=6*k; s=s+t; } else if (*b=='7') { t=7*k; s=s+t; } else if (*b=='8') { t=8*k;

s=s+t; } else if (*b=='9') { t=9*k; s=s+t; } else if (*b=='a'||*b=='A') { t=10*k; s=s+t; } else if (*b=='b'||*b=='B') { t=11*k; s=s+t; } else if (*b=='c'||*b=='C') { t=12*k; s=s+t; } else if (*b=='d'||*b=='D') { t=13*k; s=s+t; } else if (*b=='e'||*b=='E') { t=14*k; s=s+t; } else if (*b=='f'||*b=='F') { t=15*k; s=s+t; } else if (*b=='0') { t=0; s=s+t; } else { printf(" ERROR"); getch(); exit(0); } } printf(" Its decimal equivalent is %d.",s); return; }

Program to compute difference between two dates.

#include<stdio.h> #include<math.h> void main() { int day1,mon1,year1,day2,mon2,year2; int ref,dd1,dd2,i; clrscr(); printf("Enter first day, month, year"); scanf("%d%d%d",&day1,&mon1,&year1); scanf("%d%d%d",&day2,&mon2,&year2); ref = year1; if(year2<year1) ref = year2; dd1=0; dd1=func1(mon1); for(i=ref;i<year1;i++) { if(i%4==0) dd1+=1; } dd1=dd1+day1+(year1-ref)*365; printf("No. of days of first date fronm the Jan 1 %d= %d",year1,dd1); /* Count for additional days due to leap years*/ dd2=0; for(i=ref;i<year2;i++) { if(i%4==0) dd2+=1; } dd2=func1(mon2)+dd2+day2+((year2-ref)*365); printf("No. of days from the reference year's first Jan = %d",dd2); printf("Therefore, diff between the two dates is %d",abs(dd2-dd1)); getch(); }

int func1(x) //x for month y for dd { int y=0; switch(x) { case 1: y=0; break; case 2: y=31; break; case 3: y=59; break; case 4: y=90; break; case 5: y=120;break; case 6: y=151; break; case 7: y=181; break; case 8: y=212; break; case 9: y=243; break; case 10:y=273; break; case 11:y=304; break; case 12:y=334; break; default: printf("Error encountered"); exit(1);

} return(y); }

Das könnte Ihnen auch gefallen