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?

Ask 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:
|aksingh1818|satyashiromani|vaishnavi-Deshpande|milanyoyoyogmailcom|Umang|ThreeRed|Msshikhil|batsam22|deepuckraj|Manisha12|sumesh|sameekshya|13priya|LupiN|Naveen-Kumar|Mansa|Oshal-Borkar|milan-ransingh|Aparna-Dasgupta|leo|Aaditi|madachod|tichaona-garaidenga|ronald|tarun101|jinsnjjose|dillu550|Katherine|bhagyaRaj|Vinit

Likes:
|aksingh1818

Dislikes:
Be first to dislike this question

Talk about thisDelete|Like|Dislike|


Answers

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
Talk about this|Once you have earned teacher badge you can edit this questionDelete|Like|Dislike|
------------------------------------

Can you help us to add better answer here? Please see this



Not the answer you're looking for? Browse other questions from this Question paper or ask your own question.

Join eduladder!