Write a C C++ program to generate series of natural numbers

 

Flowchart

Generate series of natural numbers flowchart

 

Coding in C (while)

#include<stdio.h>
int main()
{
int num=1;
while(num<=5)
{
printf(“%d\n”,num);
num++;
}
return 0;
}

 

Coding in C (for)

#include<stdio.h>
int main()
{
int num;
for(num=1; num<=5; num++)
{
printf(“%d\n”,num);
}
return 0;
}

 

Coding in C (do while)

#include<stdio.h>
int main()
{
int num=1;
do
{
printf(“%d\n”,num);
num++;
}while(num<=5);
return 0;
}

 

Coding in C++ (while)

#include<iostream>
using namespace std;
int main()
{
int num=1;
while(num<=5)
{
cout<<num<<“\n”;
num++;
}
return 0;
}

 

Coding in C++ (for)

#include<iostream>
using namespace std;
int main()
{
int num;
for(num=1; num<=5; num++)
{
cout<<num<<“\n”;
}
return 0;
}

 

Coding in C++ (do while)

#include<iostream>
using namespace std;
int main()
{
int num=1;
do
{
cout<<num<<“\n”;
num++;
}while(num<=5);
return 0;
}

 

Output

Generate series of natural numbers output

 

Published by

admin

I am a computer teacher, programmer and web developer