Write a Main Method That Will Read in 10 Integers, Find the Average, and Display It.
#ane
Write an integer and displays all its factors in increasing social club
Posted 09 November 2015 - 08:04 AM
(Find the factors of an integer) Write a program that reads an integer and displays
all its smallest factors in increasing order. For example, if the input integer is 120,
the output should be as follows: 2, 2, 2, 3, 5.
#include <iostream> #include <iomanip> #include <cmath> #include <string> using namespace std; int main() { int integer = ii; int n; cout << "Enter an integer: "; cin >> integer; while (integer != 0) { if (integer % ii == 0) n = two organization("pause"); return 0; }
I'm having a difficult time figuring out how to pause the number apart. I know I demand to start with a 2 and practice some math. and probably the while loop should not equal 1.
#two
Re: Write an integer and displays all its factors in increasing order
Posted 09 Nov 2015 - 08:17 AM
The brute force method:
- Set the divisor to ii.
- Is the number divisible by the divisor? If and so, print the divisor, divide by the number by the divisor, and echo this pace
- Increment the divisor.
- Is the divisor less than or equal to the number? If so, go back to footstep ii. If not, you're finished.
If the number were 20:
twenty % 2 == 0, and so impress 2, set the number to xx/2 = ten
10 % ii == 0, and so impress 2, fix the number to ten/2 = 5
five % ii == 1, so increase divisor to 3
5 % three == two, and then increase divisor to 4
5 % 4 == one, so increment divisor to five
v % 5 == 0, then print five, increment divisor to half dozen
half dozen is not less than or equal to v, and so we're done.
This post has been edited by CTphpnwb: 09 November 2015 - 08:18 AM
#3
Re: Write an integer and displays all its factors in increasing society
Posted 09 November 2015 - 11:17 AM
Give thanks y'all, I got to the trouble to piece of work
#4
Re: Write an integer and displays all its factors in increasing order
Posted 09 November 2015 - eleven:xx AM
Working and done correctly are two different things. Yous might want to postal service your solution so others tin can critique it. Judging by postal service #1 I'd expect there to be numerous improvements that could exist fabricated.
#5
Re: Write an integer and displays all its factors in increasing lodge
Posted 09 November 2015 - 12:xi PM
That'south what I got, although for some reason the loop does non stop
#include <iostream> using namespace std; int main() { int integer;; int divisor = 2; cout << "Enter an integer: "; cin >> integer; while (divisor != 1) { if (integer % divisor == 0) { cout << divisor; integer = integer / divisor; } else divisor++; } cout << "The factors of the integer are " << divisor << endl; arrangement("pause"); return 0; }
Don't mind the afterwards ; on int integer;;. I was trying to notice how to edit the text just I can't discover the option to.
#half dozen
Re: Write an integer and displays all its factors in increasing order
Posted 09 November 2015 - 12:fifteen PM
Of course the loop doesn't terminate - you don't have a condition that will brand divisor equal to 1. It starts at 2 and then either increases or stays at 2.
#7
Re: Write an integer and displays all its factors in increasing order
Posted 09 November 2015 - 12:45 PM
yeah but I can't figure out how to ended it at the moment, I volition eventually though
#8
Re: Write an integer and displays all its factors in increasing order
Posted 09 November 2015 - 01:57 PM
See post #ii, step #4.
Source: https://www.dreamincode.net/forums/topic/384290-write-an-integer-and-displays-all-its-factors-in-increasing-order/
0 Response to "Write a Main Method That Will Read in 10 Integers, Find the Average, and Display It."
Post a Comment