Programming in C
Programming in C
4th Edition
ISBN: 9780321776419
Author: Stephen G. Kochan
Publisher: Addison-Wesley
bartleby

Concept explainers

bartleby

Videos

Textbook Question
Book Icon
Chapter 4, Problem 8E

Program 4.5 allows the user to type in only five different numbers. Modify that program so that the user can type in the number of triangular numbers to be calculated.

Expert Solution & Answer
Check Mark

Explanation of Solution

Modified Program:

The following program will ask the user to enter the total number of triangular they want and calculate the triangular number using nested “for” condition:

//include the header file

#include <stdio.h>

//definition of main method

int main (void)

{

//declare the variables

int n, number, triangularNumber, counter, count;

//get the input from the user

printf("How many triangular would user want?");

scanf("%i", &count);

//check the condition

for (counter = 1; counter <=count; ++counter)

{

//get the input from the user

printf("What triangular number do you want? ");

scanf("%i", &number);

//set the value

triangularNumber = 0;

//check the condition

for (n = 1; n <=number; ++n)

//calculate the "triangularNumber" value

triangularNumber = triangularNumber + n;

//display the result

printf("Triangular number %i is %i\n", number, triangularNumber);

}

//return statement

return 0;

}

Explanation:

In the above program, declare the required header file. Inside the main method, declare the necessary variables and ask the user to enter the total number of triangular they want. The “for” condition is used to get the input from the user then nested “for” condition is used to iterate the “n” value up to the user entered input and inside the loop calculate the triangular of “n” value and display the result on the output screen.

Sample Output

How many triangular would user want? 2

What triangular number do you want?  12

Triangular number 12 is 78

What triangular number do you want?  25

Triangular number 25 is 325

Want to see more full solutions like this?

Subscribe now to access step-by-step solutions to millions of textbook problems written by subject matter experts!
Students have asked these similar questions
Write the following function to draw a regular polygon:def drawPolygon(x = 0, y = 0, radius = 50, numberOfSides = 3):The polygon is centered at (x, y) with a specified radius for the bounding circle for the polygon and the number of sides. Write a test program that displays a triangle, square, pentagon, hexagon, heptagon, and octagon, as shown in Figure 6.12a.
6. Write a program to generate random numbers between the given range?.
C++, How do I write a program that asks how many tickets for each class of seats were sold, then displays the amount of income generated from ticket sales? Class A costs $15, Class B costs $12, and class C costs $9.

Additional Engineering Textbook Solutions

Find more solutions based on key concepts
This key word causes an object to be created in memory. a. create b. new c. object d. construct

Starting Out with Java: From Control Structures through Data Structures (4th Edition) (What's New in Computer Science)

Refer to your answer to Checkpoint 14.35. In what package are the three classes?

Starting Out with Java: From Control Structures through Objects (7th Edition) (What's New in Computer Science)

The conditional operator takes this many operands. a. one b. two c. three d. four

Starting Out with Java: From Control Structures through Objects (6th Edition)

A(n) _______ eliminates the need to place a function definition before all calls to the function.

Starting Out with C++ from Control Structures to Objects (9th Edition)

Knowledge Booster
Background pattern image
Computer Science
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning
Control Structure in Data Structure - Data Structures - Computer Science Class 12; Author: Ekeeda;https://www.youtube.com/watch?v=9FTw2pXLhv4;License: Standard YouTube License, CC-BY