[go: up one dir, main page]

0% found this document useful (0 votes)
29 views29 pages

Unit 6 Loop Control

Unit VI covers loop control structures in programming, detailing the types of loops (for, while, do-while), their syntax, and practical examples. It explains the purpose of loops for repeating code based on conditions, as well as concepts like nesting, break, and continue statements. Additionally, it distinguishes between finite and infinite loops, highlighting their applications in programming.

Uploaded by

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

Unit 6 Loop Control

Unit VI covers loop control structures in programming, detailing the types of loops (for, while, do-while), their syntax, and practical examples. It explains the purpose of loops for repeating code based on conditions, as well as concepts like nesting, break, and continue statements. Additionally, it distinguishes between finite and infinite loops, highlighting their applications in programming.

Uploaded by

himalbest01
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 29

Unit VI: Loop Control Structure (4)

Introduction, Need of Looping, Types of Loop Statements (for, while, do while), Nesting of
Loops, Break and Continue statement, Finite and infinite loops, Programming examples

Introduction: "A loop is an instruction that will let you iterate (repeat) code as much as you
want based on specific condition." Loop control structures are used to execute and repeat a
block of statements depending on the value of a condition. Loop may be defined as block of
statements which are repeatedly executed for a certain number of times or until a particular
condition is satisfied. When an identical task is to be performed for a number of times, then loop
is used. A loop allows executing certain block of statements repeatedly till a conditional
expression is true. When expression becomes false, the loop terminates and control passes on to
the statement following the loop. A loop consists of two segments, one is known as the control
segment and the other is the body of loop. The control statement in loop decides whether the
body is to be executed or not. There are three types
of loop structures in C language.

1. for statement or for loop


2. while statement or while loop
3. do-while statement or do-while loop
For example: Write a program to display the message " Welcome to Far West University" ten
times using loop and without loop.

a) Method 1 without using loop


#include<stdio.h>
int main()
{
printf("\n Welcome to Far West University");
printf("\n Welcome to Far West University");
printf("\n Welcome to Far West University");
printf("\n Welcome to Far West University");
printf("\n Welcome to Far West University");
printf("\n Welcome to Far West University");
printf("\n Welcome to Far West University");
printf("\n Welcome to Far West University");
printf("\n Welcome to Far West University");
printf("\n Welcome to Far West University");
return 0;
}

b. #include<stdio.h>
int main()
{
int i;
for(i=1;i<=10;i++)
printf("\n Welcome to Far West University");
return 0;
}

c. #include<stdio.h>
int main()
{
int i=1;
while(i<=10)
{
printf("\n Welcome to Faculty of Engineering");
i++;
}
return 0;
}
1. for statement or for loop: The for loop is useful to execute a statement for a number of
times. When the number of repetitions is known in advance, the use of this loop will be more
efficient. Thus, this loop is also known as definite loop. The for statement is most often used in
situations where the programmer knows in advance how many times a particular set of
statements are to be repeated. The for statement is sometimes termed a counted loop.
The general syntax of the for loop is as follows:

Syntax: for ( initialization ; condition ; increment )


[Statement body];

i=0 ; i<10 ; i++


False
True

for body

Exit
Fig: Flow chart of for loop

i. initialization: this is usually an assignment to set a loop counter variable.


ii. test-condition: determines when loop will terminate
iii. increment: defines how the loop control variable will change each time the loop is executed.
This can be decrement or empty.
iv. statement body: can be a single statement, no statement or a block statements.
At first; counter is initialized to some value (i.e. step i). Then counter variable is tested with test
condition(i.e. step ii). If test is true, the body of loop is executed (i.e. step iii). After finishing
body, the counter variable is incremented or decremented (i.e. step iv) and then again updated
counter variable is tested with test condition (i.e. step ii) . The same process is repeated as long
as the condition is true. If the result with test condition is false, the control passes outside the
loop i.e. the statement following the loop.
Example: Write a program to calculate factorial of a number.
#include<stdio.h>
int main()
{
int num, i;
long fact = 1;
printf("Enter the number whose factorial to be calculated\n");
scanf("%d", &num);
for(i=1;i<=num; i++)
fact*=i;
printf("\n The factorial of %d is %ld", num, fact);
return 0;
}
Example: Write a program that asks an integer number n and calculate sum of all natural
numbers from 1 to n.
#include<stdio.h>
int main()
{
int num, i ,sum =0;
printf("\n Enter the number \t");
scanf("%d", &num);
for(i=1;i<=num;i++)
sum+=i;
printf("\n The sum is %d ", sum);
return 0;
}
Example: Program to print the Floyd's triangle. First four rows of Floyd's triangle are as follows.
1
2 3 Example: Program to print out all numbers
4 5 6 from 1 to 100 and calculate their sum
7 8 9 10
#include<stdio.h>
#include<stdio.h> int main()
int main()
{
{
int n, i, j, a= 1; int x, sum =0;
printf(" Enter the number of row Floyd's triangle"); for(x=1; x<=100; x++)
scanf("%d", &n);
for(i=1;i<=n;i++) {
{ printf("%d\t", x);
for(j=1; j<=i; j++)
sum+= x;
{
printf(" %d\t", a); }
a++; printf("\ Sum is =%d", sum);
}
return 0;
printf("\n");
} }
return 0;
}

The While Loop

The while loop is used to repeat a section of code an unknown number of times until a specific
condition is met. While loop is a pre-test loop, it first test a specified conditional expression and
as long as the conditional expression is true, loop body statements will be executed.

Syntax Example
while(test condition) int i=0;
while(i<10)
{
{
body of loop printf("%d", i);
} i++;
}
The test condition is evaluated first and if the condition is true, then the body of loop is executed.
After execution of body of loop once, the test condition is again evaluated to determine the body
of loop is to be executed for next time or not. If test condition produces true value, the body is
executed once again. The process is repeated execution of the body continues until the test
condition finally becomes false and then the control is transferred out of the loop. On exit, the
program continues with the statement immediately after the body of the loop.

Start
Fig: Flow chart of while loop

Initialize

test

body of while loop

increment / decrement
variable End

Example: Program to sum all integers from 1 to 100 using while loop
#include<stdio.h>
int main()
{
int sum =0, i=1;
while(i<=100)
{
sum+=i;
i++;
}
printf("Sum is %d\n", sum);
return 0;
}
Example: Program to find the sum and average of the marks of five subjects using while loop
#include<stdio.h>
int main()
{
int marks, total =0, i=1;
float average;
while(i<=5)
{
printf("\n Enter marks of %d subject:", i);
scanf("%d", &marks);
total =total + marks;
i++;
}
average = (float)total/5;
printf("\n The sum =%d\t and average of marks of five subjects is %.2f", total, average);
return 0;
}
Example: Program to find the sum of digits of any num
#include<stdio.h>
int main()
{
int n, sum=0, rem;
printf(" Enter the number:");
scanf("%d", &n);
while(n>0)
{
rem = n%10;
sum+=rem;
n/=10;
}
printf("Sum of digits = %d\n", sum);
return 0;
}
Example: Write a program to calculate factorial of a number using while loop
#include<stdio.h>
int main()
{
int num, i = 1;
long fact =1;
printf("Enter the number whose factorial is to be calculated\n");
scanf("%d", &num);
while(i<=num)
{
fact*=i;
i++;
}
printf(" The factorial is %ld", fact);
return 0;
}

Example: Write a program to add two numbers and display their sum. The program must ask
next two numbers and add till user wants. (i.e. Program would terminate if user doesn't want to
add another numbers).

#include<stdio.h>
int main()
{
int a,b, sum;
char nextTime;
nextTime = 'y';
while(nextTime=='y')
{
printf("\n Enter two number to be added:\t");
scanf("%d%d", &a, &b);
sum = a+b;
printf("\n The sum is : \t%d", sum);
printf("\n\n Do you want to add another two numbers?");
printf("\n Press y for yes and other characters for exit\t");
scanf(" %c", &nextTime); /* Be careful blank space must be given before %c */
}
return 0;
}
The do-while loop
A do...while loop is similar to a while loop, except the fact that it is guaranteed to execute at
least one time. Only then, the test expression is evaluated.

Syntax
The syntax of a do...while loop in C programming language
is −
do {
statement(s);
} while ( condition );

How do...while loop works?

 The body of do...while loop is executed once. Only then,


the test expression is evaluated.
 If test expression is true, the body of the loop is executed
again and test expression is evaluated once more.
 This process goes on until test expression becomes false.
 If test expression is false, the loop ends.

Example:

// Program to add numbers until the user enters zero


#include <stdio.h>
int main() {
double number, sum = 0;
// the body of the loop is executed at least once
do {
printf("Enter a number: ");
scanf("%lf", &number);
sum += number;
}
while(number != 0);
printf("Sum = %.2lf",sum);
return 0;
}
Example: //C program to print the table of any number from 1 to 10.

#include<stdio.h>
int main()
{
int num, i=1;
printf("Enter the number to generate table from 1 to 10");
scanf("%d", &num);
do
{
printf("%d * %d = %d\n",num, i,num*i);
i++;
}while(i<=10);
return 0;
}

Example: // Program to read price and summing the given price until inputting 0 price.
#include<stdio.h>
int main()
{
float price, total;
total =0;
do
{
printf("Enter price (0 to end): ");
scanf("%f", &price);
total+=price;
}while(price>0);
printf("Total: %.2f", total);
return 0;
}
Difference between while loop and do-while loop

while loop do-while loop

Nesting of Loops: Nested loop means a loop statement inside another loop statement. That is
why nested loops are also called as "loop inside loop". Any type of loop can be nested inside
any other type of loop.
Syntax for Nested For loop:

for ( initialization; condition; increment )


{
for ( initialization; condition; increment )
{
// statement of inside loop
}

// statement of outer loop


}
Syntax for Nested While loop:
while(condition)
{

while(condition)
{

// statement of inside loop


}

// statement of outer loop


}

Syntax for Nested Do-While loop:


do
{
do
{
// statement of inside loop
}while(condition);

// statement of outer loop


}while(condition);
Note: There is no rule that a loop must be nested inside its own type. In fact, there can be any
type of loop nested inside any type and to any level. We can loop different kinds of loops within
each other to form nested loops.
Example: Program to print pattern as given
*****
*****
*****
*****
*****
#include <stdio.h>
int main()
{
int i, j;
for (i=0; i<5; i++) {
for (j=0; j<5; j++)
{
printf(" * ");
}
printf("\n");
}
return 0;
}
Example: Program to print pattern as given
*
**
***
****
*****
#include <stdio.h>
int main() {
int i, j;
for (i=0; i<5; i++) {
for (j=0; j<=i; j++) {
printf(" * ");
}
printf("\n");
}
return 0;
}
Example: Program to print pattern as given
*
**
***
****
*****
#include <stdio.h>
int main()
{
int i,j,k;
for (i=1; i<=5; i++)
{
for (j=5; j>=i; j--)
{
printf(" ");
}
for (k=1; k<=i; k++)
{
printf("*");
}
printf("\n");
}
return 0;
}
Break and Continue statement:
The break statement in C
In any loop break is used to jump out of loop skipping the code below it without caring about the
test condition.
It interrupts the flow of the program by breaking the loop and continues the execution of
code which is outside the loop.
The common use of break statement is in switch case where it is used to skip remaining part of
the code.

In while loop
while (test_condition)
{
statement1;
if (condition )
break;
statement2;
}

In do…while loop
do
{
statement1;
if (condition)
break;
statement2;
}while (test_condition);

In for loop
for (int-exp; test-exp; update-exp)
{
statement1;
if (condition)
break;
statement2;
}
Now in above structure, if test_condition is true then the statement1 will be executed and again
if the condition is true then the program will encounter break statement which will cause the flow
of execution to jump out of loop and statement 2 below if statement will be skipped.
#include<stdio.h>
int main()
{
int i;

for(i = 1; i < 10 ; i++)


{
if(i==5)
{
break;
}
printf("Value of i = %d\n", i);
}
return 0;
}
Continue statement
The continue statement is used to prematurely end the current iteration and move on the to the
next iteration. When the continue statement is encountered in a loop, all the statements after
the continue statement are omitted and the loop continues with the next iteration
.
The following program prints all the numbers between 0 to 10 which are not divisible
by 4 using the continue statement.
#include<stdio.h>
int main()
{
int i;

for(i = 0; i < 10; i++)


{
if( i % 4 == 0 )
{
continue;
}
printf("%d\n", i);
}

return 0;}
Difference Between the break and continue statement:
Break Statement Continue Statement

