ADVERTISEMENT
ADVERTISEMENT

C program to print ODD numbers from 1 to N using while loop

This is an example of while loop - In this C program, we are going to learn how can we print all ODD numbers from given range (1 to N) using while loop?
Submitted by Manju Tomar, on March 10, 2018

Given a range (value of N) and we have to print all ODD numbers from 1 to N using while loop.

Example:

    Input:
    Enter value of N: 10

    Output:
    ODD Numbers from 1 to 10:
    1 3 5 7 9

Logic:

  • There are two variables declared in the program 1) number as a loop counter and 2) n to store the limit.
  • Reading value of n by the user.
  • Initialising loop counter (number) by 1 as initial value number =1
  • Using while loop by checking the condition number<=n (value of number is less than or equal to n) - this will execute the loop until the value of number is less than or equal to n.
  • Then, within the loop, we are checking the condition to check whether number is ODD or not, the condition is (number%2 != 0) - it returns true if number is not divisible by 2.
ADVERTISEMENT

Program to print ODD numbers from 1 to N using while loop

#include <stdio.h>

int main()
{
	//loop counter declaration
	int number;
	//variable to store limit /N
	int n;

	//assign initial value 
	//from where we want to print the numbers
	number=1;

	//input value of N
	printf("Enter the value of N: ");
	scanf("%d",&n);

	//print statement
	printf("Odd Numbers from 1 to %d:\n",n);

	//while loop, that will print numbers 
	while(number<=n)
	{
		//Here is the condition to check ODD number
		if(number%2 != 0)
			printf("%d ",number);
		
		// increasing loop counter by 1
		number++;
	}

	return 0;
}

Output

Run(1)
Enter the value of N: 10 
Odd Numbers from 1 to 10:
1 3 5 7 9 

Run(2)
Enter the value of N: 100 
Odd Numbers from 1 to 100:
1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 
33 35 37 39 41 43 45 47 49 51 53 55 57 59 
61 63 65 67 69 71 73 75 77 79 81 83 85 87 
89 91 93 95 97 99 

Another way to print ODD numbers from 1 to N

Since, we know that ODD numbers starts from 1 and ODD numbers are those numbers which are not divisible by 2. So, we can run a loop from 1 to N by increasing the value of loop counter by 2.

ADVERTISEMENT

Consider the given code...

//initialising the number by 1
number=1;
//print statement
printf("Odd Numbers from 1 to %d:\n",n);

//while loop, that will print numbers 
while(number<=n)
{
	//print the number 
	printf("%d ",number);
	//increase the value of number by 2
	number+=2;
}

C Looping Programs »


ADVERTISEMENT




ADVERTISEMENT



Comments and Discussions!

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.