#include <stdio.h>
/* C Prrogram to print EVEN numbers from 1 to N Using While loop*/
int main()
{
/*declare variable c as counter */
int c;
/*declare variable n to store a number */
int n;
/* Assign initial value
to counter c from where we want to print the numbers */
c=1;
/*accept the value for n */
printf("Enter any Number N: ");
scanf("%d",&n);
printf("Even Numbers from 1 to %d:\n",n);
/* the while loop, that will print even numbers */
while(c<=n)
{
/*the condition to check Even number */
if(c%2 == 0)
printf(" %d ",c);
/* increasing loop counter by 1 */
c++;
}
return 0;
}
Output:
Enter Value of N
10
Even Numbers from 1 to 10
2 4 6 8 10
2. Write a program in C to print EVEN numbers from 1 to N using while loop.
2. Write a program in C to print Even numbers from 1 to N using do while loop.
#include <stdio.h>
/* C Prrogram to print Even numbers from 1 to N Using do While loop*/
int main()
{
/*declare variable c as counter */
int c;
/*declare variable n to store limit */
int n;
/*assign initial value
to counter c from where we want to print the numbers */
c=1;
/*accept the value for n */
printf("Enter the value of N: ");
scanf("%d",&n);
printf("Even Numbers from 1 to %d:\n",n);
/* do while loop, that will print Even numbers */
do
{
/* the condition to check EVEN number */
if(c%2 == 0)
printf(" %d ",c);
/* increasing loop counter c by 1 (c++) */
c++;
}while(c<=n);
return 0;
}
Output:
Enter Value of N
10
Even Numbers from 1 to 10
2 4 6 10
Program Explanation:
1.In the program given above there are two variables declared
1.)n to store
the limit and
2)variable c=1 as loop counter.
2. printf("Enter the value of N: ");
display message "Enter the value of N:"
10
"We enter the value 10"
scanf("%d",&n);
Read the value entered by user and store it in n.
next printf() display message "Odd Numbers from 1 to 10"
3. control jumps in do while loop start executing condition c%2==0 (1%2!=0)
The Condition is true and display 1.
value of c is increment by 1 and then while(c<=n) condition(2<=10) is evaluated true
and body of do while loop is executed.
the condition if(c%2==0) is tested which is false , value of c incremented (c++)by 1 now becomes c=3 .
Next while(c<=n) condition is tested(3<=12) which is true.
Again body of loop executes , this process continues while value of c<=10.
control jumps out of loop as soos as value of c becomes 8 and stops the program.
Assignments/practicles using while/do while loop:
Practice/Assignment set 1:
1. Write a program in C to print ODD numbers from 1 to N using while and do while loop.
This is an example of while loop and do while loop - In this C program, we are going to study or write a program to show/print all ODD numbers from given range (1 to N) using while loop and do while loop?
2. Write C program to show EVEN numbers from 1 to N using while loop and do while loop.
This is an example of while and do while loop - In this C program, we are going to study how can we print or display all EVEN numbers from given range 1.. N using while and do while loop?
3.Write a program in C to print all uppercase alphabets using do while and while loop.
This is an example of while loop and do while loop in C programming language - In this C program, we are going to print all uppercase alphabets from ‘A’ to ‘Z’ using while loop and do while loop.
4. Write a program in c to display all lowercase alphabets using while and do while loop.
This is an example of while and do while loop in C programming language - In this C program, we are going to print all lowercase alphabets from ‘a’ to ‘z’ using do while and while loop.
5. C program to print numbers from 1 to 10 using while and do while loop.
This is an example of while and do while loop in C programming language - In this C program, we are going to print numbers from 1 to 10 using while and do while loop.
6.Write C program to read an integer and print its multiplication table using while and do while loop.
In this program, we are reading an integer number and printing its multiplication table, the programs are implemented using while and do while loop.
.7 C Program to check entered number is ZERO, POSITIVE or NEGATIVE until user does not want to quit.
This program will read an integer number and check whether entered number is Positive, Negative or Zero until user does not want to exit.
8. Write the Program in C language to find factorial of a number using while and do while.
In this program, we will read and integer number and find the factorial using different methods - using simple method
9. Write C Program to find sum of first N natural number, N must be provided by the user.
This program will read the value of N from user(keyboard)and calculate sum from 1 to N, first N natural numbers means numbers from 1 to N and N will be read through user.
10. C program to print all prime numbers from 1 to N using while and do while loop.
This program will read the value of N and print all prime numbers from 1 to N. The logic behind implement this program - Run loop from 1 to N and check each value in another loop, if the value is divisible by any number between 2 to num-1 (or less than equal to num/2) - Here num is the value to check it is prime of not.
11. C program to print all Armstrong numbers from 1 to N.
This program will read value of N and print all Armstrong Numbers from 1 to N.
12. C program to print all leap years from 1 to N using while and do while loop.
This program will read value of N and print all Leap Years from 1 to N years. There are two conditions for leap year: 1- If year is divisible by 400 , 2- If year is divisible by 4 and must not be divisible by 100 (for Non Century years).
Practice/Assignment set 2:
1. C program to find sum of the square of all natural numbers from 1 to N.
Series: 1^2+2^2+3^2+4^2+..N^2
2. C program to find sum of the all natural numbers from 1 to N.
Series: 1+2+3+4+..N
3.C program to find the sum of Natural Number/Factorial of Number of all natural numbers from 1 to N.
Series: 1/1! + 2/2! + 3/3! + 4/4! + ... N/N!
4) C program to find sum of following series:
1+ 1/2 + 1/3 + 1/4 + 1/5 + .. 1/N
5) C program to find sum of following series:
1 + 3^2/3^3 + 5^2/5^3 + 7^2/7^3 + ... till N terms
Previous Topic:-->> Write C program to show EVEN numbers from 1 to N using while loop and do while loop. || Next topic:-->>Write a program in C to print all uppercase alphabets using do while and while loop.
Other Topics:
Variables and Identifiers Relational Operators if-else statements Switch case While Loop Infinite while Loops C FOR Loop Infinite for Loops Continue in Loops One Dimensional Array Two Dimensional Arrays Read and Display 2D Arrays Types of functions Passing Array To Functions Nesting of Function Array vs Structure Array of Structure Structures and Functions Structures Within Structures Use Of Pointers In C File Handling In C Loops FAQ Arrays FAQ count vowels in a file Function FAQ Conditional Statements Assignments For Loops Assignments Arrays Assignments Function Assignments Structure Assignments Pointers Assignments Files Assignments Storage classes Assignments Binary Files count words,lines in a file Copy files Update File Continue in Loops break in Loops Difference Between While and Do while difference while do..while & for malloc calloc Storage Classes Operators MCQ Conditional Statements MCQ Loops MCQ Arrays MCQ Function MCQ Structure MCQ Pointers MCQ Files MCQ Storage classes MCQ