answersLogoWhite

0

How do you do an alternate numbers in c plus plus?

Updated: 8/20/2019
User Avatar

Wiki User

11y ago

Best Answer

If I'm understanding correctly, you want to alternate between two numbers back and forth?

If so, try something like this:

/* num1 is the number to be alternated

and alt is the amount of times do so */

int num1 = 1;

int alt;

/* Get number of times to alternate */

cout << "Enter number of times to alternate: ";

cin >> alt;

/* Perform alternation and cout result each time */

for (int i = 0; i < alt; i++)

{

num1 *= -1; // in this case, alternating positive/negative

cout << num1 << endl;

}

Note that you can change the "num1 *= -1" to whatever you need.

For example, to alternate between 50 and 25, you would put

/* Starting with 50, then alternating to 25 for the first cout */

num1 = 50;

/* On odd times through (1,3,5), ADD 25;

On even times through (0,2,4...etc) SUBTRACT 25 */

if ( (i % 2) == 1)

{

num1 += 25;

}

else

{

num1 -= 25;

}

User Avatar

Wiki User

11y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How do you do an alternate numbers in c plus plus?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

How do you alternate the input numbers using for loop in c plus plus?

Input a variable.


How do you add more than two numbers in C plus plus?

sum = a + b + c;


How do you write a macro to find the biggest of 3 numbers in c plus plus?

#define biggest (a) &gt; (b) &amp;&amp; (a) &gt; (c) ? (a) : (b) &gt; (c) ? (b) : (c)


How do you write a C plus plus program that will display the first 10 positive prime numbers?

By learning how to program on C+.


How do you play a c sharp on alto sax?

you can play it by not pressing down any keys. OR for low c#, all six keys plus the second left pinky key (low C plus the pinky). this can be alternate fingering with an octave key


What is IEEE standard in c plus plus?

The format of floating-point numbers. On some platforms.


C program to print numbers 1 to n?

how do we use loops in c plus plus programing and what are basic differences between do,for and while loop


C plus plus programming for addition of two numbers?

int main() { int x = 40 + 2; }


A program c plus plus on automorphic numbers or not?

how to write a program that counts automorphic number from 1 to 999


Write a c plus plus program to find the largest among three numbers using ternary operators?

R = (A &gt; B &amp;&amp; A &gt; C) ? A : (B &gt; C) ? B : C; // parentheses not necessary - for clarity only


What property is 9 plus l-bracket 9 plus 0 r-bracket equals l-bracket 9 plus 9 r-bracket plus 0?

This is the associative law for addition: If a, b, c are any numbers then: a + (b + c) = (a + b) + c


Quadratic equation in standard form is written ax2 plus bx plus c equals 0 where a b and c are real numbers and a is not zero?

Yes that about sums it up.