0
Write a program that ask the user for an integer number and find sum of all natural numbers up to the users inter number
In c++ Write a program that ask the user for an integer number and find sum of all natural numbers up to the users inter number https://code.sololearn.com/cJ7wL6FXk337/?ref=app Here is my attempt help me out
12 Answers
+ 2
Your attempt???
also instead of put 'mahir' as a tag next time use the keywords of your question like c++, etc
+ 2
Check this out
https://code.sololearn.com/cQ2MRW3YmQJ8/?ref=app
+ 1
I can share with you the answer, but that's not learning, can you try first and see where you get it wrong? Or if you already done share your attempt..
+ 1
https://code.sololearn.com/cJ7wL6FXk337/?ref=app
Here is my attempt help me out
+ 1
Yeah sure you can have function to execute the summation
+ 1
#include <iostream>
using namespace std;
//declaring
int add(n=o,num,i,sum=0); //this declaration has incorrect, varisbles are not initilized, o or 0? , need braces for open { and closing }, but not semicolon. .
for(int=0;i<n:i++){ // int i=0; , not int=0
sum=sum+num;
cout>>sum; // cout << sum;, not cout>>sum
}
// closing brace for function missing , and return value. make void function.
int main (){
int num;
cout<<"enter number";
cin>>num;
add(sum) //no variable sum here. pass num
return 0;
}
// do it without function. then try with function.
// correct way by loop is
for( int i=1; i<=n ; i++)
sum = sum + i;
cout<<sum;
..
If you want with functions :
Correct the function declaration and calling statements and write this code in function. Then your code works fine
hope it helps...
0
Do you know ...
... what an integer is?
... what a number is?
... what a natural number is?
... what a sum is?
... what a user is?
... what a program is?
0
Ok give me 2 min
0
So can i do this with my own function
Like by Viod or smt
0
Can u show me the attempt //if don't bother am beginner
0
Miraj Kedir If you are asking me, then I already mentioned mistakes along with corrections in comments.. Go through again. And try the corrections..
If anything not clear, then you may ask again..
Your mistakes mainly with implementing of functions..
Function Syntax :
return_type function_name ( <parameters list>)
{
// function body
...
}
call function like:
function_name( <parameters> );
Try and post your attempt..
hope it helps..
- 1
To get the sum it's an assignment