Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

c++ program to demonstrate RECURSION James has written a number pattern that beg

ID: 3693886 • Letter: C

Question

c++ program to demonstrate RECURSION

James has written a number pattern that begins with 1, 5, 14, and 30. Write a program that will help James to figure out and display the first twenty numbers of the pattern; for each number, its corresponding ordinal number must be displayed as well, in a tabular format (see sample run below). The program will then, at the user’s choice entered at the run time, figures out and displays any particular number, with its corresponding ordinal number, in the pattern. For the user’s interest to find another number, allow the program to loop back continuously for any other element in the pattern to be revealed. Lastly, your full name must be displayed on screen before the ending of the program. Validate your input value so that your program won’t be crashed at run time by any reason. If it crashed there will be no credit, or 0 point.

Sample run: Your output must be similar to the following: The first twenty numbers in the pattern: Order Number 1st 1 2nd 5 3rd 14 4th 30 ... What's the ordered element in the pattern that you wanted to find? (Or enter 0 to end): 31 The 31st element is: 10416 What's the ordered element in the pattern that you wanted to find? (Or enter 0 to end): 4300 The 4300th element is: 26511579050 What's the ordered element in the pattern that you wanted to find? (Or enter 0 to end): 52 The 52nd element is: 48230 What's the ordered element in the pattern that you wanted to find? (Or enter 0 to end): 0 GOOD BYE! Programmer: Your Full Name Press key to end ...

Explanation / Answer

//Here is the program in C++

#include <iostream>
#include <stdio.h>
#include<stdlib.h>
#include<math.h>
using namespace std;
int main() {
   unsigned long long int init=1;
   int i,elem;
   char str[60];
   cout<<"Enter your full name ";
   gets(str);
for(i=2;i<21;i++)
{
   init=init+pow(i,2);
   //printf("%d is pow and init is %d ",pow(i,2),init);
   cout<<"The "<<i<<"th element is "<<init<<" ";
}
cout<<"What's the ordered element in the pattern that you wanted to find? (Or enter 0 to end): ";
cin>>elem;
while(elem!=0)
{
   init=1;
   for(i=2;i<=elem;i++)
{
   init=init+pow(i,2);

}
cout<<"The "<<elem<<"th element is "<<init<<" ";
cout<<"What's the ordered element in the pattern that you wanted to find? (Or enter 0 to end): ";
cin>>elem;
}
cout<<"GOOD BYE! Programmer:"<<str<<" ";
   return 0;
   }

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote