C Programming - Control Instructions - Discussion
Discussion Forum : Control Instructions - Find Output of Program (Q.No. 19)
19.
What will be the output of the program?
#include<stdio.h>
int main()
{
char j=1;
while(j < 5)
{
printf("%d, ", j);
j = j+1;
}
printf("\n");
return 0;
}
Discussion:
24 comments Page 2 of 3.
Hammad said:
1 decade ago
j=j+1 is same as j++ post increment so
If(1<5)
printf("%d,",j);
j++;
It keep on increasing unless it encounters the condition 4<5 when it become 5<5 it fails then ans is 1,2,3,4
If(1<5)
printf("%d,",j);
j++;
It keep on increasing unless it encounters the condition 4<5 when it become 5<5 it fails then ans is 1,2,3,4
Omkumar said:
1 decade ago
int main()
{
int i=0;
for(; i<=5; i++);
printf("%d,", i);
return 0;
}
Answer for above question is 6
char j=1;
while(j < 5)
{
printf("%d, ", j);
j = j+1;
}
printf("\n");
return 0;
Answer for ths is 1 2 3 4. In first program why we are not mentioning 12345 ?
{
int i=0;
for(; i<=5; i++);
printf("%d,", i);
return 0;
}
Answer for above question is 6
char j=1;
while(j < 5)
{
printf("%d, ", j);
j = j+1;
}
printf("\n");
return 0;
Answer for ths is 1 2 3 4. In first program why we are not mentioning 12345 ?
Govind said:
1 decade ago
@Omkumar.
In first programme you put a semicolon right after the for statement.
for (;i<=5;i++) ;
This means that for loop doesn't have any statements to run.
So even if the for loop condition is TRUE nothing is gonna happen.
For loop will run without printing anything until the condition is TRUE. Once the condition is FALSE (i.e., When i=6) it will come out of the for loop and print i.
Thanks.
In first programme you put a semicolon right after the for statement.
for (;i<=5;i++) ;
This means that for loop doesn't have any statements to run.
So even if the for loop condition is TRUE nothing is gonna happen.
For loop will run without printing anything until the condition is TRUE. Once the condition is FALSE (i.e., When i=6) it will come out of the for loop and print i.
Thanks.
(1)
Chandan rathore said:
1 decade ago
Write the same program in c++. It will give you different result. It will show the ascii value of corresponding digit.
can anyone explain?
can anyone explain?
(1)
Gunjan dhawas said:
1 decade ago
here j is char which is of 1 byte. In 1 byte we can have number from 0 to 255.(2^8=256)as 1 byte=8 bits.
Hence using char as data type we can store integer values up to 255.
Hence using char as data type we can store integer values up to 255.
Sudheer said:
1 decade ago
What is meaning of %i in c language I mean it declares which data type can any one tell me?
Srk said:
1 decade ago
%i means it will print integral type values like octal,decimal and hexadecimal.
Sanjoy said:
1 decade ago
printf ("%d, ", j) ;.
Why there is a comma after the symbol d?
Why there is a comma after the symbol d?
(1)
Narendra.n.h said:
1 decade ago
This above statement will print value along with comma. For example:
int j=9;
printf ("%d, ", j) ;
Output:9,
int j=9;
printf ("%d, ", j) ;
Output:9,
(2)
Sonamuthu said:
1 decade ago
While loop execute 4 times so will print 1, 2, 3, 4.
(1)
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers