JAVA Write a main method that prompts user for an integer. Main then calls the t
ID: 3669789 • Letter: J
Question
JAVA
Write a main method that prompts user for an integer. Main then calls the
three methods on the input number and prints the results.The results MUST
printed from main not from the individual methods.
printSequence: Taking a positive or negative integer as input, then printing either:
all integers starting from 0 to the input number if number is positive, or all integers from 0 down to the input number if the number is negative.
tringularNum: Taking a positive integer as input. It then computes and returns the triangular number of the input integer. The triangular
number of an integer is defined as the sum of all numbers from 1 up to the given integer.The triangular number 5 is 1 + 2 + 3 + 4 + 5 = 15.
factorial: Taking a positive integer as input, then computes and returns the factorial of the input integer.
Explanation / Answer
/**The java progra JavaProgram that tests the three methods
* printSequence, tringularNum and factorial metod.
* The program prompts user to enter a number and finds the
* sequence, triangular number and factorial of a number*/
//JavaProgram.java
import java.util.Scanner;
public class JavaProgram
{
public static void main(String[] args)
{
//Create a scanner class object
Scanner scanner=new Scanner(System.in);
int num;
System.out.println("Enter a number ");
//read num from console
num=scanner.nextInt();
System.out.println("Sequence of Numbers from 0 to "+num);
//Calling printSequence method
String sequence=printSequence(num);
System.out.println(sequence);
//Calling tringularNum method
int triangleNumber=tringularNum(num);
System.out.println("Triangular Number of "+num+" is "+triangleNumber);
//Calling factorial method
int fact=factorial(num);
System.out.println("Factorial of a number "+num+" is "+fact);
}
/**The method that takes num as input integer value and returns the
* factorial of a number*/
private static int factorial(int num)
{
if(num==0 || num==1)
return 1;
else
return num*factorial(num-1);
}
/*Returns the sum of the numbers from 1 to given input number ,num
and returns the triangular num */
private static int tringularNum(int num)
{
//Initialize sum to zero
int sum=0;
for (int i = 1; i <=num; i++)
{
sum+=i;
}
//returns sum value
return sum;
}
/**The method printSequence that takes an input argument num
* and returns the sequence of the numbers */
private static String printSequence(int num)
{
String sequence="";
//Check if num is negative
if(num<0)
{
//create a sequence of negative numbers
for (int i = num; i <=0; i++)
sequence+=i+",";
}
else
//create a sequence of positive numbers
for (int i = 0; i<=num; i++)
sequence+=i+",";
return sequence;
}
}
------------------------------------------------------------------------------------------------------------------------------------------------
Sample Output:
Enter a number
5
Sequence of Numbers from 0 to 5
0,1,2,3,4,5,
Triangular Number of 5 is 15
Factorial of a number 5 is 120
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.