a) Write an algorithm (pseudocode) to generate a set of triangular numbers using
ID: 3625785 • Letter: A
Question
a) Write an algorithm (pseudocode) to generate a set of triangular numbers using the formula below:TriangularNumber = n(n + 1)/2 for any integer value of n.
Generate every 5th triangular number between a given start-number and end-number using a repetition construct. End-number is assumed to be greater than start-number.
The appearance on the display is as follows ( for clarity, input data is in bold here):
Enter start number: 5
Enter end number : 20
Triangular Numbers between 5 and 20 are:- 15, 55, 120, 210
b) Implement your algorithm in C programming language. Only one funtion i.e the "main" function is to be implemented.
c) Modify your code in (b). Perform the 'generation' and 'dislay' of triangular numbers in a separate function call "genNum".
Input statements are to remain in the "main" function.
Will Rate!
Explanation / Answer
Not sure exactly how the pseudocode is supposed to be formatted, I know every teacher/professor seems to have their preference so here's the basic idea and I'll let you decide however to change it.
Algorithm
print "Enter start number:"
Get start number
print "Enter end number: "
Get end number
print "Triangular Numbers between start number and end number are: "
for(int i = start num; i <= end number; i += 5)
print i * (i + 1) / 2;
First Implementation
#include <stdio.h>
int main(void) {
int startNum, endNum;
printf("Enter start number: ");
scanf("%d", &startNum);
printf("Enter end number: ");
scanf("%d", &endNum);
printf("Triangular Numbers between %d and %d are: ", startNum, endNum);
int i;
for(i = startNum; i <= endNum; i += 5)
printf("%d ", i * (i + 1) / 2);
printf(" ");
system("PAUSE");
return 0;
}
Second Implementation
#include <stdio.h>
void genNum(startN, endN);
int main(void) {
int startNum, endNum;
printf("Enter start number: ");
scanf("%d", &startNum);
printf("Enter end number: ");
scanf("%d", &endNum);
genNum(startNum, endNum);
printf(" ");
system("PAUSE");
return 0;
}
void genNum(startN, endN) {
printf("Triangular Numbers between %d and %d are: ", startN, endN);
int i;
for(i = startN; i <= endN; i += 5)
printf("%d ", i * (i + 1) / 2);
}
If you have any questions just ask, I included an extra pause at the end of the program so you don't have to run it from command line to see the result but that can be removed without affecting anything if you wish. Rate if your happy
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.