The Break statement is used to exit from the The continue statement is not used to exit
loop constructs. from the loop constructs.

The break statement is usually used with the The continue statement is not used with the
switch statement, and it can also use it within switch statement, but it can be used within the
the while loop, do-while loop, or the for-loop. while loop, do-while loop, or for-loop.

When a break statement is encountered then the When the continue statement is encountered
control is exited from the loop construct then the control automatically passed from the
immediately. beginning of the loop statement.

Syntax: Syntax:
break; continue;

Finite and infinite loops:


An infinite loop is a looping construct that does not terminate the loop and executes the loop
forever. It is also called an indefinite loop or an endless loop. It either produces a continuous
output or no output.An infinite loop is useful for those applications that accept the user input and
generate the output continuously until the user exits from the application manually. In the
following situations, this type of loop can be used:
o All the operating systems run in an infinite loop as it does not exist after performing some
task. It comes out of an infinite loop only when the user manually shuts down the system.
o All the servers run in an infinite loop as the server responds to all the client requests. It
comes out of an indefinite loop only when the administrator shuts down the server
manually.
o All the games also run in an infinite loop. The game will accept the user requests until the
user exits from the game.
#include <stdio.h>
int main()
{
for(;;)
{
printf("This is infinite loop");
}
return 0;
}
#include <stdio.h>
int main()
{
int i=0;
while(1)
{
i++;
printf("i is :%d",i);
}
return 0;
}

Finite loop Infinite loop

It iterates for a finite number of iterations. It continues iterating indefinitely.


1. /*C program to check whether a character is VOWEL or CONSONANT using switch.*/
#include <stdio.h>
int main()
{
charch;
printf("Enter a character: ");
scanf("%c",&ch);
Lab 3

if((ch>='A' &&ch<='Z') || (ch>='a' &&ch<='z'))


{
switch(ch)
{
case 'A':
case 'E':
Programming

case 'I':
case 'O':
case 'U':
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
printf("%c is a VOWEL.\n",ch);
break;
C

default:
printf("%c is a CONSONANT.\n",ch);
}
}
else
{
printf("%c is not an alphabet.\n",ch);
}
return 0;
}

2. /*C program to read weekday number and print weekday name using switch.*/
#include <stdio.h>
int main()
{
intwDay;
printf("Enter weekday number (0-6): ");
scanf("%d",&wDay);
switch(wDay)
{
case 0:
printf("Sunday");
break;
case 1:
printf("Monday");
break;
case 2:
printf("Tuesday");
break;
case 3:
printf("Wednesday");
break;
case 4:
printf("Thursday");
break;
case 5:
printf("Friday");
break;
case 6:
printf("Saturday");
break;
default:
printf("Invalid weekday number.");
}
printf("\n");
return 0;
}
3. /*C program to read gender (M/F) and print corresponding gender using switch.*/
#include <stdio.h>
int main()
{
char gender;
printf("Enter gender (M/m or F/f): ");
scanf("%c",&gender);
switch(gender)
{
case 'M':
case 'm':
printf("Male.");
break;
case 'F':
case 'f':
printf("Female.");
break;
default:
printf("Unspecified Gender.");
}
printf("\n");
return 0; }
4. //Simple program to show the use of for loop
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i;
printf("enter the number:");
{
for(i=1;i<=5;i++)
printf("\n%d:School of Engineering",i);
}
return 0; }
5. // Program to find factorial of a number using while
#include <stdio.h>
int main()
{
int number;
int factorial;
printf("Enter an integer: ");
scanf("%d",&number);
factorial = 1;
while (number > 0)
{
factorial *= number; // factorial = factorial*number;
--number;
}
printf("Factorial= %d", factorial);
return 0;
}
6. // Program to add numbers until user enters zero using while loop
#include <stdio.h>
int main()
{
float number, sum = 0;
do
{
printf("Enter a number: ");
scanf("%f", &number);
sum += number;
}
while(number != 0.0);
printf("Sum = %.2f",sum);
return 0;
}
7. //Sum of Natural Numbers Usingfor Loop
#include <stdio.h>
int main()
{
int n, i, sum = 0;
printf("Enter a positive integer: ");
scanf("%d",&n);
for(i=1; i <= n; ++i)
{
sum += i; // sum = sum+i;
}
printf("Sum = %d",sum);
return 0;
}
8. //Sum of Natural Numbers Using whileLoop

