0% found this document useful (0 votes)
10 views6 pages

Here Is A Simplc#####

Uploaded by

Nura Mohamed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views6 pages

Here Is A Simplc#####

Uploaded by

Nura Mohamed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 6

Here is a simple C# program that implements a calculator using methods for addition, subtraction,

multiplication, and division. The program takes input from the user for two numbers and the desired
operation:

using System;

class Calculator

static void Main()

Console.WriteLine("Welcome to Simple Calculator!");

// Input first number

Console.Write("Enter the first number: ");

double num1 = Convert.ToDouble(Console.ReadLine());

// Input second number

Console.Write("Enter the second number: ");

double num2=Convert.ToDouble(Console.ReadLine());

// Choose Operation

Console.WriteLine("\nChoose an operation:");

Console.WriteLine("+ : Addition");

Console.WriteLine("- : Subtraction");

Console.WriteLine("* : Multiplication");

Console.WriteLine("/ Division");

// Read operator choice

char opChoice = char.Parse(Console.ReadLine());

switch (opChoice)

case '+':
Addition(num1,num2);

break;

case '-':

Subtraction(num1,num2);

break;

case '*':

Multiplication( num1 ,num2);

break;

case '/':

Division( num1 ,num2);

break;

default:

Console.WriteLine($"Invalid Operator '{opChoice}' chosen.");

break;

static void Addition(double x,double y){

double result=x+y ;

Console .WriteLine ($"The sum of {x} +{y}= {result}");

static void Subtraction(double x,double y){

double result=x-y ;

System.Console .WriteLine($ "the difference between{x}-{y}={result}");

static void Multiplication(double x,double y){

double product=0 ;
product=(x*y) ;

System.Console.Writeline ("product of "+x+"*"+y+ "=" +product);

static void Division (double numerator , double denominator ){

if(denominator==0 ){

console.writeline ("cannot divide by zero") ;

}else{

Double quotient=numerator/denominator ;

system.console.writelne(numerator+"/"+denomiantor +"="+quotient );

This code defines a Calculator class with methods for each arithmetic operation - Addition,
Substraction,multiplicatioin ,and ‘division’. It then prompts users to enter two numbers along with their
preferred operations (+,-,* or /). Based on this input,the corresponding method will be called to perform
calculations.

Here is a simple C# program that concatenates two strings and returns the result using a method:

using System;

class StringConcatenation

static void Main()

Console.WriteLine("String Concatenation Program");

// Input first string

Console.Write("Enter the first string: ");

string str1 = Console.ReadLine();

// Input second number


Console.Write("Enter the second : ");

string str2=Console .ReadLine();

// Call ConcatenateStrings method to concatenate two strings

string concatenatedString = ConcatenateStrings(str1, str2);

Console.WriteLine($"The concatenated string is: {concatenatedString}");

static string ConcatenateStrings(string first Stringsecond)

return first+second;

In this code snippet, we have defined a StringConcatenation class with a Main method where users can
input two strings. The program then calls the ConcateanteStirngsmethod which takes in these two
strings as parameters and returns their concatenation. Finally,the main function displaysthe resulting
concatenated sting.

Here is a C# program that converts temperatures between Celsius and Fahrenheit using methods for
conversion:

using System;

class TemperatureConverter

static void Main()

Console.WriteLine("Temperature Converter Program");

// Input temperature value and unit from the user

Console.Write("Enter the temperature value: ");

double tempValue = Convert.ToDouble(Console.ReadLine());

// Choose input scale


Console .WriteLine ("Choose the input scale:");

console.writeline( "C : Celsius");

console.Writeline ( "F Fahrenheit") ;

char inputScale=char.Parse(console.readline());

// Call ConvertTemperature method to convert temperature

double convertedTemp;

if(inputScale=='C'){

convertedTemp=CelsiusToFahrenheit(tempValue);

Console.WriteLine($"The equivalent temperature in Fahrenheit is: {convertedTemp} °F");

}else if(inputscale =='f'){

Convertedtemp=Fahrenheittocelcius(Tempvalue);

console.writelne ($"the equvalent temprature incelsuis{converedtemperatue }° c ");

else {

consol.writeine('invalid scle chosen');

static double CelsiusToFahrenheit(double celsius)

return (celsius*9/5)+32;

static Double FahrenheiToCelius(Double fahreheit ) {

return(farenhiet-32)*5/9 ;

In this code snippet, we have defined a TemparatureConvertor class with main function where users can
enter temperatur values along with their respective scales. The program then calls either
‘celisustofarhenit’ or farento celisu based on thier choice of unitscale.The result will be displayed
accordingly.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy