C – C Problems and Solutions Part 2

0
6

Program to calculate Factorial using static storage class.

`#include<stdio.h>#include<conio.h>void main(){ int x,i; long int f;  long int factstat(int i); printf("Input number:"); scanf("%d",&x) for(i=1; i<=x; i++)  f=factstat(i); printf("n Factorial=%d",f);}long int factstat(int i){ static long int f=1;  f*=i;  return(f);}`

Program to calculate Area of Triangle, Circle, Rectangle, square.

`#include<stdio.h>#include<conio.h>#include<math.h>void main(){ int choice; clrscr(); printf("1. For are of triangle"); printf("2. For area of circle"); printf("3. For area of ractangle"); printf("4. For are of square"); printf(" Enter your choice:n"); scanf("%d",choice); { case 1:   {    int b,h,a;    printf("Enter base and height:");   scanf("%d%d",&b,&h);   a=(b*h)/2;   printf("The aree of triangle=%d",a);   break;   } case 2:   {    float r,a;   printf("Enter readius");   scanf("%g",&r);   a=3.14594*r*r;   printf("n area of circle=%g",a);   break;  } case 3:   {   int l,b,a;   printf("Enter lenght and breadth:");   scanf("%d%d",&l, &b);   a=l*b;   printf("Area of rectangle:%dn",a);   break;   } case 4:   {   int a,b;    printf("Enter lenght");   scanf("%d",&a);   b=a*a;   printf("The are of square:%d",b);   break;  } default:  {   printf("Wrong Choice"); } getch();}`

Program to calculate square root.

`#include<stdio.h>#include<conio.h>#include<math.h>void main(){ int num;  int squreroot(int num); printf("Input Number:"); scanf("%d",&num); squreroot(num); getch();}int squreroot(int num){ printf("squreroot: %f",sqrt(num)); return 0; }`

Program to generate below triangle.

1
23
345
4567
56789
67891011
78910111213

`#include<stdio.h>#include<conio.h>void main(){ int a,b,c,e=1; clrscr(); for(a=1; a<=7; a++,e=e+1) {  for(b=1, c=e; b<=a; b++, c=c+1)   printf("%d",c);   printf("n"); } getch();}`

Program to calculate Greater Numbers.

`#include<stdio.h> #include<conio.h>int max(int, int);void main(){ int a,b,i,j,m,n; printf("n Enter two numbers:"); scanf("%d%d",&a,&b); printf("nGreater is: %d",max(a,b);  printf("n Enter two numbers:"); scanf("%d%d",&i,&j); printf("nGreater is: %d",max(i,j);   printf("n Enter two numbers:"); scanf("%d%d",&m,&n); printf("nGreater is: %d",max(m,n); getch();}int max(int x, int y){ if(x>y)  return x;  else   return y; }`

Program to calculate factorial using function.

`#include<stdio.h>#include<conio.h>long int factorial(int);void main(){ int n; long int fact; printf("Enter number:"); scanf("%d",&n); fact=factorial(n); printf("Factorial number of %d=%ld",fact); getch();}long int factorial(int n){ long int fact=1;  for(i=1; i<n; i++)  fact=fact*i; return fact;}`

Program to calculate factorial using recursion.

`#include<stdio.h>#include<conio.h>void main(){ int n;  int factorial(int); printf("Enter a number:"); scanf("The factorial of %d is %d", n,factorial(n)); getch();}int factorial(int n){ if(n<1) return(1); else  return(n*factorial(n-1));}`

Program to calculate factorial using external storage class.

`#include<stdio.h>#include<conio.h>int x; long int f=1;void main(){ void fact(void) printf("n Input number:"); scanf("%d",&n); factect(); printf("n Factorial=%ld",f); getch();}void factect(void){ int i,x;  for(i=1; i<=x; i++) {  f*=1; }} `

Program to change character into upper case.

```#include<stdio.h>#include<conio.h>void upper(char x);void main(){ char x;  clrscr(); printf("Enter character:"); scanf("%c",x); upper(x); getch();}void upper(char x){ if(x>='a' && x<='z') { printf("n The Entered character:%c",n); x=x-32; printf("n Entered Character In Uppercase:%c",x);
} else {   printf("Wrong input"); }}```

a

Program to calculate the character into upper case.

`#include<stdio.h>#include<conio.h>void upper(char x);void main(){ char x;  clrscr(); printf("Enter character:"); scanf("%c",&x); upper(x); getch();}void upper(char x){ printf("Entered character in upper case:%c", toupper(x));}`