Q64 The output of executing the following C program is gate computer science 2017
The Eduladder is a community of students, teachers, and programmers just interested to make you pass any exams. So we solve previous year question papers for you.
See Our team
Wondering how we keep quality?
Got unsolved questions?

### Use Me  ?

New searches
GATE-Computer-Science-Engineering-2017-->View question

## Q.64) The output of executing the following C program is ________. -gate computer science 2017

# include
int total(int v)
static int count = 0;
while (v) {
count += v & 1;
v >>= 1;
}
return count;
void main()
static int x = 0;
int i = 5;
for (; i> 0; i--) {
x = x + total(i);
}
printf (“%d\n”, x) ;

A) 23
B) 24
C) 26
D) 27

By:satyashiromani

Taged users:

Likes:
|aksingh1818

Dislikes:
Be first to dislike this question

A) 23

Explanation:
Digits : 5-0101, 4-0100, 3-0011, 2-0010, 1-0000 Countof 1s : 2, 3, 5, 6, 7 Total: 2+3+5+6+7 = 23 total(i)=23 Therfore, option A is correct Check out the running code with comments: [sourcecode language="CPP"] #include int total(int v) { static int count = 0; while(v) { count += v&1; v >>= 1; } //This count can be used to see number of 1s returned //for every number i //printf("%d", count); return count; } void main() { static int x=0; int i=5; for(; i>0; i--) { //total gets added everytime with total number //of digits in the given number i x = x + total(i); } printf("%d\n", x); } [/sourcecode]

This explanation is contributed by Satya Shiromani

deepuckraj

Likes:
Be first to like this answer

Dislikes:
Be first to dislike this answer