I have to write a programme in c++ where user enters a number N, then on a second line he enters as many numbers as N, no more. The ouput shoud be the sum of all positive numbers among the entered numbers.
I have to use for loop. Also we have not covered much so far, only if statements.
The code I have tried gives the sum of positive numbers only, but I can't make the programme use N inputs and stop. It either calculates only one or continues as long as user enters numbers.
#include <iostream>
using namespace std;
int main ()
{
int n, sum=0;
cin>> n;
cout<<endl;
cout<<"Enter numbers"<<endl;
for (int i=1; i<=n; i++)
{
cin>>i;
if(i>0)
{sum=sum+i;
}
cout<<sum<<endl;
}
return 0;
}
The problem is that you're using the same variable (i) for looping and input.
for (int i=1; i<=n; i++)
{
cin>>i;
Whatever gets entered in that cin>>i ruins the logic of your program. Add one separate input variable and keep your i for the loop.
Example:
#include <iostream>
int main() {
int n, sum = 0;
std::cout << "How many numbers do you want to enter? \n";
std::cin >> n;
std::cout << std::endl;
std::cout << "Enter numbers: \n";
for(int i = 1; i <= n; i++) {
std::cout << i << ": ";
int input;
if(std::cin >> input) {
if(input > 0) {
sum = sum + input;
}
std::cout << sum << std::endl;
} else
break; // user failed to enter a number
}
}
Related
Let me preface this by saying I am fairly new to functions and arrays.
I have to make 3 functions: Function1 will be user input, Function2 will determine even/odd numbers, and Function3 will display the contents. I have Function1 and Function3 complete, and will post below, but I'm having a difficult time with Function2. What I have now will give the user an error message if they enter an even number, but it's messed up, and I just can't seem to figure out.
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
void getUsrInput(int num[], int size) //function for user input (function 1 of 3)
{
int n;
for (int i = 0; i < size; i++)
{
cout << "Enter five odd numbers: ";
cin >> n;
num[i] = n;
if (num[i] % 2 != 0) //if the number is odd then store it, if it is even: //function for even or odd (function 2 of 3 *doesn't work)
{
i++;
}
else
{
cout << "Invalid input. Please only enter odd numbers!" << endl; //let the user know to enter only odd numbers.
}
}
}
int main()
{
const int size = 5; //array size is 5 numbers
int num[size];
getUsrInput(num, size);
cout << "D I S P L A Y - PART C/B" << endl;
cout << "========================" << endl;
for (int i = 0; i < size; i++)
{
cout << num[i] << endl; //function for display (function 3 of 3)
}
}
for (int i = 0; i < size; i++)
increments i each time through the loop.
if (num[i] % 2 != 0) {
i++;
}
increments i each time the number is odd. So each time the user inputs an odd number, i gets incremented twice. Change the loop control to
for (int i = 0; i < size; }
so that i only gets incremented on valid input.
It' perfect for a while loop and you only count up if the insert is ok:
void getUsrInput(int num[], int size) //function for user input (function 1 of 3)
{
int n, i=0;
while( i < size )
{
cout << "Enter five odd numbers: ";
cin >> n;
if (n % 2 == 0)
cout << "Invalid input. Please only enter odd numbers!" << endl;
else
num[i++] = n; // ok then store and count up
}
}
i've just learned cpp and want to know is there a shorter way to display a sequential number.
this is my code
#include<iostream>
using namespace std;
int main(){
int n;
cout<<"Input number: ";
cin>>n;
cout<<"The next 5 rows of number is :"<<n+1<<n+2<<n+3<<n+4<<n+5;
}
A simple loop should solve your problem:
int main(){
int n;
cout << "Input number: ";
cin >> n;
cout << "The next 5 rows of number are: ";
for (int i = n + 1; i <= n + 5; i++) {
cout << i << ' ';
}
}
I know this question asked many times but I am facing different problem in my code, I try to calculate sum of long integers range between 2-15.
Code:
long array[20];
long NUMBERS;
cout << "How many numbers ? : ";
cin >> NUMBERS;
long sum=0;
for (int i = 0; i < NUMBERS;i++){
cout << "Input number " << (i+1) << " : ";
cin >> array[i];
}
cout << "Calculate Sum" << endl;
for (int i = 0; i < NUMBERS;i++){
sum = sum + array[i];
}
cout << "Sum is : " << sum << endl;
When I input these three numbers.
1234567
123456
12345
Output:
Sum is : 1370368
but actual answer is : 3703627.
I try these solutions
summing-large-numbers and sum-of-alternate-elements-of-integer-array but still not get right solution, also how we can solve this problem if user input different number with different ranges.
This isn't about programming, but math...
Hope this helps: http://www.wikihow.com/Add-Large-Numbers
(As a simple example, add 1 and 11. What is the result? 12 or 21?)
my code sum large number with string.
first you enter number of numbers you want to sum(to 25).
and then you enter the number (to 180 'each number').
#include <iostream>
#include <stdlib.h>
using namespace std;
int main (){
int band;
cin >> band;
string string_of_number[25];
for (int i=0; i<band; i++){ //for get all string number
cin >> string_of_number[i];
}
int strings_length[band];
for (int i=0; i<band; i++){ //for get all length of strings
strings_length[i]=string_of_number[i].length();
}
int answer[180];
for(int i=0; i<180; i++){
answer[i]=0;
}
int remaner=0;
int sum=0;
int last=180;
for (int h=0; h<band; h++){ // for sum strings with sum answer one by one
int j=179;
for (int i=strings_length[h]; i>=0; i--){
if(string_of_number[h][i]=='\0'){
i--;
}
sum = (remaner + (string_of_number[h][i]-'0') + answer[j]) %10;
remaner = ((string_of_number[h][i]-'0') + answer[j]) / 10;
answer[j]=sum;
if (i==0 && remaner>0){
j--;
answer[j]+=remaner;
remaner=0;
}
if(last>j)
last=j;
j--;
}
}
for(; last<180; last++){
cout << answer[last];
}
}
It seems that your program assumes all numbers are 7 digit:
1234567
123456[0]
12345[00]
I am having trouble with translating my algorithm into c++ code that will display all abundant numbers less than the inputted number. I want to create a program that will display all abundant numbers less than an inputted number. For example if I entered a number like 19, the console should print out 12 and 18. The algorithm is this.
Scan through each number
Check whether its an abundant number
If it is an abundant number, then print it out
I am using Microsoft Visual Studios 2013 IDE and a compiler that supports c++11.
This is the code I have so far
#include <iostream>
using namespace std;
int main()
{
int number;
cout << "Enter a number" << endl;
cin >> number;
for (int i = 1; i < number; i++)
{
// Don't know what to put here
}
char inputCharacter;
cin >> inputCharacter;
return 0;
}
I didn't knew, what abundant numbers are, but if you mean this: https://en.wikipedia.org/wiki/Abundant_number
Then this may help you:
#include <iostream>
using namespace std;
int main()
{
int number;
cout << "Enter a number" << endl;
cin >> number;
for (int i = 1; i < number; i++)
{
int sum = 0;
for(int k = 1; k < i; k++)
{
if(i % k == 0)
{
sum += k;
}
}
if(sum > i)
{
cout << i << endl;
}
}
char inputCharacter;
cin >> inputCharacter;
return 0;
}
If you need any explanation about the algorithm, just ask for it.
int number;
cout << "Enter a number" << endl;
cin >> number;
for (int i ==1; i<number; i++)
{
int temp =0;
for (int j ==1; j<i; j++)
{
if (i%j==0)
{
temp+=j;
}
}
if (temp>i)
{
cout<<j;
}
}
this uses modular division and will accumulate all numbers which are factors.
if the above returns 1 its abundant i think... if i got the right idea of what abundancy is.
#include <iostream>
using namespace std;
int main()
{
int howmany;
int i=0;
cout <<"How many integers you want to add,just enter the number.\n";
cin >> howmany;
while (i < howmany)
{
int sum = 0;
sum = sum +i;
i++;
cout << sum << endl;
}
system ("pause");
return 0;
}
what is the mistake? It gives me list of numbers rather than their sum.I have tried to change order of statements in loop body but still problem not solved.
Initialise sum=0 outside the loop. Because in your code every time you loop, the varaible sum is set to 0.
Change like this
int sum = 0;
while (i < howmany)
{
sum = sum +i;
i++;
cout << sum << endl;
}
Declare the sum variable out side the loop.
Or
you can declare a static variable inside loop (static variable will
be initialized once)
int main()
{
int howmany;
int i=0;
cout <<"How many integers you want to add,just enter the number.\n";
cin >> howmany;
int sum = 0; //Declare here
while (i < howmany)
{
sum = sum +i;
i++;
}
//Display the result after the while loop.
cout << sum << endl;
system ("pause");
return 0;
}