C language tricky good pointers questions answers and explanation operators data types arrays structures questions functions recursion preprocessors, looping, file handling, strings questions switch case if else printf advance c linux objective types mcq faq interview questions and answers with explanation and solution for freshers or beginners. Placement online written test prime numbers Armstrong Fibonacci series factorial palindrome code programs examples on c 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.

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 .

ReplyDeletesorry 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

Deletehow make this program with for loop? please code

ReplyDelete