Question

I have a CS Lab and obviously i'm not asking anyone to write the full code, but can you guys point me in the right direction? Thanks! Here's the lab:


*Write a program to determine if a natural number has only 2 and/or 3 as prime factors and how many of each factor (2 and 3) it does have. Write your program from scratch (you can reference other examples to get started with the basic structure of a program) and name it prime23.cpp. The program should meet the following requirements:

a. Prompt (print a message to the user) to enter a natural number. [i.e. use cout] b. Receive the integer input from the user. [i.e. use cin ] c. Implement your algorithm (using while loops and if statements). d. Print either “Yes” and a count of 2 factors and a count of 3 factors (i.e. an input of 24 would print: Twos=3, Threes=1) or “No” if the number has neither a factor of 2 nor 3.*


Quick description of my code so far. I have a While loop nested inside an If statement and am using the modulus operator often. Thanks guys.

Was it helpful?

Solution

Well, I'm not going to write the whole code. You would probably be needing a counter variable for counting the number of two and three factors.

while((num % 2 == 0) || (num % 3 ==0))
{
if (num % 2 == 0)
 {
 twoFactCounter++;
 num /= 2;
 }
else if (num % 3 == 0)
 {
 threeFactCount++;
 num /= 3;
 }
}

//cout the vars

;)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top