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.
Home » Programming » C++ Programming » Programs » Others » CPP for loop program
Suryateja Pericherla Categories: Others. No Comments on CPP for loop program
0
(0)

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?


Subscribe to our monthly newsletter. Get notified about latest articles, offers and contests.


Leave a Reply

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

Facebook
Twitter
Pinterest
Youtube
Instagram
Blogarama - Blog Directory