Sum of n natural numbers || Functions || C++ || Functions in C++
In this, we are going to see Functions, the program is based on function which Calculates, Profit or Loss.

The code given below can be used for TURBO C++ Compiler:-

#include<iostream.h>
#include<conio.h>
int num;
void user_input()
{
    cout<<"Enter the number till which you want the sum\n";
    cin>>num;
}

void process()
{
    int sum=0;
    for(int i=1;i<=num;i++)
    {
        sum = sum+i;
    }
    cout<<"The Sum from 1 to "<< num << " = " <<sum;
}

void main()
{
    clrscr();
    user_input();
    process();
    
    getch();
}
	
    
The code given below can be used for g++/gcc Compiler:-

#include<iostream>
using namespace std;

int num;
void user_input()
{
    cout<<"Enter the number till which you want the sum\n";
    cin>>num;
}

void process()
{
    int sum=0;
    for(int i=1;i<=num;i++)
    {
        sum = sum+i;
    }
    cout<<"The Sum from 1 to "<< num << " = " <<sum;
}

int main()
{
    user_input();
    process();
    
    return 0;
}
	
    
#ENJOY CODING

Post a Comment

FOR ANY DOUBTS AND ERRORS FEEL FREE TO ASK. YOUR DOUBTS WILL BE ADDRESSED ASAP

Previous Post Next Post