Write a main method that prompts user for an integer. Your main then calls the t
ID: 3669662 • Letter: W
Question
Write a main method that prompts user for an integer. Your main then calls the
three methods on the input number and prints the results. Make sure that the results are
printed from main not from the individual methods.
printSequence: Method 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: Method 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: Method taking a positive integer as input. It then
computes and returns the factorial of the input integer.
Explanation / Answer
#include<iostream.h>
#include<conio.h>
//main function from which methods are called
void main()
{
//variable and function definitions
int n,f,t;
int fact(int);
int triangle(int);
printseq(int);
clrscr();
//Getting user Input
cout<<"Enter the number:->";
cin>>n;
//Calling the functions
printseq(n);
f=fact(n);
//Printing results
cout<<endl<<"The factorial of the Number is: "<<f<<endl;
t=triangle(n);
cout<<endl<<”The triangular number of the given input is:”<<t<<endl;
getch();
}
//Function to calculate factorial
fact(int x)
{
int f1;
if(x==1)
return(x);
else
{
f1=x*fact(x-1);
return(f1);
}
//Function to calculate triangular number
triangle(int y)
//Function for printing a sequence of number
printseq(z)
{
int j;
If z>0 then
{
for(j=0;j<=z;j++)
{
cout<<endl<<”The Print Sequence till the given input is:”<<j<<endl;
}}
else
{
for(j=0;j<=z;j--)
{
cout<<endl<<”The Print Sequence till the given input is:”<<j<<endl;
}}
}
return(0)
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.