C# Program to Calculate the Simple Interest.

Calculating simple interest is a basic financial calculation used to determine the interest earned or paid on a principal amount over a specific period. In this article, we'll learn how to write a C# program to calculate simple interest.


Problem Statement: Write a C# program to calculate the simple interest given the principal amount, rate, and time.

Example:

Suppose the principal amount is $1000, the interest rate is 5%, and the time is 3 years. The program should output:

Simple Interest: $150.00

Steps to Find Simple Interest.

Below are steps that you need to follow to calculate simple interest:

Step 1: Take inputs for principal amount, rate, and time from the user.
Step 2: Calculate simple interest using the formula: Simple Interest = (Principal * Rate * Time) / 100.
Step 3: Print the calculated simple interest.

C# Code to Find Simple Interest.

// C-sharp code implementation to calculate simple interest
using System;

namespace SimpleInterestCalculator
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("Enter the principal amount: ");
            double principal = Convert.ToDouble(Console.ReadLine());

            Console.Write("Enter the interest rate (in percentage): ");
            double rate = Convert.ToDouble(Console.ReadLine());

            Console.Write("Enter the time (in years): ");
            double time = Convert.ToDouble(Console.ReadLine());

            double simpleInterest = (principal * rate * time) / 100;

            Console.WriteLine($"Simple Interest: ${simpleInterest:F2}");
        }
    }
}
Output:
Enter the principal amount: 1000
Enter the interest rate (in percentage): 8
Enter the time (in years): 4
Simple Interest: $320.00

Similar articles:

C# Program to Find the largest among three numbers.

In many programming scenarios, you might need to determine the largest number among a set of values. This is a common problem and can be solved using conditional statements. In this article, we'll learn how to write a C# program to find the largest among three numbers.


Problem Statement: Write a C# program that finds the largest among three given numbers.

Example:

Input: num1 = 10, num2 =12, num3 = 7
Output: Largest Number = 12

Input: num1 = 8, num2 = -2, num3 = 5
Output: Largest Number = 8

C# Code to Find the Largest Number Among Three.

//C-sharp code to find largest among three numbers
using System;

namespace LargestNumberFinder
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("Enter three numbers: ");
            int num1 = Convert.ToInt32(Console.ReadLine());
            int num2 = Convert.ToInt32(Console.ReadLine());
            int num3 = Convert.ToInt32(Console.ReadLine());

            int largest = num1;

            if (num2 > largest)
            {
                largest = num2;
            }

            if (num3 > largest)
            {
                largest = num3;
            }

            Console.WriteLine($"Largest number among {num1}, {num2}, and {num3} is {largest}.");
        }
    }
}
Output:
Enter three numbers: 10
23
9
Largest number among 10, 23, and 9 is 23.

Code Explanation:
  • The program takes three integers as input using the Console.Write and Console.ReadLine functions.
  • It initializes the largest variable with the value of the first input.
  • Using a series of if statements, it compares the other two inputs with the largest variable and updates it if a larger value is found.

C# Program to Calculate the Factorial of a Number using Loop.

Factorial is a mathematical operation that is used quite often in programming. It involves multiplying a given number by all positive integers that are less than it. In this article, we'll explore how to write a C# program to calculate the factorial of a given number.

Problem Statement: Write a C# program that calculates the factorial of a positive integer.

Example:

Input: num = 5
Output: Factorial of 5 = 120

Explanation: 5 x 4 x 3 x 2 x 1 = 120

Input: num = 7
Output: Factorial of 7 = 5040

Steps to Find Factorial of a Number.

Below are the steps that need to be followed to Find a Factorial of a number in C-Sharp.

Step 1: Take a positive integer input from the user.
Step 2: Initialize a variable factorial to 1. This will store the result of the factorial operation.
Step 3: Use a loop to iterate from 1 to the input number.
Step 4: In each iteration, multiply the factorial by the current iteration value.
Step 5: After the loop completes, the factorial will hold the calculated factorial value.
Step 6: Print the result.

C# Code to Find Factorial of a Number.

// C-sharp code to find factorial of a number.
using System;

namespace FactorialCalculator
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("Enter a positive integer: ");
            int number = Convert.ToInt32(Console.ReadLine());

            int factorial = 1;
            for (int i = 1; i <= number; i++)
            {
                factorial *= i;
            }

            Console.WriteLine($"Factorial of {number} is {factorial}.");
        }
    }
}
Output:
Enter a positive integer: 7
Factorial of 7 is 5040.

