Write a Main Method That Will Read in 10 Integers, Find the Average, and Display It.

#ane

  • New D.I.C Head

Reputation: one

  • View blog
  • Posts: 44
  • Joined: 08-November xv

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.


Is This A Good Question/Topic? 0

  • +

#two CTphpnwb User is offline

Reputation: 3872

  • View blog
  • Posts: 14,213
  • Joined: 08-August 08

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 KING iMm0rt4lxM User is offline

  • New D.I.C Head

Reputation: 1

  • View blog
  • Posts: 44
  • Joined: 08-November xv

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 CTphpnwb User is offline

Reputation: 3872

  • View blog
  • Posts: 14,213
  • Joined: 08-August 08

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 KING iMm0rt4lxM User is offline

  • New D.I.C Head

Reputation: ane

  • View blog
  • Posts: 44
  • Joined: 08-November 15

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 ndc85430 User is offline

Reputation: 1065

  • View blog
  • Posts: 4,107
  • Joined: 13-June fourteen

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 KING iMm0rt4lxM User is offline

  • New D.I.C Head

Reputation: 1

  • View blog
  • Posts: 44
  • Joined: 08-November xv

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 CTphpnwb User is offline

Reputation: 3872

  • View blog
  • Posts: 14,213
  • Joined: 08-August 08

Re: Write an integer and displays all its factors in increasing order

Posted 09 November 2015 - 01:57 PM

See post #ii, step #4.

muellerwilver.blogspot.com

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

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel