Print total odd numbers from 1 to n
C Program
1. For Loop
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n,i;
printf("Enter any number:");
scanf("%d",&n);
printf("Total odd number from 1 to %d is:\n",n);
for(i=1;i <=n;i++)
{
if(i%2!=0)
printf("%d ",i);
}
}
2. While Loop
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n,i;
printf("Enter any number:");
scanf("%d",&n);
printf("Total odd number from 1 to %d is:\n",n);
i=1;
while(i <=n)
{
if(i%2!=0)
printf("%d ",i);
i++;
}
}
3. Do While Loop
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n,i;
printf("Enter any number:");
scanf("%d",&n);
printf("Total odd number from 1 to %d is:\n",n);
i=1;
do
{
if(i%2!=0)
printf("%d ",i);
i++;
}while(i<=n);
}
0 Comments:
Post a Comment