Code Explanation:

1. The program takes a positive integer as input using the Console.Write and Console.ReadLine functions.

2. It then uses a for loop to iterate from 1 to the input number.

3. Inside the loop, the factorial variable is updated by multiplying it with the current value of i.

4. After the loop completes, the program prints the calculated factorial value.

C# Program to check if a given number is Even or Odd.

Even numbers are those that are divisible by 2 without leaving a remainder, while odd numbers are not divisible by 2 without a remainder. In this article, we will explore how to write a simple C# program to check if a given number is even or odd.


Problem Statement: Write a C# program that takes an integer as input and determines whether it is an even or odd number.

Example:

Input: num = 7
Output: 7 is an Odd Number.

Explanation: 7 % 2 = 1

Input: num = 10
Output: 10 is an Even Number.

Explanation: 10 % 2 = 0

Steps to Check Even and Odd Numbers.

Below are the steps that you need to follow to check if the given number is odd or even.
Step 1: Start by taking an integer input from the user.
Step 2: Use the modulo operator % to check if the remainder of dividing the input number by 2 is zero or not.
Step 3: If the remainder is zero, it's an even number. Otherwise, it's an odd number.
Step 4: Print the appropriate message based on the result.

C# code to check Even and Odd Numbers.

// C-sharp code check if a number is even or odd
using System;

namespace EvenOddChecker
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("Enter an integer: ");
            int number = Convert.ToInt32(Console.ReadLine());

            if (number % 2 == 0)
            {
                Console.WriteLine($"{number} is an Even number.");
            }
            else
            {
                Console.WriteLine($"{number} is an Odd number.");
            }
        }
    }
}
Output:
Enter an integer: 12
12 is an Even number.

Code Explanation:

We use the Console.Write and Console.ReadLine functions to take input from the user. The % operator calculates the remainder when the number is divided by 2. If the remainder is zero, the number is even; otherwise, it's odd.

C# Program to Convert Fahrenheit to Celsius.

In this article, we'll explore how to create a simple C# program that takes a temperature in Fahrenheit as input and converts it to Celsius.
Convert Fahrenheit to Celsius.

Problem Statement: Write a C# program that converts a temperature in Fahrenheit to Celsius. Take the temperature in Fahrenheit as user input and display the converted temperature in Celsius.

Example:
Input:
Fahrenheit: 98.6

Output:
98.6°F is equal to 37°C

The formula to convert temperature from Fahrenheit (°F) to Celsius (°C) is as follows:
Celsius (°C) = (Fahrenheit (°F) - 32) × 5/9

C# Code to Convert Fahrenheit to Celsius.

// C-sharp code to calculate celsius
using System;

namespace TemperatureConverter
{
    class Program
    {
        static void Main(string[] args)
        {
            // Read temperature in Fahrenheit from the user
            Console.Write("Enter the temperature in Fahrenheit: ");
            double fahrenheit = Convert.ToDouble(Console.ReadLine());

            // Convert Fahrenheit to Celsius using the formula
            double celsius = (fahrenheit - 32) * 5 / 9;

            // Display the converted temperature
            Console.WriteLine($"{fahrenheit} F is equal to {celsius:F2} C");

            // Keep the console window open
            Console.ReadLine();
        }
    }
}
Output:
Enter the temperature in Fahrenheit: 45
45 F is equal to 7.22 C

C# Program to Calculate Area of Rectangle.

Calculating the area of a rectangle is a fundamental mathematical operation often encountered in programming. In this article, we'll explore how to write a simple C# program to calculate the area of a rectangle using user-provided input. 


Problem Statement: Write a C# program that takes the length and width of a rectangle as user input and calculates its area. Display the result to the user.

Example: 

Input:
Length (L): 8
Width (W): 5

Output: The area of the rectangle is: 40

Explanation: 
Area = L x W
     = 8 x 5
     = 40

C# Code to Calculate Area of Rectangle.

//C-sharp code to calculate area of Rectangle
using System;

