Basic C# programs
1] Prime Number
namespace PrimeNumber
{
Static void Main(string[] args)
{
int num = 5;
int count = 0;
for (int i = 1; i <= number; i++)
{
if (number % i == 0)
{
count++;
}
}
if (count == 2)
{
Console.writeline("prime number")
}
else
{
Console.writeLine("Not a prime number");
}
Console.Readline();
}
}
2) 1 to 100 Prime Number
namespace Primenumber{static void Main(string[] args){For(number = 1, number <= 100; number++){int count = 0;For(int i = 1; i <= number; number++){if (number % i == 0){Count++;}}if (count == 2){Console.writeline(number)}}Console.readline();}}
3) Fibonacci Series
Namspace Fibonacii Seriesclass programme{static void main(string[] args){int a = 0;int b = 0;int c;Console.writeline(a);Console.writeline(b);For(int i = 0; i <= 10; i++){C = a + b;Console.writeline(c);a = b;b = c;}Console.Readline();}}
4) Factorial Number
Namespace Factorial
{
class program
{
static void Main(string[] args)
{
int num = 4;
int fact = 1;
for (int = 1; i <= num; i++)
{
fact = fact * 1;
}
Console.writeLine(fact);
Console.readline()
}
}
5) ( * ) Pattern
public class HelloWorld
{
public static void Main(string[] args)
{
for (int i = 1; i <= 5; i++)
{
for (int j = 1; j <= i; j++)
{
Console.write("*");
}
Console.writeLine();
}
for (int i = 1; i <= 4; i++)
{
for (int j = i; j <= 4; j++)
{
Console.write("*");
}
Console.writeLine();
}
Console.Readline();
}
}
6) Amstrong Number
class program{static void main(string[] args){int number = 153;int rem;int cube;int result = 0;int temp = number;while (number != 0){rem = number % 10;cube = rem * rem * rem;result = result + cube;number = number / 10;}number = temp;if (number == result){Console.writeline("armstring number");}else{Console.writeline("Not an Aremstring number");}Console.Readline();}}
7) Calculator using Switch Statement
class program{static void main(string[] args){int res;Console.write("Enter First number:");int num1 = Convert.Toint32(Console.readline());Console.write("Enter Second number:");int num2 = Convert.Toint32(Console.readline());Console.write("Enter Symbol(/,*,+,-):");string symbol = Console.Readline();switch (symbol){case "+":res = num1 + num2;Console.writeline("Addition:" + res);break;case "-":res = num1 - num2;Console.writeline("subtraction:" + res);break;case "*":res = num1 * num2;Console.writeline("multiplication:" + res);break;case "/":res = num1 / num2;Console.writeline("division:" + res);break;Default:Console.writeline("wrong input");break;}Console.writeline}}
8) Reverse number
namespace Reverse Number
class program
{
static void Main(string[] args)
{
int num = 123456789;
int Result = 0;
int rem;
while (num != 0)
{
rem = num % 10;
result = result * 10 + rem;
num = num / 10;
}
Console.Writeline(result);
Console.Readline();
}
}
}
9) Diamond Pattern
Class Program
{
static void Main(string[] args)
{
for (int i = 1; i <= 5; i++)
{
for (int j = 2; j <= i; j++)
{
Console.write(" ");
}
for (int k = 2 * i - 1; k <= 9; k++)
{
Console.writeline("*")
}
Console.writeline();
}
for (int i = 1; i <= 5; i++)
{
for (int j = 2; j <= i; j++)
{
Console.write(" ");
}
for (int k = 2 * i - 1; k <= 9; k++)
{
Console.writeline("*")
}
Console.writeline();
}
Console.Readline();
}
}
10) Celsius to Fahrenheit Conversion
namespace Demo
{
class MyApplication
{
static void Main(string[] args)
{
double Fahrenheit;
double celsius = 36;
Console.WriteLine("Celsius: " + celsius);
fahrenheit = (celsius * 9) / 5 + 32;
Console.WriteLine("Fahrenheit: " + fahrenheit);
Console.ReadLine();
}
}
}
* If you found this article useful, share it.*
MS SQL Server interview questions for developers ~ SoftCodeLearner
Nature's Routes (naturesroutes.blogspot.com)
Join telegram group-https://t.me/softwareCodeLearner
No comments:
Post a Comment