C
Programming and Technical
Programming
Basics
Consider the following code:
for i= m to n increment 2
{ print "Hello!" }
Assuming m < n and exactly one of (m,n) is even, how many times will Hello be printed?
Option 1 : (n - m + 1)/2
Option 2 : 1 + (n - m)/2
Option 3 : 1 + (n - m)/2 if m is even, (n - m + 1)/2 if m is odd
Option 4 : (n - m + 1)/2 if m is even, 1 + (n - m)/2 if m is odd
Read Solution (Total 3)
-
- say m=5 n=16....by incrementing, values of m are 5,7,9,11,13,15....appying say 1st two values in all options, only option a gives valid values for all cases of m and n....for option b and c, m taken as 4 and n as 17, values are implemented in the formulae to check the results.
accordingly option a is correct for any cases taken. - 6 years agoHelpfull: Yes(2) No(0)
- Answer is option 1.
As n is being incremented by 2, so after any no. of increment an even 'n' is going to remain even and an odd 'n' is going to remain odd.
Again as exactly one of (m,n) is even, it gives rise to 2 cases as follows:
case I: n is even, m is odd. The loop is executed once (1) and then with every odd m it encounters next, the loop is
executed until the m - 6 years agoHelpfull: Yes(1) No(0)
- case 3 is the correct answer.
- 1 year agoHelpfull: Yes(0) No(0)
C Other Question
integer i = 0
integer sum = 0
while ( i <= 50 )
{
sum = sum + i
-- MISSING STATEMENT 5 --
}
print sum
main()
{
int a[3][4]={(5,7,5,9),(4,6,3,1),(2,9,0,6)};
int *p;
int (*q)[4];
p=(int *)a;
q=a;
printf("n%u%u",p,q);
p++;
q++;
printf("n%u%u",p,q);
}