search
Log In
7 votes
6k views

Consider the following C program:

#include <stdio.h>
int main()
{
    int a[] = {2, 4, 6, 8, 10};
    int i, sum=0, *b=a+4;
    for (i=0; i<5; i++)
        sum=sum+(*b-i)-*(b-i);
    printf("%d\n", sum);
    return 0;
}

The output of the above C program is _______

in Programming
edited by
6k views

3 Answers

23 votes
 
Best answer
$\text{sum}=0, *b = a+4  \text{ i.e.pointing to }10$

$\text{sum}= \text{sum}+ (*b-i) - *(b-i)$

$i=0$
$\text{sum}= 0+ (10-0) - (10) = 0$

$i=1$
$\text{sum}= 0 + (10-1) - (8) = 1$

$i=2$
$\text{sum}= 1 + (10-2) - (6) = 3$

$i=3$
$\text{sum}= 3 + (10-3) - (4) = 6$

$i=4$
$\text{sum}= 6 + (10-4) - (2) = 10$

edited by
1
For i=4,
You took sum = 7 while in i=3 sum is 6
5 votes
#include <stdio.h>
int main()
{
    int a[] = {2, 4, 6, 8, 10};
    int i, sum=0, *b=a+4; /* Here b points to the address of a[4]=10 */
    
    /*  *b means the value of which the address b points to. */
    /*  So *b = a[4] = 10 */
    
    /*  (b-1) means &a[4-1], so *(b-1)=a[4-1]=8  */
    /*  (b-2) means &a[4-2], so *(b-2)=a[4-2]=6  */
    /*  and so on.  */
    
    for (i=0; i<5; i++)
        sum=sum+(*b-i)-*(b-i); /* sum=0+(10-0)-(10) = 0 */
                               /* sum=0+(10-1)-(8) = 1 */
                               /* sum=1+(10-2)-(6) = 3 */
                               /* sum=3+(10-3)-(4) = 6 */
                               /* sum=6+(10-4)-(2) = 10 */
        
    printf("%d\n", sum);       /* It will give the output as 10. */
    return 0;
}

 

So the correct answer is $10$.

 

2 votes

Hope this helps :)

Answer:

Related questions

6 votes
5 answers
1
4.8k views
Consider the following C program : #include<stdio.h> int jumble(int x, int y){ x = 2*x+y; return x; } int main(){ int x=2, y=5; y=jumble(y,x); x=jumble(y,x); printf("%d \n",x); return 0; } The value printed by the program is ______________.
asked Feb 7, 2019 in Programming Arjun 4.8k views
7 votes
7 answers
2
4.8k views
Consider the following C program: #include <stdio.h> int main() { int arr[]={1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 5}, *ip=arr+4; printf(“%d\n”, ip[1]); return 0; } The number that will be displayed on execution of the program is _______
asked Feb 7, 2019 in Programming Arjun 4.8k views
10 votes
4 answers
3
4.8k views
Consider the following C program: #include <stdio.h> int main() { float sum = 0.0, j=1.0, i=2.0; while (i/j > 0.0625) { j=j+j; sum=sum+i/j; printf("%f\n", sum); } return 0; } The number of times the variable sum will be printed, when the above program is executed, is _________
asked Feb 7, 2019 in Programming Arjun 4.8k views
30 votes
10 answers
4
9.8k views
Consider the following C program: #include <stdio.h> int r() { static int num=7; return num--; } int main() { for (r();r();r()) printf(“%d”,r()); return 0; } Which one of the following values will be displayed on execution of the programs? $41$ $52$ $63$ $630$
asked Feb 7, 2019 in Programming Arjun 9.8k views
...