namespace RectangleAreaCalculator
{
    class Program
    {
        static void Main(string[] args)
        {
            // Read length and width from the user
            Console.Write("Enter the length of the rectangle: ");
            double length = Convert.ToDouble(Console.ReadLine());

            Console.Write("Enter the width of the rectangle: ");
            double width = Convert.ToDouble(Console.ReadLine());

            // Calculate the area
            double area = length * width;

            // Display the area
            Console.WriteLine($"The area of the rectangle is: {area}");

            // Keep the console window open
            Console.ReadLine();
        }
    }
}
Output:
Enter the length of the rectangle: 8
Enter the width of the rectangle: 4
The area of the rectangle is: 32

Code Explanation:
In the above C-sharp program, Convert.ToDouble() is used to convert the user input from strings to double values for calculations. We also need to add Console.ReadLine() at the end keeps the console window open until the user presses Enter.

This program demonstrates basic user input, data conversion, arithmetic operation, and output formatting in C#. 

C# Program to Input Two Numbers and Display their Sum.

In this C# programming example, we will learn how to take input from the user for two numbers, calculate their sum, and display the result. This basic program will help you understand how to interact with the user and perform simple arithmetic operations in C#.

Sum of Two Numbers

Problem Statement: Write a C# program that takes two numbers as input from the user, calculates their sum, and displays the result.

Example:

Enter the first number: 5
Enter the second number: 7
The sum of 5 and 7 is: 12


C# Code to Find the Sum of Two Numbers.

//C-sharp code to print sum of two number
using System;

namespace SumCalculator
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("Enter the first number: ");
            //coverting input string to integer value
            int num1 = Convert.ToInt32(Console.ReadLine());

            Console.Write("Enter the second number: ");
            int num2 = Convert.ToInt32(Console.ReadLine());

            int sum = num1 + num2;

            Console.WriteLine($"The sum of {num1} and {num2} is: {sum}");
        }
    }
}
Output:
Enter the first number: 12
Enter the second number: 10
The sum of 12 and 10 is: 22

Code Explanation:

Inside the Main method of the above C# code:
  • Console.Write("Enter the first number: "); displays a message asking the user to enter the first number.
  • int num1 = Convert.ToInt32(Console.ReadLine()); reads the user's input and converts it to an integer.
  • Similar steps are followed to get the second number.
  • int sum = num1 + num2; calculates the sum of the two numbers.
  • Console.WriteLine($"The sum of {num1} and {num2} is: {sum}"); displays the result using string interpolation.

Key Points:
  • The Convert.ToInt32() method is used to convert the user's input (which is a string) into an integer.
  • The $ symbol is used for string interpolation, which allows us to embed expressions within strings for dynamic output.
  • The Console.ReadLine() method reads the entire line of text entered by the user, including spaces.

This program demonstrates how to interact with the user, perform arithmetic operations, and output results in C#.

C# program to print 'Hello, World!' to the console.

In this article, we will write our first C# program in which we will learn how to print the message "Hello World" on the Console screen. This is an introduction program we write whenever we start learning any new programming language as a beginner.

Example: 
Output: Hello, World!

Steps to Print a Message on Console in C#.

We need to follow the below steps to print our message to the console screen:

Step 1: Open a C# development environment such as Visual Studio or Visual Studio Code.

Step 2: Create a new C# project or open an existing one.

Step 3: Inside the project, create a new C# source code file with the ".cs" extension.

Step 4: In the source code file, use the Console.WriteLine() statement to print "Hello, World!" to the console.

Step 5: Save the file.

Step 6: Build and run the program to see the output.

C# Code Implementation to Print "Hello World!" on Console.

// C# code to print message on console
using System;

namespace HelloWorldApp
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello, World!");
        }
    }
}
Output:
Hello, World!


Explanation of Working:

1. The using System; directive includes the System namespace, which contains fundamental classes and base types.

2. The namespace HelloWorldApp encapsulates the program's code in a specific namespace.

3. The class Program defines a class called Program, which is the entry point of the application.

4. The static void Main(string[] args) method is the starting point of execution. It's the method that gets executed when the program is run.

5. Console.WriteLine("Hello, World!"); is a statement that prints "Hello, World!" to the console and adds a newline character at the end.

Key Note:
The Console.WriteLine() method is used to display output to the console and automatically moves to the next line after printing the message. (alert-passed)
If you want to print the message without moving to the next line, you can use Console.Write() instead. (alert-passed)

DON'T MISS

Nature, Health, Fitness
© all rights reserved
made with by AlgoLesson