how can we add first 10 integers using while loop in c++ - c++

#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;
}

Related

How can I make my function only accept odd numbers into my array, and reject even numbers? C++

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
}
}

Code it being affected by a cout which is not need it code

The program simply have a function which checks if a number is a perfect number. A perfect is the one for which sum of all its factors(excluding number itself) is equal to number it self. Like 6(1 + 2 + 3 = 6) also 28
I am seeing a werid behavior. There is cout << "". If we remove this line code will not work properly if we put this everything works fine. I can't understand this
#include<iostream>
using namespace std;
int IsPerfect(int);
int main(){
int N, j;
cout << "Enter a value for N: ";
cin >> N;
cout << "Perfect numbers are ";
for(j=1; j<=N; j++){
cout << ""; //If you remove this line no output will be printed
if(IsPerfect(j) == 1){
cout << j << endl;
}
}
}
int IsPerfect(int a){
int sum=0;
for(int i; i<a; i++){
if(a%i == 0){
sum=sum+i;
}
}
if(sum == a){
return 1;
}
else{
return 0;
}
}
The code has nothing to do with the cout. Look in the loop in isPerfect function. The variable i has undefined value as it is not initialized. Initialize it like this:
for(int i = 1; i<a; i++){

Multiple input in for loop

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
}
}

Adding integers with c++

I am trying to write a program in c++ that allows you to enter 10 numbers and receive the sum of those numbers using a for loop. However, I am encountering a problem where I am not getting the added integers and instead getting the sum of the last 2 numbers.
#include <iostream>
using namespace std;
int main ()
{
int i;
int number;
for(i; i < 10; i++)
{
cout << "enter a number" << endl;
cin >> number;
if( i < 10)
number+= number;
}
cout << number;
return 0;
}
1) You never initialize i, you should do so in the for loop.
for(int i=0; i < 10; ++i)
You also don't need:
if( i < 10 )
because based on your for loop conditions, this can never be false.
2) You also need to initialize number.
int number = 0;
3) You shouldn't cin directly to number or you will replace the total every single time. You could do this in your for loop for example.
int temp = 0;
cin >> temp;
number += temp;
Summary
If you correct the above three issues, the modified code would look like this:
int main ()
{
int number = 0;
for(int i=0; i < 10; ++i)
{
cout << "enter a number" << endl;
int temp = 0;
cin >> temp;
number += temp;
}
cout << number;
return 0;
}
When you write cin >> number; you are replacing your sum so far. You need to take user input into a separate variable and then add it. Something like:
for(i = 0; i < 10; i++)
{
cout << "enter a number" << endl;
int input;
cin >> input;
number += input;
}
A couple of things. You need to initialize i before you use it in a for loop
for(int i=0; i<10; i++)
Also, you are using the same variable to get the number from cin as you are using to store the sum. You should use two separate variables.
Here's a list of things to change and a working program with a few modifications below
1.You must initialize variables before you can use them in any meaningful way.
Initialize means assigning an initial value but it also requires you declare variables and that they are properly defined (e.g. loop variable is i NOT defined to be an int)
Therefore, you have to initialize the for loop variable i. You also have to initialize number by changing it to 0
2. Use a different number for your input and for your sum because you will just overwrite any old values as you read them. Note that you do not need to assign a value to this number because you are reading from the input stream into it
int sum=0,n;//n is input
for(int i=0; i < 10; i++)
{
cout << "enter a number" << endl;
cin >> n;
sum+= n;
}
cout << sum;

Sum of Numbers C++

I am supposed to write a program that asks the user for a positive integer value. The program should use a loop to get the sum of
all the integers from 1 up to the number entered. For example, if the user enters 50, the loop will find the sum of
1, 2, 3, 4, ... 50.
But for some reason it is not working, i am having trouble with my for loops but this is what i have down so far.
#include <iostream>
using namespace std;
int main()
{
int positiveInteger;
int startingNumber = 1;
int i = 0;
cout << "Please input an integer up to 100." << endl;
cin >> positiveInteger;
for (int i=0; i < positiveInteger; i++)
{
i = startingNumber + 1;
cout << i;
}
return 0;
}
I am just at a loss right now why it isn't working properly.
The loop is great; it's what's inside the loop that's wrong. You need a variable named sum, and at each step, add i+1 to sum. At the end of the loop, sum will have the right value, so print it.
try this:
#include <iostream>
using namespace std;
int main()
{
int positiveInteger;
int startingNumber = 1;
cout << "Please input an integer upto 100." << endl;
cin >> positiveInteger;
int result = 0;
for (int i=startingNumber; i <= positiveInteger; i++)
{
result += i;
cout << result;
}
cout << result;
return 0;
}
I have the following formula that works without loops. I discovered it while trying to find a formula for factorials:
#include <iostream>
using namespace std;
int main() {
unsigned int positiveInteger;
cout << "Please input an integer up to 100." << endl;
cin >> positiveInteger;
cout << (positiveInteger * (positiveInteger + 1)) / 2;
return 0;
}
You can try:
int sum = startingNumber;
for (int i=0; i < positiveInteger; i++) {
sum += i;
}
cout << sum;
But much easier is to note that the sum 1+2+...+n = n*(n+1) / 2, so you do not need a loop at all, just use the formula n*(n+1)/2.
mystycs, you are using the variable i to control your loop, however you are editing the value of i within the loop:
for (int i=0; i < positiveInteger; i++)
{
i = startingNumber + 1;
cout << i;
}
Try this instead:
int sum = 0;
for (int i=0; i < positiveInteger; i++)
{
sum = sum + i;
cout << sum << " " << i;
}
int result = 0;
for (int i=0; i < positiveInteger; i++)
{
result = startingNumber + 1;
cout << result;
}
First, you have two variables of the same name i. This calls for confusion.
Second, you should declare a variable called sum, which is initially zero. Then, in a loop, you should add to it the numbers from 1 upto and including positiveInteger. After that, you should output the sum.
You are just updating the value of i in the loop. The value of i should also be added each time.
It is never a good idea to update the value of i inside the for loop. The for loop index should only be used as a counter. In your case, changing the value of i inside the loop will cause all sorts of confusion.
Create variable total that holds the sum of the numbers up to i.
So
for (int i = 0; i < positiveInteger; i++)
total += i;