Vous êtes sur la page 1sur 11

C# program to print even numbers

using System;
namespace SamplePrograms
{
class EvenNumbers
{
public static void Main()
{
// Prompt the user to enter a target number. Target number is the
// number untill which the user want to have even and odd numbers printed
Console.WriteLine("Please enter your target");

// Declare a variable to hold the target number


int targetNumber = 0;

// Retrieve, Convert and store the target number


targetNumber = Convert.ToInt32(Console.ReadLine());

// Use a FOR or WHILE loop to print the even numbers, until our target number
for (int i = 0; i <= targetNumber; i = i + 2)
{
Console.WriteLine(i);
}

// You can also check if a number is even, by dividing it by 2.


//for (int i = 0; i <= targetNumber; i++)
//{
// if ((i % 2) == 0)
// {
//
Console.WriteLine(i);
// }
//}

// You can also use a while loop to do the same as shown below.
//int start = 0;
//while (start <= targetNumber)
//{
// Console.WriteLine(start);
// start = start + 2;
//}
// This line is to make the program wait for user input, instead of immediately closing
Console.ReadLine();
}
}
}

C# program to print prime numbers

A prime number is not divisible by any other number apart from 1 and itself. So, we use this
logic in this program to determine if a number is prime.

using System;
namespace SamplePrograms
{
class PrimeNumber
{
public static void Main()
{
// Declare a boolean variable to determine is if a number is prime
bool isNumberComposite = false;
int j;

// Prompt the user to enter their target number


Console.WriteLine("Enter your Target?");

// Read the target number and convert to integer


int target = Int32.Parse(Console.ReadLine());

// 1 is neither prime nor composite. So start at 2


for (int i = 2; i <= target; i++)
{
for (j = 2; j < i; j++)
{
// A number is not prime if it is divisible by any other number,
// other than 1 and itself.
if (i % j == 0)
{
isNumberComposite = true;
// We can break out of the inner for loop as we know the number
// is not prime
break;
}
}
// Print the number if it is not composite
if (!isNumberComposite)
Console.Write("{0} ", j);
else
isNumberComposite = false;
}
// This line is to make the program wait for user input,
// instead of immediately closing
Console.ReadLine();
}
}
}

C# program to print fibonacci series

A prime number is not divisible by any other number apart from 1 and itself. So, we use this
logic in this program to determine if a number is prime.
using System;
namespace SamplePrograms
{
class FibonacciSeries
{
public static void Main()
{
// Prompt the user to enter their target number
Console.WriteLine("How many numbers do you want in the fibonacci series");
// Read the user input from console and convert to integer
int Target = int.Parse(Console.ReadLine());
// Create integer variables to hold previous and next numbers
int PreviousNumber = -1, NextNumber = 1;
// This for loop controls the number of fibonacci series elements
for (int i = 0; i < Target; i++)
{
// Logic to compute fibonacci series numbers
int Sum = PreviousNumber + NextNumber;
PreviousNumber = NextNumber;
NextNumber = Sum;
Console.Write(NextNumber + " ");
}
Console.ReadLine();
}
}
}
A prime number is not divisible by any other number apart from 1 and itself. So, we use this
logic in this program to determine if a number is prime.
program to print multiplication table
This program can be used to print any multiplication table until any number
using System;
namespace SamplePrograms
{
class NumberTable
{
public static void Main()
{
// Prompt the user to enter number for multiplication table
Console.WriteLine("For which number do you want to print multiplication table");
// Read the number from console and convert to integer
int Number = Convert.ToInt32(Console.ReadLine());

// Prompt the user for multiplication table target


Console.WriteLine("What is your target? 10, 20, 30 etc...");

// Read the target from console and convert to integer


int Target = Convert.ToInt32(Console.ReadLine());
// Loop to print multiplication table until we reach the target
for (int i = 1; i <= Target; i++)
{
// Compute multiplication result
int Result = Number * i;
// Format and Print the multiplication table
Console.WriteLine(Number.ToString() + " X " + i.ToString() +
" = " + Result.ToString());
// The above line can also be rewritten as shown below.
// Console.WriteLine("{0} X {1} = {2}", Number, i, Result);
}
}
}
}
C# program to print alphabets
This c# program prints both upper and lower case alphabets using 2 different approaches.
using System;

namespace SamplePrograms
{
class Alphabets
{
public static void Main()
{
// Loop from a thru z (lower case alphabets)
for (char alphabet = 'a'; alphabet <= 'z'; alphabet++)
{
Console.Write(alphabet + " ");
}

//Another way to print lower case alphabets


//for (int i = 0; i < 26; i++)
//{
// Console.Write(Convert.ToChar(i + (int)'a') + " ");
//}

Console.WriteLine();

// Loop from A thru Z (upper case alphabets)


for (char alphabet = 'A'; alphabet <= 'Z'; alphabet++)
{
Console.Write(alphabet + " ");
}

//Another way to print uppercase case alphabets


//for (int i = 0; i < 26; i++)
//{
// Console.Write(Convert.ToChar(i + (int)'A') + " ");
//}
Console.ReadLine();
}
}
}
Reverse characters in a string
Write a C# program to print the characters in a string in the reverse order.
using System;
using System.Collections.Generic;
using System.Linq;
namespace SamplePrograms
{
class ReverseCharacters
{
public static void Main()
{
// Prompt the user to enter the string
Console.WriteLine("Please enter your string");

// Read the user string from console


string UserString = Console.ReadLine();

// The simple way to reverse a string is to use


// the built-in .net framework Reverse() function
List<char> StringCharacters = UserString.Reverse().ToList();
// Finally print each character from the collection
foreach (char c in StringCharacters)
{
Console.Write(c);
}
Console.WriteLine();
Console.ReadLine();
}
}
}

