(1) Using Visual Studio community c# Please create the code, debug and complete
ID: 3601672 • Letter: #
Question
(1)
Using Visual Studio community
c#
Please create the code, debug and complete Programming :
"Calories Burn" Running on a particular treadmill you burn 3.6 calories per minute. Write a program that uses a loop to display the number of calories burned after 5, 10, 15, 20, 25, and 30 minutes
**Modify the program to allow user to enter the number of calories per minute burned and then display the results. Use a loop as instructed.
-------------------------------------------------------------------------------------------------------------------------------
(2)
Using Visual Studio community
c#
Please create the code, debug and complete Programming :
"Sum of Numbers" Write a program that asks the user for a positive integer value. The program should use a loop to get the sum of all the integers from 1 up to the number entered. For example, if the user enters 50, the loop will find the sum of 1, 2, 3, 4, ... 50.
Input Validation: Do not accept a negative starting number.
You will need at least 4 functions: menu, summation, factorial, and exponential.
Make sure that your header block shows that you have functions and how information is passed between them. Each function should have it's own header block. A header block is comprised of several lines of comments at the top of each function that explain what the function does.
Using Visual Studio community PLEASE!!! C#
Explanation / Answer
1)
using System;
// Class Program defined
class Program
{
// main function definition
static void Main(string[] args)
{
// To store minutes entered by the user
int minute;
// To store calculated calories
float calories = 0;
// Accepts number of minutes entered by the user and converts it to integer
Console.WriteLine("Enter minutes:");
minute = Convert.ToInt32(Console.ReadLine());
// Counter is initialized to one
int c = 1;
// Loops till number of minutes entered by the user
while (c <= minute)
{
// Calculates calories
calories += 3.6f;
// Displays number of minutes
Console.Write(" Calories burned after ");
Console.Write(c);
// Displays calories
Console.Write(" Minutes Calories = ");
Console.Write(calories);
// Increase the counter by one
c++;
}// End of while loop
Console.ReadKey();
}// End of main
}// End of class
Sample Run:
Enter minutes:3
Calories burned after 1 Minutes
Calories = 3.6
Calories burned after 2 Minutes
Calories = 7.2
Calories burned after 3 Minutes
Calories = 10.8
-----------------------------------------------------------------------------------------------------------
2)
using System;
// Class Program defined
class Program
{
// Function to display menut
// Accept user choice and return it
static int menu()
{
// To store user choice
int choice;
// Display menu
Console.WriteLine("1 - Sum");
Console.WriteLine("2 - Facrotial");
Console.WriteLine("3 - Exponential");
Console.WriteLine("4 - Exit");
// Accept choice
Console.Write("Enter your choice: ");
// Convert it into integer
choice = Convert.ToInt32(Console.ReadLine());
// Return user choice
return choice;
}// End of function
// Function to calculate and return sum till the number entered by the user
static int sum(int no)
{
// To store the result
int res = 0;
// Loops till the number entered by the user
for (int x = 1; x <= no; x++)
// Calculates sum
res += x;
// Returns sum
return res;
}// End of function
// Function to calculate and return factorial of the number entered by the user
static int facrorial(int no)
{
// To store the result
int res = 1;
// Loops till the number entered by the user
for (int x = 2; x <= no; x++)
// Calculates factorial
res *= x;
// Returns facrorial
return res;
}// End of function
// Function to calculate and return 2 to the power of the number entered by the user
static int exponent(int no)
{
// Calculates and returns power
return (int)Math.Pow(2, no);
}// End of function
// Function to accept, validate and return a number entered by the user
static int accept()
{
// To store the number entered by the user
int number;
// Loops till positive number entered by the user
do
{
// Accepts number of minutes entered by the user and converts it to integer
Console.WriteLine("Enter a positive number:");
number = Convert.ToInt32(Console.ReadLine());
// Checks if the number is greater than zero then return the number otherwise ask again to enter number
if (number > 0)
return number;
else
Console.WriteLine("Error: Invalid number!");
}while(true);
}// End of function
// main function definition
static void Main(string[] args)
{
int number;
int choice;
int res;
// Loops till user coice is not 4
do
{
// Calls the function to display menu and store the return result in variable choice
choice = menu();
// Checks if the choice is 4 stop
if(choice == 4)
System.Environment.Exit(0);
// Calls the function to accept and validate the numebr entered by the user
number = accept();
// Checks the choice and calls appropriate function and display result
if(choice == 1)
{
res = sum(number);
Console.Write("Sum = ");
Console.WriteLine(res);
}
else if(choice == 2)
{
res = facrorial(number);
Console.Write("Facrotial = ");
Console.WriteLine(res);
}
else if(choice == 3)
{
res = exponent(number);
Console.Write("Exponent of 2 = ");
Console.WriteLine(res);
}
else
Console.Write("Invalid choice!");
}while(true);
}// End of main
}// End of class
Sample Run:
1 - Sum
2 - Facrotial
3 - Exponential
4 - Exit
Enter your choice: 1
Enter a positive number:
6
Sum = 21
1 - Sum
2 - Facrotial
3 - Exponential
4 - Exit
Enter your choice: 2
Enter a positive number:
4
Facrotial = 24
1 - Sum
2 - Facrotial
3 - Exponential
4 - Exit
Enter your choice: 3
Enter a positive number:
-2
Error: Invalid number!
Enter a positive number:
3
Exponent of 2 = 8
1 - Sum
2 - Facrotial
3 - Exponential
4 - Exit
Enter your choice: 4
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.