Skip to main content

Write a C C++ program to generate series of natural numbers from 1 to 5


Flowchart

Flowchart to generate display series of natural numbers

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

Write a C C++ program to display generate series of natural numbers from 1 to 5