Question

4. Write a C++ program to find and display factors of a number. You can use either input from a user or created random number
0 0
Add a comment Improve this question Transcribed image text
Answer #1

C++ Program to Display Factors of a Number

Factors of a number are the numbers which completely divides it without leaving any remainders .

eg . Factors of 6 = 1 , 2 , 3 , 6

Steps :

1. Declare a variable ( n ) of which factors to be find .

2. Take input from the user using cin>> n ;

3. Use a for loop from i=1 to i=n and check if ( n % i ==0 ) or not .

4. if ( n % i ==0 ) that is if remainder of n divide by i is 0 then i is one of the factor of n .

5. and hence print value of ' i ' using cout<< i ;

Note:

cin is used with extraction operator ">>" to take user inputs .

cout is used with insertion operator "<<" to show the output .   

Code :

#include<iostream> /**cin/cout are object of input/output stream (iostream) **/
using namespace std;

int main()
{
int n; /** declaring the number for which numbers to find **/
cout<<"Enter the number :" ;
cin>> n ; /** Taking input from user **/
cout<<"Factors of "<< n <<": ";
for(int i=1;i<=n;i++) /** for loop run from i=1 to i=n **/
{
if(n%i==0)
{
cout<< i<<" " ; /** if i divides n it means 'i' is a factor of 'n' , hence print 'i'**/

}
}
return 0;

}

ScreenShots :

finding_factor.cpp - Code:Blocks 17.12 File Edit View Search Project Build Debug Fortran wxSmith Tools Tools+ Plugins DoxyBlo

Output :

- 0 x tinding_factor.cpp - Code:Blocks 17.12 File Edit View Search Project Build Debug Fortran wxSmith Tools Tools+ Plugins D

Taking input using rand():

rand() is a function defined inside cstdlib , which is used to generate random numbers .

If you want to take input using rand() then just include<cstdlib> and then store this random number in any variable and use it as input .

#include<cstdlib>

int n ;

n = rand() ;   

just put this code for taking input from rand() .

Add a comment
Know the answer?
Add Answer to:
4. Write a C++ program to find and display factors of a number. You can use...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a C++ program to solve S = 1 + 1/2 + 1/3 + …. +...

    Write a C++ program to solve S = 1 + 1/2 + 1/3 + …. + 1/n. You can use either input from a user or created random number using rand() function.

  • In HTML write a program that calculates and displays all factors of a number with a...

    In HTML write a program that calculates and displays all factors of a number with a function. A factor is any number that divides into a number evenly. For examples: Factors of 20 are 1,2,4,5,10,20 For the program: Prompt the user for a number or use an input Call a function to calculate all factors using loops and conditionals and modulus Display all factors to the page Bonus +5 If the factor is Even : Print it in Green If...

  • In this program, you will be using C++ programming constructs, such as functions. main.cpp Write a...

    In this program, you will be using C++ programming constructs, such as functions. main.cpp Write a program that asks the user to enter an integer value. Your program will then display that integer back to the user. Your program should include a function called getInteger that requests an integer value from the user and returns that value back to the caller. Your main () function will call the function getInteger and will then display the value returned by that function....

  • C++ C++ Write a program to input eight integer numbers into an array named grade. As...

    C++ C++ Write a program to input eight integer numbers into an array named grade. As each number is input, add the number into a total. After all numbers are input, display the numbers and their average. (You can use cin to take number or use rand() to assign random numbers to the array.)

  • Write a C++ program that will create an array with a given number of elements. Use...

    Write a C++ program that will create an array with a given number of elements. Use size = 10 for demonstration purposes, but write the program where the size of the array can be changed by changing the value of one constant. Fill the array with random numbers between 0 and 100. Write the program to perform the following operations: 1. Find and display the largest value in the array. 2. Find and display the smallest value in the array....

  • Using c++.. 1. Write a program to find the sum(), Subtraction(), Multiplication(), Division() operations using Switch...

    Using c++.. 1. Write a program to find the sum(), Subtraction(), Multiplication(), Division() operations using Switch statement and functions. 2. Write a program to find the summation of N numbers. Use two functions. One function will take the input from user and the other will perform the summation from 1 to N. 3. Write a program to find the factorial of a number. Use two functions. One function will take the input from user and the other will perform the...

  • Write the Code in C program. Create a random password generator that contains a user-specified number...

    Write the Code in C program. Create a random password generator that contains a user-specified number of characters. Your program should prompt the user for the desired length of the password. Length of password: To create your password, use the random number generator in the stdlib.h C library. To generate random numbers, you will need the srand() and rand() functions. srand(seed) is used to "seed" the random number generator with the given integer, seed. Prompt the user for the seed...

  • In C language using printf and scanf statements: Write a program to display a histogram based...

    In C language using printf and scanf statements: Write a program to display a histogram based on a number entered by the user. A histogram is a graphical representation of a number (in our case, using the asterisk character). On the same line after displaying the histogram, display the number. The entire program will repeat until the user enters zero or a negative number. Before the program ends, display "Bye...". The program will ask the user to enter a non-zero...

  • C++ programming please Write a program that will display random numbers in order from low to...

    C++ programming please Write a program that will display random numbers in order from low to high. 1. Declare an integer array of size 100. Ask the user how many numbers to work with (n). It can be less than 100. 2. Generate random numbers from 10 to 50. 3. Write the random numbers to an output file named random.dat. Close the file. 4. Open random.dat for input and read the data values into your array. Pass your array to...

  • In this program, you will be using C++ programming constructs, such as overloaded functions. Write a...

    In this program, you will be using C++ programming constructs, such as overloaded functions. Write a program that requests 3 integers from the user and displays the largest one entered. Your program will then request 3 characters from the user and display the largest character entered. If the user enters a non-alphabetic character, your program will display an error message. You must fill in the body of main() to prompt the user for input, and call the overloaded function showBiggest()...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT