Example programs c
Example programs c
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?
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.