C language interview questions solution for freshers beginners placement tricky good pointers answers explanation operators data types arrays structures functions recursion preprocessors looping file handling strings switch case if else printf advance linux objective mcq faq online written test prime numbers Armstrong Fibonacci series factorial palindrome code programs examples on c++ tutorials and pdf

since the program is to find odd numbers from 1 to 100 , we use for loop. Here i=2 is the initialization,i<100 is the condition and i=i+2 is the increment operation i.e it will increment the condition. Here i=i+2 is to find for odd numbers.So all the numbers b/w 1 and 100 will get printed .

/*Find the even no and their sum*/ #include main() { int i=1,n,s=0; printf("enter the value of n\n"); scanf("%d",&n); while(i<=n) { if(i%2==0) { printf(" %d ",i); s=s+i; } i++; } printf("\n"); printf("s=%d\n",s); }

if you want to take input i also user than not declare i=0, also take value of i from user same taken from n,but carefully at user input end I is less than N.

It means if d value of i is divided by 2 and if d remainder is equal to 0 den the value of i is an even number!! for eg. assume that if i=2 den from d if condition it is divisible by 2 n d remainder is 0 so the value of i wud b printd as an even number

i dont understand the solutions pliiz help.....what does "%d"/n",i mean?

ReplyDeleteprinf function is formated function.

ReplyDelete"%d\n" is format. Here

%d represent decimal number or integer type number

\n is new line character.

We have used %d since variable is integer type variable and in the output %d will be replaced by value i.

If you have still doubt the you are welcome.

where use i value

ReplyDeletewhat does i=i+2

ReplyDeletewhat does i+2?

ReplyDeleteInitially i value is 1 so loop add 1+2 = 3

Deletesince the program is to find odd numbers from 1 to 100 , we use for loop. Here i=2 is the initialization,i<100 is the condition and i=i+2 is the increment operation i.e it will increment the condition. Here i=i+2 is to find for odd numbers.So all the numbers b/w 1 and 100 will get printed .

ReplyDeleteWell explained

Deletesorry odd numbers will get printed.

ReplyDeletesir if user prints two unknown number and we have to find all the odd numbers between them then what should we do????

ReplyDelete/*Find the even no and their sum*/

ReplyDelete#include

main()

{

int i=1,n,s=0;

printf("enter the value of n\n");

scanf("%d",&n);

while(i<=n)

{

if(i%2==0)

{

printf(" %d ",i);

s=s+i;

}

i++;

}

printf("\n");

printf("s=%d\n",s);

}

if you want to take input i also user than not declare i=0, also take value of i from user same taken from n,but carefully at user input end I is less than N.

how make this program with for loop? please code

Deletein line number 14 you have stated if (i%2==0), what does it mean?

DeleteIt means if d value of i is divided by 2 and if d remainder is equal to 0 den the value of i is an even number!! for eg. assume that if i=2 den from d if condition it is divisible by 2 n d remainder is 0 so the value of i wud b printd as an even number

Deletehow make this program with for loop? please code

ReplyDeletewhy the program is coming when the void is placed in it ????????

ReplyDelete