C++ program to print first n odd numbers. - coderforcoders

Search

Wednesday, 13 March 2019

C++ program to print first n odd numbers.

#include<iostream>
using namespace std;
class odd{
public:
int i=1,count;
void printOdd(int n)
{
cout<<"The first "<<n<<" odd numbers are "<<endl;
for(count=0;count<n;)
{
if(i%2!=0)
{
cout<<i<<"\t";
count++;
}
i++;
}

}
   };
int main()
{
odd obj1;
int n;
cout<<"Please enter how many odd numbers you want."<<endl;
cin>>n;
obj1.printOdd(n);
cout<<endl;
return 0;
  }

Output 1:
Please enter how many odd numbers you want.
3
The first 3 odd numbers are
1 3 5

Output 2:
Please enter how many odd numbers you want.
5
The first 3 odd numbers are
1 3 5 7 9

Output 3:
Please enter how many odd numbers you want.
10
The first 3 odd numbers are
1 3 5 7 9 11 13 15 17 19




No comments:

Post a Comment