#include<stdio.h>
1. Program to print number pattern
int main()
2 3
{
45 6
int n, i, sum =0;
7 8 9 10
printf("Enter a positive integer: "); #include <stdio.h>
scanf("%d",&n); int main()
i =1; {
while( i <=n ) int i, j, rows, num=0;
printf("Enter number of rows: ");
{
scanf("%d",&rows);
sum+= i; for(i=1; i<=rows; ++i)
++i; {
} for(j=1; j<=i; ++j)
{
printf("Sum = %d",sum);
num++;
return0; printf("%d ",num);
} }
printf("\n");
}
return 0;
9. // program to print the number with base and power using whileloop
}
#include <stdio.h>
int main()
{
int number=1;
intpower,base, product;
printf("enter the base number");
scanf("%d", &base);
printf("enter the power value");
scanf("%d",&power);
product=base;
while(number<power)
{
product*=base;
number++;
}
printf("%d power %d is %d\n",base,power,product);
return 0;
}
1
10. Program to print number pattern
#include <stdio.h>
12
int main() 123
{ 1234
int i, j, rows;
printf("Enter number of rows: ");
scanf("%d",&rows);
for(i=1; i<=rows; ++i)
{
for(j=1; j<=i; ++j)
{
printf("%d ",j);(//in the same program if we replace j by i then pattern will be)
}
printf("\n"); 1
} 2 2
return 0; } 3 3 3
4 4 4 4
11. Program to print the pattern *
#include <stdio.h> **
int main() ***
{ ****
int i, j, rows;
printf("Enter number of rows: ");
scanf("%d",&rows);
for(i=1; i<=rows; ++i)
{
for(j=1; j<=i; ++j)
{
printf("* ");
}
printf("\n");
}
return 0;
}
12. Program for multiplication Table Up to 10 using for loop
#include <stdio.h>
int main()
{
int n, i;
printf("Enter an integer: ");
scanf("%d",&n);
for(i=1; i<=10; ++i)
{
printf("%d * %d = %d \n", n, i, n*i);
}
return 0;
}

13. Multiplication Table Up to a range (entered by user)using for loop


#include <stdio.h>
int main()
{
int n, i, range;
printf("Enter an integer: ");
scanf("%d",&n);
printf("Enter the range: ");
scanf("%d", &range);
for(i=1; i <= range; ++i)
{
printf("%d * %d = %d \n", n, i, n*i);
}
return 0;
}
13. HCF Using for loop and if Statement
#include <stdio.h>
int main()
{
int n1, n2, i,hcf;
printf("Enter two integers: ");
scanf("%d %d", &n1, &n2);
for(i=1; i <= n1 && i <= n2; ++i)
{
// Checks if i is factor of both integers
if(n1%i==0 && n2%i==0)
hcf = i;
}
printf("H.C.F of %d and %d is %d", n1, n2,hcf);
return 0;
}

14. LCM using for loop


#include <stdio.h>
int main()
{
int n1, n2, i, gcd, lcm;
printf("Enter two positive integers: ");
scanf("%d %d",&n1,&n2);
for(i=1; i <= n1 && i <= n2; ++i)
{
// Checks if i is factor of both integers
if(n1%i==0 && n2%i==0)
gcd = i;
}
lcm = (n1*n2)/gcd; // greatest common divisor(gcd)
printf("The LCM of two numbers %d and %d is %d.", n1, n2, lcm);
return 0; }
15. Simple program to use do while loop
#include <stdio.h>
int main()
{
int j=0;
do
{
printf("Value of variable j is: %d\n", j);
j++;
}while (j<=8);
return 0; }
16. C program to print the table of 5 from 1 to 10 usingdo while loop
#include<stdio.h>
int main()
{
int i=1;
do
{
printf("5 * %d = %d\n",i,5*i);
i++;
}
while(i<=10);
return 0;}
17. Factors of a Positive Integer using for loop
#include <stdio.h>
int main()
{
int number, i;
printf("Enter a positive integer: ");
scanf("%d",&number);
printf("Factors of %d are: ", number);
for(i=1; i <= number; ++i)
{
if (number%i == 0)
{
printf("%d ",i);
}
}
return 0;}
1
18. Program to print pattern using for loop
#include <stdio.h> 23
int main()
{ 456
int rows, i, j, number= 1;
printf("Enter number of rows: "); 7 89 10
scanf("%d",&rows);
for(i=1; i <= rows; i++)
{
for(j=1; j <= i; ++j){
printf("%d ", number);
++number;
}
printf("\n");
}
return 0; }

19. Program to print given symbol pattern


$$$$$$$$$$$
#include <stdio.h>
$$$$$$$$$$$
int main()
{ $$$$$$$$$$$
int i, j; $$$$$$$$$$$
for(i=0; i<=5; i++) $$$$$$$$$$$
{ $$$$$$$$$$$
for(j=0 ;j<=10 ; j++)
{
printf("$");
}
printf("\n");
}
return 0;
} 1 2 3 4 5
20. Program to produce the given table values 2 3 4 5 6
#include <stdio.h> 3 4 5 6 7
int main() 4 5 6 7 8
{ 5 6 7 8 9
int j, k;
for(j=1; j<=5; j++)
{
for(k=j; k<j+5; k++)
{
printf("%d\t", k);
}
printf("\n");
}
return 0;
}
21. Program to demonstrate the execution of loop to generate square of number for unknown number
#include<stdio.h>
int main()
{
charch;
intnum;
do
{
printf("Enter a number");
scanf("%d" , &num);
printf("square of %d is %d", num, num*num);
printf("\n Want to enter another number y/n :");
scanf("%c", &ch);
}
while(ch != 'n');
return 0;
}

22. Program to display prime numbers between 1 to 100


#include <stdio.h>
int main()
{
inti,j, flag;
printf("\n Prime numbers between 1 and 100 are : ");
for (i=1; i<=100; i++)
{
for (j=2; j<=i/2; j++)
{
if (i%j == 0)
flag =1;
}
if (flag ==0)
printf("\t%d", i);
flag = 0;
}
return 0;
}

End of lab 3
Some important programs:
1. Program to check whether the given number is palindrome or not
(Palindrome means the number is same from forward and reverse view ex. 121,363etc)
#include <stdio.h>
int main()
{
int n, reverse=0,temp;
printf("Enter the number to check");
scanf("%d",&n);
temp=n;
while(temp!=0)
{
reverse=reverse*10;
reverse=reverse+temp%10;
temp=temp/10;
}
if(n==reverse)
{
printf("the number given by you %d is palindrome and the reverse is %d",n,reverse);
}
else
{
printf("the number given by you %d is not palindrome and the reverse is %d",n,reverse);
}
return 0; }
2. What is Armstrong number?
A number is said to Armstrong number if the sum of the cube of its digit is same as number.
example 153,371 etc
13+53+33=153
Program to check whether the given number is Armstrong number or not
#include <stdio.h>
int main()
{
intn,sum=0,temp,r;
printf("enter a number\n");
scanf("%d",&n);
temp=n;
while(temp!=0){
r=temp%10;
sum=sum + r*r*r;
temp=temp/10;
}
if(sum==n)
printf("%d is an Armstrong number",n);
else
printf("%d is not an Armstrong number",n);
return 0;}
3.Program to generate Fibonacci series up to n terms.
Fibonacci series is a series in which the sum of last two digit is further number like 0 1 1
2 3 5 8...etc
here
0+1=1
1+1= 2
2+3=5
5+3=8
#include <stdio.h>
int main()
{
int num1=0,num2=1,i,n,fib;
printf("Enter the value of n:");
scanf("%d",&n);
for(i=1;i<=n-2;i++) {
fib=num1+num2;
num1=num2;
num2=fib;
printf("%d\t",fib); }
return 0;}
Or same program can be done as
#include<stdio.h>
int main()
{
int f=0,s=1,next, i, num;
printf("Enter the number of terms");
scanf("%d", &num);
printf("The first %d terms of Fibonacci series are: \n", num);
for(i=0;i<num;i++){
if(i<=1)
{
next=i;
}
else
{
next=f+s;
f=s;
s=next;
}
printf("%d\t",next);
}
return 0;
}

You might also like