

InterviewSolution
Saved Bookmarks
1. |
Write a C++ program to find the sum of first ‘N’ natural numbers using a user defined function. |
Answer» # include<iostream> using namespace std; int sum(int n) { int i,sum=0; for(i=1;i<=n;i++) sum+=i; return sum; } int main() { int n; cout<<"Enter the number of numbers to find the sum:"; cin>>n; cout<<"The sum of first "<<n<<" natural numbers is:<< sum(n); } |
|