Tony is exploring functions and needs your help. Write a program to assist Tony where he inputs an integer N. The program should display the factors of N and count how many digits N has. Help him with the program using call-by-reference.Function Specifications:To display factors - void factors(int ); To display digits - int count_digits(int *);Input format :The input consists of an integer N.Output format :The first line of output prints "Factors: " followed by the factors of N, separated by a space.The second line of output prints "Total digits: " followed by the total digits of N.
Question
Tony is exploring functions and needs your help. Write a program to assist Tony where he inputs an integer N. The program should display the factors of N and count how many digits N has. Help him with the program using call-by-reference.Function Specifications:To display factors - void factors(int ); To display digits - int count_digits(int *);Input format :The input consists of an integer N.Output format :The first line of output prints "Factors: " followed by the factors of N, separated by a space.The second line of output prints "Total digits: " followed by the total digits of N.
Solution
Here is a simple C++ program that meets your requirements:
#include <iostream>
using namespace std;
void factors(int n) {
cout << "Factors: ";
for(int i = 1; i <= n; ++i) {
if(n % i == 0)
cout << i << " ";
}
cout << endl;
}
int count_digits(int *n) {
int count = 0;
while(*n != 0) {
*n = *n / 10;
++count;
}
return count;
}
int main() {
int N;
cout << "Enter a number: ";
cin >> N;
factors(N);
int N_copy = N; // Copy of N for count_digits function
cout << "Total digits: " << count_digits(&N_copy) << endl;
return 0;
}
This program first asks the user to input a number. It then calls the factors function to print out all the factors of the number. After that, it calls the count_digits function to count the number of digits in the number. The count_digits function uses a pointer to the number so that it can modify the number as it counts the digits.
Similar Questions
Problem StatementBeula is solving a mathematical problem and seeks your assistance in writing a program. She is particularly interested in finding numbers with exactly 9 factors. Help her by creating a program that takes an integer N as input and identifies numbers from 1 to N that have exactly 9 factors.Function Specifications:check_9_factors - Prints the integers that have exactly 9 divisors as well as the total number of such integers.Note: This question helps in clearing technical coding tests for companies like Infosys.Input format :The input consists of an integer N.Output format :The first line displays space-separated integers representing numbers from 1 to N that have exactly 9 factors.The second line displays a single integer representing the count of numbers with exactly 9 factors.
Write a Java program to print the factors of a number, N got as input.Sample Input :12Sample Output :The factors of 12 are 1 2 3 4 6 12 You are provided with a shuffled code. Arrange in correct order.for(int i=1;i<=n;i++) {System.out.print("The factors of "+n+" are ");public class Test {public static void main(String args[]) {Scanner sc=new Scanner(System.in);int n = sc.nextInt();}}}import java.util.Scanner;System.out.print(i+" ");}if(n % i == 0) {
Imagine you are tasked with creating a program that calculates the cube of a given number. Your goal is to write a program that takes an integer n from the user, calculates its cube using a call-by-reference function, and then prints the result.Function Specifications: int cube(int *n) Input format :The input consists of a positive integer n.Output format :The output prints an integer which is the cube value of n.Refer to the sample output for formatting specifications.Code constraints :
Complete the function Numberofdigit() to count the number of digits of the given input and return the result.Input Format:No need to read input , as it is predefined in main.Output Format:No need to print just return the count from functionConstraints:1 <= number <= 10^9Sample Input 1:1000Sample Output 1:4Sample Input 2:123Sample Output 2:3
Count the number of factors for the given numberInput Format:Accept an integer as inputOutput Format:Print the output as Count value in integer formatConstraints:1 <= N <= 10^15Sample Input 1:32Sample Output 1:6Sample Input 2:10Sample Output 2:4
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.