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
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