I'm working on building a loop right now that only shows the end value. However, the code is showing all the integers though.
Here's my code:
#include <iostream>
using namespace std;
int sum = 0;
void findMultiples(int n){
cout <<"Enter a positive integer:"<<endl;
for(int i = 0; i <= n; i++)
if (i % 3 == 0)
cout << "Sum: "<<(sum =sum+i)<<endl;
}
int main() {
int num;
cin>>num;
findMultiples(num);
return 0;
}
You are using for then if then showing output. The for and if scope area is one line without { }, so you are printing and summing at the same time and it is each time in the scope of if statement.
#include<iostream>
using namespace std;
int sum = 0;
void findMultiples(int n){
cout <<"Enter a positive integer:"<<endl;
for(int i = 0; i <= n; i++)
if (i % 3 == 0)
sum =sum+i;
cout << "Sum: "<<sum<<endl;
}
int main() {
int num;
cin>>num;
findMultiples(num);
return 0;
}
Your cout statements are in the wrong places. Try this instead:
#include <iostream>
using namespace std;
int findMultiples(int n){
int sum = 0;
for(int i = 0; i <= n; i++){
if (i % 3 == 0)
sum += i;
}
return sum;
}
int main() {
cout << "Enter a positive integer:" << endl;
int num;
cin >> num;
cout << "Sum: " << findMultiples(num) << endl;
return 0;
}
Related
How do I change this into a do-while loop? I am trying to have the exact the same output as the code posted here, but I want to use do-while loop instead of for loop
#include <iostream>
using namespace std;
int main()
{
int side;
cout << "Enter a number: ";
cin >> side;
for (int i = 0; i < side; i++)
{
for (int j = i; j >= 0; j--)
{
cout << "#";
}
cout << "\n";
}
}
#include <iostream>
using namespace std;
int main(){
int side;
cout << "Enter a number: ";
cin >> side;
int i = 0;
do {
int j = i;
while(j >= 0){
std::cout << "#";
j--;
}
std::cout << "\n";
i++;
}
while(i < side);
}
Is this what you meant? - Do While Loop
My for loop works outside the void function but not inside it.
I tried not using a function and it works but I need to put this for loop inside a function because I want to use it in other code.
It works like this:
#include <iostream>
using namespace std;
int main()
{
int n, sum = 0;
cout << "Enter a positive integer: ";
cin >> n;
for (int i = 1; i <= n; ++i) {
sum += i;
}
cout << "Sum = " << sum;
return 0;
}
But not like this:
#include <iostream>
using namespace std;
void somation(){
int n, sum = 0;
cout << "Enter a positive integer: ";
cin >> n;
for (int i = 1; i <= n; ++i) {
sum += i;
}
cout << "Sum = " << sum;
}
This the error message I get in dev-cpp:
D:\Dev-Cpp\MinGW64\x86_64-w64-mingw32\lib\libmingw32.a(lib64_libmingw32_a-
crt0_c.o) In function `main':
18 C:\crossdev\src\mingw-w64-v3-git\mingw-w64-
crt\crt\crt0_c.c undefined reference to `WinMain'
D:\CPP Projects\collect2.exe [Error] ld returned 1 exit status
I believe the problem you are having is that you are trying to execute a function without calling it in a main.
If you want to use the function somation somewhere else you can just copy it, but you always have to call the function in a main or else it won't work.
void somation()
{
int n, sum = 0;
cout << "Enter a positive integer: ";
cin >> n;
for (int i = 1; i <= n; ++i) {
sum += i;
}
cout << "Sum = " << sum;
return;
}
int main(){
somation();
return 0;
}
Your program needs to have a main function (entry point for your program).
Try this
#include <iostream>
using namespace std;
void somation()
{
int n, sum = 0;
cout << "Enter a positive integer: ";
cin >> n;
for (int i = 1; i <= n; ++i)
{
sum += i;
}
cout << "Sum = " << sum;
}
int main()
{
somation();
return 0;
}
I'm beginner in C++, and I've got a question about a simple sum code in c++.
Here is my code :
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
int n;
int sum;
int arr_i = 0;
cin >> n;
vector<int> arr(n);
while (arr_i != n)
{
cin >> arr[arr_i];
sum += arr[arr_i];
//cout << sum << endl;
if (arr_i == n - 1)
cout << sum;
arr_i++;
}
return 0;
}
The output doesn't print the correct answer without "cout << sum" before the if condition.
How can I solve this problem ?
You forget to initialize sum to 0.
int sum = 0;
As the previous post mentioned, sum was not initialized to 0. In terms of good practices and styles its a good idea to initialize any variables that are modified within a loop right before the loop body so that someone reading the code can easily grasp the context of your variables.
int main()
{
int n;
int sum;
int arr_i;
cin >> n;
vector<int> arr(n);
sum = 0;
arr_i = 0;
while (arr_i != n)
{
cin >> arr[arr_i];
sum += arr[arr_i];
//cout << sum << endl;
if (arr_i == n - 1)
cout << sum;
arr_i++;
}
return 0;
}
or as I prefer a "for" loop...
int main()
{
int n;
int sum;
int arr_i;
cin >> n;
vector<int> arr(n);
for (sum = 0, arr_i = 0; arr_i != n; arr_i++)
{
cin >> arr[arr_i];
sum += arr[arr_i];
//cout << sum << endl;
if (arr_i == n - 1)
cout << sum;
}
return 0;
}
I am using C++ for a very simple program but I can't seem to figure out what to do. I want to output the numbers in the loop and at the end of it, sum all the numbers.
What I need is the sum of all the numbers from 1 to 10 (55) to display after the loop output.
#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
int a;
for (a=1; a<=10; a++)
{
cout<<a<<endl;
}
getch();
}
#include <iostream>
int main()
{
int i = 2;
int sum = 1;
std::cout << sum;
while (i <= 10)
{
std::cout << " + " << i;
sum += i;
i++;
}
std::cout << " = " << sum << std::endl;
return 0;
}
int main()
{
int total = 0;
for (int a = 1; a <= 10; ++a)
{
std::cout << a << '\n';
total += a;
}
std::cout << total << '\n';
getch();
}
try like this
int a=0;
for (int i=1; i<=10; i++)
{
a+=i; //First add all the numbers (1 to 10)
}
cout<<a; //show the result
If this is what you want..
int main()
{
int myNum, answer=0;
cin >> myNum;
for (int x=1; x<=myNum; x++)
{
answer += x;
}
cout << "Sum of 1 to " << myNum << " = " << answer << endl;
}
Input: 10
Output: Sum of 1 to 10 = 55
Try this one
int n=10;
int sum=0;
sum=(n*(n+1))/2;
cout<<sum;
or
int a=0;
for(int i=0;i<10;i++)
{
a=a+i;
}
cout<<a;
Or you cant try this
void main()
{
cout<<Fun(10);
}
int Fun(int a)
{
if(a==0)
return a;
a+=fun(a-1);
}
try this,
#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
int a,sum=0;
for(a=1; a<=10; a++)
{
cout<<a<<endl;
sum=sum+a;
}
cout<<sum;
getch();
}
Try this code
#inlcude <iostream>
using namespace std;
int main() {
int number,sum=0;
for(int i=0; i<number; i++) {
cout<<"i = "<<i<<endl;
sum+=i;
}
cout<<"Total = "<<sum<<endl;
return 0;
}
Very new to c++. Making this factorial program, and somehow I've scraped through the code and built something, but the value it returns is always 10 times the correct value. For example, factorial for 5 is computed at 1200 instead of 120, and factorial of 3 is computed at 60. Why the extra unit?
This is the code:
#include <iostream>
using namespace std;
int main() {
int i, j, num;
cout << "Compute factorial: " << endl;
cin >> num;
for (i=1; i<num; i++){
for (j=num; j>1; j--)
num *=(j-i);
cout << num;
}
return 0;
}
All you need is one loop. This way should be much simpler.
#include <iostream>
using namespace std;
int main() {
int i, j=1, num;
cout << "Compute factorial: " << endl;
cin >> num;
for (i=num; i>0; i--){
j *= i;
}
cout << j << endl;
}
for (i=1; i<num; i++){
num *=i;
}
This should be enough
I think you are not quite clear about for loop,and be careful with the change of num
try this piece of code, hope it will help
#include <iostream>
using namespace std;
int main() {
int i, j, num;
cout << "Compute factorial: " << endl;
cin >> num;
int output_num = 1;
for (i=1; i<=num; i++){
output_num *= i;
}
cout << output_num;
return 0;
}
Use this code :
#include<iostream>
using namespace std;
int main()
{
int num,fact=1;
cout<<" Enter the no. to find its factorial:";
cin>>num;
for(int a=1;a<=num;a++)
{
fact=fact*a;
}
cout<<"Factorial of a given Number is ="<<fact<<endl;
return 0;
}
Output :
using two loops for computing factorial is very complex..... and you do not need it.
Try this
#include <iostream>
using namespace std;
int main() {
int i,num;
cout << "Compute factorial: " << endl;
cin >> num;
for(i=num;i>1;--i)
num*=(i-1);
cout << num;
return 0;
}