Factorial of a number
using System;
namespace SamplePrograms
{
class Factorial
{
public static void Main()

{
// Prompt the user to enter their target number to calculate factorial
Console.WriteLine("Please enter the number for which you want to compute
factorial");

try
{
// Read the input from console and convert to integer data type
int iTargetNumber = Convert.ToInt32(Console.ReadLine());

// Factorial of Zero is 1
if (iTargetNumber == 0)
{
Console.WriteLine("Factorial of Zero = 1");
}
// Compute factorial only for non negative numbers
else if (iTargetNumber < 0)
{
Console.WriteLine("Please enter a positive number greater than 1");
}
// If the number is non zero and non negative
else
{
// Declare a variable to hold the factorial result.
double dFactorialResult = 1;

// Use for loop to calcualte factorial of the target number


for (int i = iTargetNumber; i >= 1; i--)
{
dFactorialResult = dFactorialResult * i;
}

// Output the result to the console


Console.WriteLine("Factorial of {0} = {1}", iTargetNumber, dFactorialResult);
}
}
catch (FormatException)
{
// We get format exception if user enters a word instead of number
Console.WriteLine("Please enter a valid number", Int32.MaxValue);
}
catch (OverflowException)
{
// We get overflow exception if user enters a very big number,
// which a variable of type Int32 cannot hold
Console.WriteLine("Please enter a number between 1 and {0}",Int32.MaxValue);
}
catch (Exception)
{
// Any other unforeseen error
Console.WriteLine("There is a problem! Please try later");

}
}
}
}
Write a program to find out the Even or Odd number.
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter the number");
int number = int.Parse(Console.ReadLine());
if(number%2==0)
{
Console.WriteLine("The Enter number is Even number");
}
else
{
Console.WriteLine("The Enter number is Odd number");
}
Console.ReadLine();
}
}
Output:
Enter the number:5
Your Enter number is Odd number
Enter the number:6
Your Enter number is Even number
Write a program swapping two numbers without use third variable
class Program
{
static void Main(string[] args)
{
int a, b;
Console.WriteLine("Enter the first number");
a = int.Parse(Console.ReadLine());
Console.WriteLine("Enter the second number");
b = int.Parse(Console.ReadLine());
a = a + b;
b = a - b;
a = a - b;
Console.WriteLine("After swaping");
Console.WriteLine("The first number is:{0}",a);
Console.WriteLine("The second number is:{0}",b);
Console.ReadLine();
}
}

Write a program a give the input and check the palindrome or not?
class Program
{ static void Main(string[] args)
{
int revers=0;
Console.WriteLine("Enter the number");
int n = Convert.ToInt32(Console.ReadLine());
int temp = n;
while (n > 0)
{
int reminder = n % 10;
revers = revers * 10 + reminder;
n = n / 10;
} Console.WriteLine(revers);
if(temp==revers)
{
Console.WriteLine("this is palindrome");
}
else
{
Console.WriteLine("this is not palindrome");
} Console.WriteLine();
Console.ReadLine();
}
}
Write a program check the Armstrong number is or not?
class Program
{
static void Main(string[] args)
{
int sum = 0, i, r;
Console.WriteLine("Enter the number");
int n = int.Parse(Console.ReadLine());
for(i=n;i>0;i=i/10)

{
r = i % 10;
sum = sum + r * r * r;
}
if(n==sum)
{
Console.WriteLine("This is Armstrong number");
}
else
{
Console.WriteLine("This is not Armstrong number");
}
Console.WriteLine();
Console.ReadLine();
}
}
Reverse the Character in the Given sentence.
class Program
{
static void Main(string[] args)
{
string str="", reverse = "";
str = "honesty is the best policy";
int num = str.Length - 1;
while(num >=0)
{
reverse=reverse+str[num];
num--;
}
Console.WriteLine(reverse);
Console.ReadLine();
}
}
*
**
***
****
*****
for (int row = 1; row<=5; row++)
{
for (int coll = 1; coll <=row; coll++)
{
Console.Write("*");
}
Console.WriteLine();
}
Console.ReadLine();

* * * * *
* * * *
* * *
* *
*
for (int row = 1; row<=5; row++)
{
for (int space = 1; space <= row; space++)
{
Console.Write(" ");
}
for (int coll = 5; coll >=row; coll--)
{
Console.Write("*");
} Console.WriteLine();
}
Console.ReadLine();
perfect number program
using
using
using
using

System;
System.Collections.Generic;
System.Linq;
System.Text;

namespace program
{
class Program
{
static void Main(string[] args)
{
int number,sum=0,n;
Console.Write("Enter a Number : ");
number = int.Parse(Console.ReadLine());
n = number;
for (int i = 1; i < number;i++)
{
if (number % i == 0)
{
sum=sum + i;
}
}
if (sum == n)
{
Console.WriteLine("\n Entered number is a Perfect Number");
Console.ReadLine();
}
else
{
Console.WriteLine("\n Entered number is not a Perfect Number");
Console.ReadLine();
}
}
}
}

Vous aimerez peut-être aussi