Sie sind auf Seite 1von 7

Factorial C program,Algorithm,Flowchart

Q. Write a C program to find the factorial value of a number. Also write the algorithm and draw flowchart. Ans. /*c program to find out factorial value of a number*/ #include<stdio.h> #include<conio.h> int main() { int n,i,fact=1; printf("Enter any number : "); scanf("%d", &n); for(i=1; i<=n; i++) fact = fact * i; printf("Factorial value of %d = %d",n,fact); return 0; } The output of above program would be:

Screen shot for calculate factorial value of a number C program

Algorithm for calculate factorial value of a number: [algorithm to calculate the factorial of a number] step 1. Start step 2. Read the number n step 3. [Initialize] i=1, fact=1 step 4. Repeat step 4 through 6 until i=n step 5. fact=fact*i step 6. i=i+1 step 7. Print fact step 8. Stop [process finish of calculate the factorial value of a number]

Flowchart for calculate factorial value of a number:

Figure: Flowchart for calculate factorial value of a number C program

User Define Function- Factorial

Q. Write a function fact to calculate the factorial value of any number. Ans. /*c program for make function fact to calculate factorial value of any number*/ #include<stdio.h> int fact(int ); int main()

{ int num,f; printf("Enter any number : "); scanf("%d", &num); f = fact(num); printf("Factorial value of %d is %d",num,f); return 0; } int fact(int n) { int z=1; for(; n>=1; n--) z = z * n; return(z); } The output of above program would be:

Figure: Screenshot for find factorial value using user define function C program

Flowchart for switch-case


How to write switch case flowchart? switch case statement: switch( choice ) { case 1: statement 1; break; case 2: statement 2; break; case 3: statement 3; break; case n: statement n;

(in above module n = number of cases.) switch case flowchart:

Figure: flowchart of switch case statemen

* Program to Swap Two Numbers by Using Call By Reference Method * /

#include main() { int i, j; clrscr(); printf("Please Enter the First Number in A : "); scanf("%d",&i); printf("\nPlease Enter the Second Number in B : "); scanf("%d",&j); swapr(&i,&j); /* call by reference*/ printf("A is now in B : %d",i); printf("B is now in A : %d",j); } /* call by reference function*/

swapr(int *x, int *y) { int t; t=*x; *x=*y; *y=t; }

Das könnte Ihnen auch gefallen