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

10th Apr 2022, 6:13 PM
Miraj Kedir
12 Antworten
+ 2
Your attempt??? also instead of put 'mahir' as a tag next time use the keywords of your question like c++, etc
10th Apr 2022, 6:20 PM
ShadowCipher
ShadowCipher - avatar
10th Apr 2022, 7:01 PM
ShadowCipher
ShadowCipher - avatar
+ 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..
10th Apr 2022, 6:24 PM
ShadowCipher
ShadowCipher - avatar
+ 1
10th Apr 2022, 6:41 PM
Miraj Kedir
+ 1
Yeah sure you can have function to execute the summation
10th Apr 2022, 7:12 PM
ShadowCipher
ShadowCipher - avatar
+ 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...
10th Apr 2022, 7:12 PM
Jayakrishna 🇮🇳
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?
10th Apr 2022, 6:24 PM
Simon Sauter
Simon Sauter - avatar
0
Ok give me 2 min
10th Apr 2022, 6:27 PM
Miraj Kedir
0
So can i do this with my own function Like by Viod or smt
10th Apr 2022, 7:08 PM
Miraj Kedir
0
Can u show me the attempt //if don't bother am beginner
10th Apr 2022, 7:25 PM
Miraj Kedir
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..
10th Apr 2022, 7:37 PM
Jayakrishna 🇮🇳
- 1
To get the sum it's an assignment
10th Apr 2022, 6:22 PM
Miraj Kedir