Startertutorials Blog
Tutorials and articles related to programming, computer science, technology and others.
Subscribe to Startertutorials.com's YouTube channel for different tutorial and lecture videos.

Categories: Others. No Comments on CPP for loop program

In this article we will look at a CPP for loop program. You will learn how to use a for in C++ for solving problems.

 

This program demonstrates the use of for loop in C++. This program reads two numbers a and b from the user. For every number in the range a to b, if the number is in between 1 and 9, we have to print the word form of that number (for example, if the number is 3, we have to print three), if the number is greater than 9 and even, we have to print even, and if the number is greater than 9 and odd, we have to print odd.

 

Program is as follows:

#include <iostream>
using namespace std;

int main() {
    int a, b;
    cin>>a>>b;
    for(int i = a; i <= b; i++){
        if(i>=1 && i<=9){
            if(i==1) cout<<"one"<<endl;
            if(i==2) cout<<"two"<<endl;
            if(i==3) cout<<"three"<<endl;
            if(i==4) cout<<"four"<<endl;
            if(i==5) cout<<"five"<<endl;
            if(i==6) cout<<"six"<<endl;
            if(i==7) cout<<"seven"<<endl;
            if(i==8) cout<<"eight"<<endl;
            if(i==9) cout<<"nine"<<endl;
        }
        else if(i>9 && (i%2==0)){
            cout<<"even"<<endl;
        }
        else{
            cout<<"odd"<<endl;
        }
    }
    return 0;
}

 

Input and output for the above program are as follows:

Input:
8
11

Output:
eight
nine
even
odd

 

How useful was this post?

Click on a star to rate it!

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

Suryateja Pericherla

Suryateja Pericherla, at present is a Research Scholar (full-time Ph.D.) in the Dept. of Computer Science & Systems Engineering at Andhra University, Visakhapatnam. Previously worked as an Associate Professor in the Dept. of CSE at Vishnu Institute of Technology, India.

He has 11+ years of teaching experience and is an individual researcher whose research interests are Cloud Computing, Internet of Things, Computer Security, Network Security and Blockchain.

He is a member of professional societies like IEEE, ACM, CSI and ISCA. He published several research papers which are indexed by SCIE, WoS, Scopus, Springer and others.

Leave a Reply

Your email address will not be published. Required fields are marked *