Visual C# 5.21 (Find The Largest Number)

This Solution is for the “Find The Largest Number Exercise” of chapter 5 in the textbook; Visual c# How to program 6th edition by Deitel.

/*Description:
*
program finds the largest number out of 10 numbers entered by the user
using counter controlled iteration */

/* pseudocode:
*
initialize counter, input, & largestnum variables to 0

prompt user for first input
set first number to input
initialize largestnum as input

initialize counter to 1

while the counter is less than 10
prompt user for next input
set next number to input
if new input is greater than the largestnum
make the new input largestnum

increment counter 1

Print largestnum
*/
using System;
class Program
{
public static void Main()
{
//initializes variables
int counter;
int input;
int largestnum;

//prompts user for first number
Console.Write(” Please enter a number: “);
largestnum = int.Parse(Console.ReadLine());//reads input into memory

//sets counter variable to 1
counter = 1;

//while statement that prompts user for another number,
//reads input into memory on a loop as long as counter is under 10
while (counter < 10)
{
Console.Write(“Please enter another number: “);
input = int.Parse(Console.ReadLine());

//checks if the input from this loop is larger than Largestnum variable
if (input > largestnum)
largestnum = input;// if so sets the new input as largestnum

++counter;//increments counter by 1
}

//prints largestnum variable
Console.WriteLine($”The Largest number you entered is: {largestnum}.”);
}
}

Output:
Please enter a number: 45
Please enter another number: 2
Please enter another number: 12
Please enter another number: 43
Please enter another number: 98
Please enter another number: 500
Please enter another number: 37
Please enter another number: 72
Please enter another number: 64
Please enter another number: 18
The Largest number you entered is; 500.
Press any key to continue . . .

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s