Program 6:
Output:
#include<stdio.h>Explanation:main() { int i,n; printf("Enter a Number to print all odd number between 0 and N \n"); scanf("%d",&n); for(i=0;i<=n;i++) { if(i%2==1) printf("%d\n",i); else continue; } printf("\n"); }
- Here we intialized 'i' and 'n'
- Then we prompted user to enter a number.
- After taking the number 'n' a for loop iis used to traverse from 'zero' to that number which is 'n'(say 10 or 20).
- In if we used i%2 which is used to identify whether the given number is odd or even
- 0%2=0(even)
- 1%2=1(odd)
- 2%2=0(even)
- 3%2=1(odd)
- and so on...
- So by using modulus operator if the result is 1 then it gets printed other wise the loop continues for next iteration.(continue--refer it here).
- So the output here is to print all odd numbers from zero to 'n'.
Output: