0% found this document useful (0 votes)
15 views

Example programs c

The document provides example C programs demonstrating the use of looping statements, including Fibonacci series, Pascal's triangle, and string reversal. It discusses the selection of loops based on pre-test and post-test requirements and outlines the types of loops available in C. Additionally, it includes a quiz with questions and explanations about the behavior of various C programs related to loops.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Example programs c

The document provides example C programs demonstrating the use of looping statements, including Fibonacci series, Pascal's triangle, and string reversal. It discusses the selection of loops based on pre-test and post-test requirements and outlines the types of loops available in C. Additionally, it includes a quiz with questions and explanations about the behavior of various C programs related to loops.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 28

Example programs Using Looping Statements in C

Write a program to print the FIBONACCI SERIES PASCAL TRIANGLE


upto N Numbers
#include<stdio.h>
#include<stdio.h>
void main() void main()
{ {
int n,i,f1,f2,f3; int i,j;
printf("enter the number = "); clrscr();
scanf("%d",&n); for(i=1;i<=5;i++)
f1=-1; {
f2=1; for(j=1;j<=i;j++)
for(i=0;i<=n;i++)
{
{
f3=f1+f2; printf("%d\n",i);
printf("%d\n",f3); }
f1=f2; printf("\n");
f2=f3; }
}
} Output
1
Enter the number = 5
22
0
1 333
1
2 4444
3
5
55555
PASCAL TRIANGLE PASCAL TRIANGLE
#include<stdio.h> #include<stdio.h>
void main() void main()
{ {
int i,j; int i,j,c=1;
clrscr(); clrscr();
for(i=1;i<=5;i++) for(i=1;i<=5;i++)
{ {
for(j=1;j<=i;j++) for(j=1;j<=i;j++)
{ {
printf("%d\n",j); printf("%d\n",c);
} c++;
printf("\n"); }
} printf("\n");
}

Output Output
1 1
12 23
123 456
1234 7 8 9 10
12345 11 12 13 14 15
PASCAL TRIANGLE to print *
#include<stdio.h> PASCAL TRIANGLE
void main() #include<stdio.h>
{ void main()
int i,j; {
clrscr(); int i,j;
for(i=1;i<=5;i++) clrscr();
{ for(i=0;i<5;i++)
for(j=1;j<=i;j++) {
{ for(j=i;j>0;j--)
printf(“*"); {
} printf("%d\n",j);
printf("\n"); }
} printf("\n");
}
Output
Output
* 1
* 21
** 321
** * 4321
** * * 54321

* * * **
To print the word in reverse
#include<stdio.h>
#include<conio.h>
#define size 10
void main()
{ Enter Any String : raja
char name[size+1];
The Given String is raja
int i=1; The Reversed String is ajar
clrscr();
printf("\n Enter Any String");
scanf("%s",name);
printf("\n The Given string is %s\n",name);
for(i=0;name[i]!='\0';i++);
printf("\n\n The Reversed String is");
for(i=size-1;i>=0;i--)
{
printf("%c",name[i]);
}
getch();
}
EXERCISES
Write a program to print the following Outputs using for while and do..while loops?
Which loop to Select?

• Selection of a loop is always a tough task for a


programmer, to select a loop do the following
steps:
• Analyze the problem and check whether it
requires a pre-test or a post-test loop.
• If pre-test is required, use a while or for a
loop.
• If post-test is required, use a do-while loop.
Summary

• Looping is one of the key concepts on any


programming language.
• It executes a block of statements number of times
until the condition becomes false.
• Loops are of 2 types: entry-controlled and exit-
controlled.
• 'C' programming provides us 1) while 2) do-while and
3) for loop.
• For and while loop is entry-controlled loops.
• Do-while is an exit-controlled loop.
Quiz in C Looping statements
1.What will be the output of the C program?
#include<stdio.h>
int main()
{
for(;;)
{
printf("%d ", 10);
}
return 0;
}
A. Compilation error
B. 10
C. Program never ends
D. None of the above
Option: C
Explanation
Yes, it is infinite for loop.
2.What will be the output of the C program?
#include<stdio.h>
int main()
{ int i;
for(i = 0;i<=3;i++);
printf("%d", i);
return 0;
}
A. Compilation error
B. 1 2 3
C. 4
D. 0 1 2 3
Option: C
Explanation
Although for loop terminated within itself, it
iterates for 4 times and 4 alone is outputted
3.What will be the output of the C program?
#include<stdio.h>
int main()
{
int fun = { printf("C for loop ") };
int x = 5;
for(x=0;x<=fun;x++)
{
printf("%x ", x);
}
return 0;
}

A. 0 1 2 3 4 5 6 7 8 9
B. C for loop0 1 2 3 4 5 6 7 8 9 a
C. 0 1 2 3 4 5 6 7 8 9 a
D. None of the above
Option: B
Explanation
for loop executes first.
for(x=0;x<=fun;x++)
ie) for(x=0; x=printf("C for loop"); x++)
Now printf in conditional place of for loop prints C for
loop and the count in that string is replace in conditional
place of for loop
ie) for(x=0; x<=10;x++)
thus outputted C for loop 0 1 2 3 4 5 6 7 8 9 a
here a represent 10 in hexadecimal.
4.What will be the output of the C program?
#include<stdio.h>
int main()
{
static int i;
for(i++;++i;i++)
{
printf("%d ", i);
if(i == 6)
break;
}
return 0;
}
A. 2 4
B. No output
C. 2 4 6
D. Program never ends
Option: C
Explanation
By Default, the values of static int variable is set
to zero by C compiler. i.e) i = 0;
Clearly, the for loop in this program is infinite
for loop, ie) At first iteration the program looks
like for( 0;2;2 ) that is it 2 4 6 8 10 .. and so on.
By this program we breaks when i = 6.
5.What will be the output of the C program?
#include<stdio.h>
int main()
{
char i = 0;
for(;i++;printf("%d", i)) ;
printf("%d",i);
return 0;
}
A. Compilation error
B. 0
C. 1
D. Program never ends
Option: C
Explanation
Here, for(; i++; printf("%d", i)) ;
i.e) for(;0;printf("%d", i));//condition fails for
the first time itself
thus the loop terminated, but the value of i is
incremented to 1.
Now printf("%d",i); printf the value 1 to the
output.
6.What will be the output of the C program?
#include<stdio.h>
int main()
{
int i = 0;
for(i = 0;i == 0;i++)
{
printf("%d", i);
}
return 0;
}
A. 0
B. Nothing prints
C. 1
D. None of the above
Option: A
Explanation
i == 0 condition in for loop is true for one time
and its prints the value of i i.e) 0 is outputted.
7.What will be the output of the C program?
#include<stdio.h>
int main()
{
int i;
for(i = 0;i<0,5;i++)
printf("%d\n",i);
return 0;
}
A. 1 3
B. Program never ends
C. 1 3 5
D. None of the above
Option: B
Explanation
Here we do not use any brackets in conditional
place of for loop, thus compiler will consider
that two conditions are given in this place,
thus
1. i< 0 // fails for the first iteration itself
1. 5 // which is infinite as 5 never becomes 0.
8.What will be the output of the C program?
#include<stdio.h>
int main()
{
int i, j;
for(i = 0, j=5;i<j;i++,j--)
printf("%d %d ",i, j);
return 0;
}
A. 0 1 2 5 4 3
B. Compilation error
C. 0 5 1 4 2 3
D. Runtime error
Option: C
Explanation
In this for loop there is two initialization, one
condition and 2 incrementation
Thus i prints from 0 and j prints from 5.
With respect to condition 0 5 1 4 2 3 is
outputted.
9.What will be the output of the C program?
#include<stdio.h>
int main()
{
int i;
while(0, i < 4)
{
printf("Loop ");
i++;
}
return 0;
}
A. Prints Nothing
B. Infinit Loop
C. Loop Loop Loop Loop
D. Loop Loop Loop Loop Loop
Option: C
Explanation
The condition (0, i < 4) is in the format
(expression1, expression2), here
expression1(0) goes false, though expression2
will get executed until expression2 is false.
10.What will be the output of the C program?
#include<stdio.h>
int main()
{
int i == 4, j == 7;
while(++i < --j)
printf("Loop")
return 0;
}
A. Loop
B. Loop Loop
C. Loop Loop Loop
D. Infinite Loop
Option: A
Explanation
The while(--i == --j) is (5 < 6), so the statement
inside the loop will executes once, if we go for
antother iteration the condition will be wrong.

You might also like