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;
}
Related
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;
}
Here is the error screenshot: http://prntscr.com/9n6ybt
Here is the code:
#include <iostream>
using namespace std;
int main()
{
int a, b;
cin>>a>>b;
for(int i=a;i<=b;i++)
{
if (b%i==0)
{
cout << i << " ";
}
}
return 0;
}
for(int i=a;i<=b;i++)
{
if (b%i==0)
{
cout << i << " ";
}
}
Will give a division by zero if i == 0.
You'll have to check the input, or the value of i, for example:
for(int i=a; i<=b; i++)
{
if (i > 0 && b%i==0)
{
cout << i << " ";
}
}
If i == 0, b%i==0 will not be evaluated.
You are not handling the case where i might be 0 (division by 0) so b % i is indetermined. You can solve it by going this way:
if (i==0) continue;
You should handle the case division by "zero". When the value of i = 0 then the code fails and produce an exception.
You should do like this:
#include <iostream>
using namespace std;
int main()
{ int a, b;
cin>>a>>b;
for(int i=a;i<=b;i++)
{ if (i == 0)
continue;
else if (b%i == 0)
cout << i << " ";
}
return 0;
}
The question is:
There is a strip of tickets with numbers of 8 digits. The first ticket has number M , the last - N . Magnitude M and N meet the following relationship: 10000000 ≤ M < N ≤ 99999999. You are required to determine the number of "lucky" ticket between the given numbers. A ticket is considered "lucky" if the sum of the first four digits equals the sum of the last four digits.
And here is my code:
#include <iostream>
#include <fstream>
#include <iomanip>
#include <stdlib.h>
#include <stdio.h>
using namespace std;
int calcSumDigits(int n)
{
int sum=0;
while (n!=0)
{
sum+=n%10;
n/=10;
}
return sum;
}
int main(void)
{
int a,b,cnt=0,x,y;
cin>>a>>b;
for (int i=a;i<=b;i++)
{
x=i%10000;
y=(i-x)/10000;
if (calcSumDigits(x)==calcSumDigits(y)) cnt++;
}
cout<<cnt;
return 0;
}
The results are right but it takes a little bit long time from the program to give the result. For ex when i try from 10000000 to 99999999 the result shows 4379055 but it takes more than 6 seconds
You just need to compare the two sets of sums generated by all the permutations of each half of your numbers - to simplify I rounded the numbers off a bit:
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
using namespace std;
int calcSumDigits(int n)
{
int sum=0;
while (n!=0)
{
sum+=n%10;
n/=10;
}
return sum;
}
int SlowVersion(int a, int b) {
int cnt=0,x,y;
for (int i=a;i<=b;i++)
{
x=i%10000;
y=(i-x)/10000;
if (calcSumDigits(x)==calcSumDigits(y)) cnt++;
}
return cnt;
}
int main()
{
int lower;
int upper;
int original_lower;
int original_upper;
cout<<"enter lower:";
cin>>original_lower;
cout<<"enter upper:";
cin>>original_upper;
lower = original_lower - (original_lower%10000);
upper = original_upper + (9999 - (original_upper%10000));
cout<<"to simplify the calculations the lower was changed to:" << lower << endl;
cout<<"to simplify the calculations the upper was changed to:" << upper << endl;
int cnt=0;
const int b=lower%10000;
const int a=(lower-b)/10000;
const int b_top=upper%10000;
const int a_top=(upper-b_top)/10000;
int a_sums[a_top-a];
int b_sums[b_top-b];
int counter = 0;
for (int i=a;i<=a_top;i++)
{
a_sums[counter] = calcSumDigits(i);
counter++;
}
counter = 0;
for (int x=b;x<=b_top;x++)
{
b_sums[counter] = calcSumDigits(x);
counter++;
}
int countera = 0;
int counterb = 0;
for (int i=a;i<=a_top;i++)
{
counterb = 0;
for (int x=b;x<=b_top;x++)
{
if (a_sums[countera]==b_sums[counterb]) cnt++;
counterb++;
}
countera++;
}
cnt = cnt - SlowVersion(lower,original_lower-1);
cnt = cnt - SlowVersion(original_upper+1,upper);
cout << "The total \"lucky numbers\" are " << cnt << endl;
cout << "a is " << a << endl;
cout << "b is " << b << endl;
cout << "a_top is " << a_top << endl;
cout << "b_top is " << b_top << endl;
system("PAUSE");
return 0;
}
which with your inputs results in 4379055 (the same result you got) and runs extremely quicker.
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;
}
I have attempted to implement the dynamic programming approach to finding the longest common sub sequence between two sequences. My algorithm works when the two strings that are being compared are the same lengths, but when the second string is longer than the first, my LCSLength() function does not return the correct value.
Here is code with a test case that returns the wrong value.
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int LCSLength(string X,string Y);
int main()
{
string first ("HELLO");
string second ("HLOCKKE");
int LCS;
//ifstream inData;
//inData.open("test.dat");
//inData >> first >> second;
//inData.close();
LCS = LCSLength(first,second);
cout << "The LCS is: " << LCS << endl;
cout << first << endl;
cout << second << endl;
return 0;
}
int LCSLength(string X,string Y)
{
int m = X.size();
int n = Y.size();
int C[m][n];
for(int i=0; i<=m; i++)
{
for(int j=0; j<=n; j++)
C[i][j] = 0;
}
for(int i=1; i<=m; i++)
{
for(int j=1; j<=n; j++)
{
if(X[i-1]==Y[j-1])
C[i][j]=C[i-1][j-1]+1;
else
C[i][j]=max(C[i][j-1],C[i-1][j]);
}
}
return C[m][n];
}
This should print "The LCS is: 3" because the LCS between my two strings is 3, however, my program does not. I can't find my error. Thank you for your help.
Fixed my errors using some google searches. Indexing was my problem. Here is the correct code:
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int LCSLength(string X,string Y);
int main()
{
string first ("HELLO");
string second ("HLOCKKE");
int LCS;
//ifstream inData;
//inData.open("test.dat");
//inData >> first >> second;
//inData.close();
LCS = LCSLength(first,second);
cout << "The LCS is: " << LCS << endl;
cout << first << endl;
cout << second << endl;
return 0;
}
int LCSLength(string X,string Y)
{
int m = X.size();
int n = Y.size();
int L[m+1][n+1];
for(int i=0; i<=m; i++)
{
for(int j=0; j<=n; j++)
{
if(i==0 || j==0)
L[i][j] = 0;
else if(X[i-1]==Y[j-1])
L[i][j] = L[i-1][j-1]+1;
else
L[i][j] = max(L[i-1][j],L[i][j-1]);
}
}
return L[m][n];
}