

















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


















I. Simple Programs --
VIII. Programs using Files --
#include #include main() {int a,b,c; float d,p,q; clrscr(); printf("valus of a,b,c?"); scanf("%d%d%d",&a,&b,&c); d=((bb)-(4ac)); if(d>0) {printf("real"); p=-b+sqrt(d)/2a; q=-b-sqrt(d)/2a; printf("the roots are %f %f",p,q);} else if(d<0) printf("imaginary"); else {printf("real and equal"); p=((-b)+sqrt(d))/(2a)); q=((-b)-sqrt(d))/(2*a)); printf("the roots are %f %f",p,q);} }
II. Programs using Loops
printf("Answer = %d", value); }
printf("\nThe string in lower case is->%s",str); }
if(n1%i==0 && n2%i==0){ gcd = i ; break; } } lcm = prod / gcd ; printf("\nThe GCD is : %d", gcd) ; printf("\n\nThe LCM is : %d", lcm); }
{float sum,term,xd,x; int i; printf("Enter x in degree:"); scanf("%f",&xd); x=(xd3.141552654)/180.0; sum=0; term=x; for(i=2;fabs(term)>0.000001;i++) {sum+=term; term=-termxx/((2i-1)(2i-2));} printf("Sin (%f)=%f",xd,sum); }
#include void main() { int i,j,k=1; int range; printf("Enter the range: "); scanf("%d",&range); printf("FLOYD'S TRIANGLE : n \nโ); for(i=1;i<=range;i++) { for(j=1;j<=i;j++,k++) printf("%d ",k); printf(โ\n"); } } FLOYD'S TRIANGLE : for range= 1 2 3 4 5 6 7 8 9 10
III. Programs using Arrays
{int n,fib[25]; scanf("%d",&n); fib[0]=0; fib[1]=1; for(i=2;i<=n;i++) fib[i]=fib[i-2]+fib[i-1]; for(i=0;i<=n;i++) printf("%d\n",fib[i]); }
for(i = 0 ;i< n ;i++) printf(โ\n %d",a[i]); }
scanf("%d",&b[i][j]); for(i=0;i<=2;i++) for(j=0;j<=2;j++) { sum = 0; for(k=0;k<=2;k++) sum = sum + a[i][k] * b[k][j]; c[i][j]=sum; } printf(โ\nMultiplication Of Two Matrices : \nโ); for(i=0;i<3;i++) { for(j=0;j<3;j++) printf(" %d ",c[i][j]); printf(โ\n);} }
printf("Enter a number: "); scanf("%d",&num); factorial = findFactorial(num); printf("Factorial of %d is: %d",num,factorial); return 0; } int findFactorial(int num) { int i,f=1; for(i=1;i<=num;i++) f=f*i; return f; }
for(k=0;k< n;k++) printf("%5d",a[k]); } }
if ( c=='o' || c=='O') { printf("Enter a binary number: "); scanf("%d",&n); printf("%d in binary = %d in octal", n, binary_octal(n)); } if ( c=='b' || c=='B') { printf("Enter a octal number: "); scanf("%d",&n); printf("%d in octal = %d in binary",n, octal_binary(n)); } } int binary_octal(int n) { int octal=0, decimal=0, i=0; while(n!=0) { decimal+=(n%10)pow(2,i); ++i; n/=10; } i=1; while (decimal!=0) { octal+=(decimal%8)i; decimal/=8; i=10; } return octal; } int octal_binary(int n) { int decimal=0, binary=0, i=0; while (n!=0) { decimal+=(n%10)pow(8,i); ++i; n/=10; } i=1; while(decimal!=0) { binary+=(decimal%2)i; decimal/=2; i=10; } return binary; }
printf("\n%d",st[i]);} } int peep(int st[]) {if (top== -1) {printf("stack is empty"); return (-1);} else return(st[top]); }
temp.real=n1.real+n2.real; temp.imag=n1.imag+n2.imag; return(temp); }
VI. Programs using strings
void main() { int j,i,k,c=0; char a[80]; clrscr(); printf("\nEnter main string:-\n"); gets(a); k=strlen(a); for(i=0,j=k-1;i void main() {char a[25]; int i; printf("enter your name\n"); scanf(โ%sโ,a); while(a[i]!='\0') {printf("%c=%d\n",a[i],a[i]); i++;} }
void main() { int i,j,k=0,l,ls; char a[80],b[80]; clrscr(); printf("\nEnter string1:-\n"); gets(a); printf("\nEnter string2:-\n"); gets(b); l=strlen(b); ls=strlen(a); for(i=0,j=0;(i
if(a[i]==b[j]) k=1; if(a[i]!=b[j]) {k=0; break; } } if (k==1) printf("strings are equal\n"); else {if(k==0) printf("\n\nstrings are not equal.");} getch();
}
printf(โenter string to be replacedโ); gets(pat); printf("\n enter new string to replace pattern"); gets(rep_pat); while(str[i]!='\0') { j=0;k=i;rep=0; while(str[k]==pat[j] && pat[j]!='\0') { k++; j++; } if(pat[j]=='\0') { i=k; while(rep_pat[rep]!='\0') { new_str[n]=rep_pat[rep]; rep++; n++; } } new_str[n]=str[i]; i++; n++; } printf("The String is "); puts(new_str); getch(); }
Finding vowels #include void main() { int n,i,f=0,k=0; char a[80]; clrscr(); printf("\nEnter main string:-\n"); gets(a); n=strlen(a); for(i=0;i areaperi ( radius, &area, &perimeter ) ; printf ( "\nArea = %f", area ) ; printf ( "\nPerimeter = %f", perimeter ) ; }
function pointers #include void isprime(int); void (fprime)(int); void main() {int n,i,j,c=0,k=1; fprime=isprime; scanf("%d",&n); (fprime)(n); getchar(); } void isprime(int a) {int i,fg=0; for(i=2;i void main(){ int arr[50]; int p; int i,j,k,size,t; printf("\nEnter size of the array: "); scanf("%d",& size); printf("\nEnter %d elements into the array: ",n); for(i=0;i< size;i++) scanf("%d",&arr[i]); p=arr; for(i=0;i(p+j))
{ t=*(p+i); (p+i)=(p+j); *(p+j)=t;} } } printf("\nThe array after removing duplicates is: "); for(i=0;i < size;i++) printf(" %d ",arr[i]); }
#define low 1 /high implies that position of pointer is out of word./ #define high 0 void main() {int nob,now,nod,nov,nos,pos=high; char s; nob=now=nod=nov=nos=0; printf("Enter any string:"); gets(s); while(s!='') { if(s==' ') / counting number of blank spaces. / { pos=high; ++nob; } else if(pos==high) / counting number of words. / { pos=low; ++now; } if(isdigit(s)) /* counting number of digits. / ++nod; if(isalpha(s)) /* counting number of vowels / switch(s) { case 'a': case 'e': case 'i': case 'o': case 'u': case 'A': case 'E': case 'I': case 'O': case 'U': ++nov; break; } /* counting number of special characters / if(!isdigit(s)&&!isalpha(*s)) ++nos; s++;} printf(โ\nNumber of words %d",now); printf(โ\nNumber of spaces %d",nob); printf(โ\nNumber of vowels %d",nov); printf(โ\nNumber of digits %d",nod); printf(โ\nNumber of special characters %d",nos); }
#include int string_ln(char*); void main() { char str[20];
int l; printf("Enter any string: \nโ); gets(str); l=string_ln(str); printf("The length of the given string %s is : %d",str,l); } int string_ln(charp) / p=&str[0] / { int count=0; while(p!='\0') { count++; p++; } return count; }