0% found this document useful (0 votes)
7 views4 pages

C Prog Lab Programs

Uploaded by

saleemaafsar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views4 pages

C Prog Lab Programs

Uploaded by

saleemaafsar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd

1.

Factorial using do-while loop

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]

Factorial Program

/* calculate factorial value using do while */


#include<stdio.h>
#include<conio.h>
void main()
{
long int i=1,n,fact=1; /*variable declaration */
clrscr();
printf("Enter the value of n \n");
scanf("%ld", &n);
/* do loop start */
do
{
fact*=i;
i++;
}
while(i<=n);

printf("Factorial = %ld\n",fact);
getch();
}
Flowchart

Explanation of above program

There are three variables in this program all of type long -


 n - is the number whose factorial we're calculating.
 f - is the factorial of n. It is initialized to 1 at the start of the program.
 i - is the loop variable.

First we ask the user to enter a number. Then we calculate the factorial of that number using a for loop. Our for loop runs for the
value of loop variable i = 2 to n. Inside the for loop we simply multiply each value of loop variable with previously calculated
factorial value (f) and again storing it in the same f variable for further calculations.

Suppose, n = 5. So our for loop will run for the value of i = 2, 3, 4 and 5 -
 i = 2, f = 1 - This is the first iteration. Inside the loop the value of f is updated as - f = f * i. So f = 1 * 2 = 2.
 i = 3, f = 2 - The new value of f = f * i = 2 * 3 = 6.
 i = 4, f = 6 - The new value of f = f * i = 6 * 4 = 24.
 i = 5, f = 24 - The new value of f = f * i = 24 * 5 = 120.

Tip: We could have run our for loop from i = 1 to n but by doing this we're just wasting one looping cycle and hence the
resources because in the first iteration if the value of i = 1 and f = 1 then again f = 1 * 1 = 1. As there is no change in the value
of f we can eliminate this step. Hence we're using our loop from i = 2 to n.

2. Fibonacci Series
Algorithm

Step 1: I=0, Num1=0, Num2=1


Step 2: While (I < 11)
Print Num1
Num1=Num2
Num2=Num1+Num2
I++
Step 3: End

Program

/* Fibonacci Series using while loop */ #include<stdio.h>

void main()
{
int i,X=0,Y=1;
clrscr();
printf("\nFIBONACCI SERIES < 1000 ARE :- \n");
i=0;
while(i<11)
{
printf(" %d ",X);
X=Y;
Y=X+Y;
i++;
}
getch();
}

Flow chart
3.

You might also like