Program to print numbers from 1 to n using while loop .
#include <stdio.h>
int main()
{
int i = 1,n;
printf("Enter the value of n:");
scanf("%d",&n);
while(i <= n) {
printf("%d \n", i);
i++;
}
return 0;
}
Enter the value of n: 20
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Program to print evun numbers from 0 to n using while loop.
#include<stdio.h>
int main()
{ int i=0,n;
printf("Enter the value of n: ");
scanf("%d",&n);
while(i<=n)
{
printf("%d\n",i);
i=i+2;
}
return 0;
}
Enter the value of n: 10
0
2
4
6
8
10
Program to print odd numbers from 1 to n using while loop.
#include<stdio.h>
int main()
{ int i=1,n;
printf("Enter the value of n: ");
scanf("%d",&n);
while(i<=n)
{
printf("%d\n",i);
i=i+2;
}
return 0;
}
Enter the value of n: 10
1
3
5
7
9
Program to add numbers from 1 to n using while loop.
#include<stdio.h>
int main()
{ int i=1,n,sum=0;
printf("Enter the value of n: ");
scanf("%d",&n);
while(i<=n)
{
sum=sum+i;
i++;
}
printf("The sum of %d numbers is :%d",n,sum);
return 0;
}
Enter the value of n: 20
The sum of 20 numbers is :210