Using while loop, write a C-program that will display all odd numbers from 1-50


Using while loop, write a C-program that will display all odd numbers from 1-50

In todays tutorial we shall be learning how to find and display odd numbers using while loop in C-Programming language. Before you can successfully write this code you need to understand 4 different concept:

  1. Variable declaration
  2. Format specifier
  3. While loop and finally
  4. If statement.

What is a variable

A variable is a container that is used to either store an integer value, char value etc. The syntax to declare a variable is Data type variable_name= value. To store an integer value to variable name number we write: int  number = 10; and to store a character value we write char grade='A';.

What is format specify

In C-programming Language a format specifier is used to tell the compiler about the type of data that must be given or input and the type of data that must be printed on the screen. Format specifiers are used to take inputs and print the output of a data type.
 % symbol is used along side a letter as format specifier. Example %d for integer, %f for float, c% for character.

What is a while loop

While loop is a control structure that is used to iterate over a block of code over until the specified condition becomes false. syntax of a while loop. 
int iterator=1;
while (condition){
    //block of code
    iterator++;
}

What is an if statement

An if statement is a programming conditional statement that execute it block of code if the condition stated for it evaluate to true. The syntax for if statement is:

if(condition){
    //block of code to be executed
}

Code Sample

#include <stdio.h>

int main() {
    // Write C code here
    printf("List of odd numbers from 1-50:\n");
    int iterator=1;
    while(iterator <=50){
        if(iterator%2==1){
            printf(" %d ", iterator);  
        }
        iterator++;
    }
    return 0;
}

Explanation on Source Code

Line 3: Print out the word "List of odd numbers from 1-50" on a new line 
Line 4: Define an integer value called iterator and initialize it to the value 1 
Line 5: Will iterate over the loop for as long as the value of iterator is less than or equal to 50.
Line 6: Checks if the modules of iterator by 2 returns a 1.
Line 7: If 1 is return then the value is an odd number and it is displayed on the screen
Line 9: Increase the value of iterator by one then check the loop condition.
Line 11: Terminate the program.

Conclusion

The tutorial give a step by step guide on how to develop a program that displays odd numbers from 1 to 50 with code sample and clear example. The % used within the if statement is called a modulus operator.
Ameh Joseph Onyeke

Hello and welcome to my blog! My name is Ameh Joseph Onyeke a passionate educator and tech enthusiast with a BSc in Computer Science from Benue State University, a PGDE from the University of Maiduguri, and a Master’s in Software Engineering from Bayero University Kano. My journey blends a strong foundation in computing with a love for teaching, aiming to simplify complex tech concepts for learners at all levels. Through this blog, I provide answers and insights across 10+ programming languages to support students and tech enthusiasts in building their skills and confidence. Whether you're solving coding problems or sharpening your programming abilities, this blog is here to guide you. Let’s learn and grow together!

Post a Comment

Previous Post Next Post