You are on page 1of 13

Problem Description

This C# Program Generates Odd Numbers within a Range.

Problem Solution
Here Enumerable.Range generates a collection of odd numbers of a
specified range. It can simplify numeric lists and drop-downs in Windows
Forms program.

Program/Source Code
Here is source code of the C# Program to Generate Odd Numbers within
a Range. The C# program is successfully compiled and executed with
Microsoft Visual Studio. The program output is also shown below.

/*
* C# Program to Caluculate the power exponent value
*/
using System;
using System.Collections.Generic;
using System.Linq;
class program
{
static void Main(string[] args)
{
IEnumerable<int> oddNums =

Enumerable.Range(20, 20).Where(x => x % 2 != 0);

foreach (int n in oddNums)


{
Console.WriteLine(n);
}
Console.ReadLine();

}
}

Program Explanation
This C# program generates odd numbers within a range. The
Enumerable. Range generates a collection of odd numbers of a
specified range. It can simplify numeric lists and drop-downs in Windows
Forms program. Using for each loop print the odd numbers in the range.
This is a C# Program to check whether the entered number is even or
odd.

Problem Description
This C# Program checks if a given integer is Odd or Even.

Problem Solution
Here if a given number is divisible by 2 with the remainder 0 then the
number is an Even number. If the number is not divisible by 2 then that
number will be an Odd number.

Program/Source Code
Here is source code of the C# program which checks a given integer is
odd or even. The C# program is successfully compiled and executed
with Microsoft Visual Studio. The program output is also shown below.

/*
* C# Program to Check whether the Entered Number is Even or Odd
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace check1
{
class Program
{
static void Main(string[] args)
{
int i;
Console.Write("Enter a Number : ");
i = int.Parse(Console.ReadLine());
if (i % 2 == 0)
{
Console.Write("Entered Number is an Even Number");
Console.Read();
}
else
{
Console.Write("Entered Number is an Odd Number");
Console.Read();
}
}
}
}

Program Explanation
In this C# program, we are reading the number using ‘i’ integer variable.
If condition statement is used to check the number is even and odd. For
even number the modulus of the value of ‘i’ variable by 2 is equal to
zero, if the condition is true then print the statement as even number.
This is a C# Program to accept a number from the user and display it if it
is Positive.

Problem Description
This C# Program Accepts a Number from the user and Display it if it is
Positive.

Problem Solution
Here if a given number is less than zero then it is displayed as positive
else negative.

Program/Source Code
Here is source code of the C# Program to Accept a Number from the
user and Display it if it is Positive. The C# program is successfully
compiled and executed with Microsoft Visual Studio. The program output
is also shown below.

/*
* C# Program to Accept a Number from the user and Display it
* if it is Positive
*/
using System;
class program
{
public static void Main(string[] args)
{
Console.WriteLine("Enter a number: ");
int number = Convert.ToInt32(Console.ReadLine());
if (number > 0)
{
Console.WriteLine("Number is positive");
}
else if (number == 0)
{
Console.WriteLine("Number is 0");
}
else
{
Console.WriteLine("Number is negative");
}
Console.ReadLine();
}
}

Program Explanation
In this C# program, we are reading a number using the ‘number’
variable. Nested if else condition statement is used to check that given
number is less than zero. If the condition is true then execute the
statement and print the statement as positive. Otherwise, if the condition
is false, then execute the else statement and print the statement as the
number is 0.
This is a C# Program to find greatest among 2 numbers.

Problem Description
This C# Program Finds Greatest among 2 numbers.

Problem Solution
Here the user enters two numbers and the greatest among the two
numbers is found by comparing the two numbers and the result is
displayed.

Program/Source Code
Here is source code of the C# Program to Find Greatest among 2
numbers. The C# program is successfully compiled and executed with
Microsoft Visual Studio. The program output is also shown below.

/*
* C# Program to Find Greatest among 2 numbers
*/
using System;
class prog
{
public static void Main()
{
int a, b;
Console.WriteLine("Enter the Two Numbers : ");
a = Convert.ToInt32(Console.ReadLine());
b = Convert.ToInt32(Console.ReadLine());
if (a > b)
{
Console.WriteLine("{0} is the Greatest Number", a);
}
else
{
Console.WriteLine("{0} is the Greatest Number ", b);
}
Console.ReadLine();
}
}

Program Explanation
In this C# program, we are finding the greatest value among 2 numbers.
If else condition statement is used to compare the two numbers and print
the greatest value among 2 numbers.
This is a C# Program to swap 2 numbers.

Problem Description
This C# Program Swaps 2 Numbers.

Problem Solution
It obtains two numbers from the user and swaps the numbers using a
temporary variable.

Program/Source Code
Here is source code of the C# program that swaps two numbers. The C#
program is successfully compiled and executed with Microsoft Visual
Studio. The program output is also shown below.

/*
* C# Program to Swap two Numbers
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Program
{
class Program
{
static void Main(string[] args)
{
int num1, num2, temp;
Console.Write("\nEnter the First Number : ");
num1 = int.Parse(Console.ReadLine());
Console.Write("\nEnter the Second Number : ");
num2 = int.Parse(Console.ReadLine());
temp = num1;
num1 = num2;
num2 = temp;
Console.Write("\nAfter Swapping : ");
Console.Write("\nFirst Number : "+num1);
Console.Write("\nSecond Number : "+num2);
Console.Read();
}
}
}

Program Explanation
In this C# program, we are reading the numbers using ‘num1’ and
‘num2’ variables respectively. Interchange the values of the ‘num1’ and
‘num2’ variables using temporary variable ‘t’. Print the swapped value of
2 numbers.
This is a C# Program to find whether the number is divisible by 2.

Problem Description
This C# Program Finds whether the Number is Divisible by 2.

Problem Solution
Any whole number that ends in 0, 2, 4, 6, or 8 will be divisible by 2.Here
the divisibility test is done by performing the mod function with 2.

Program/Source Code
Here is source code of the C# Program to Find whether the Number is
Divisible by 2. The C# program is successfully compiled and executed
with Microsoft Visual Studio. The program output is also shown below.

/*
* C# Program to Find whether the Number is Divisible by 2
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication16
{
class Program
{
static void Main(string[] args)
{
int n;
Console.WriteLine("Enter the Number :");
n = int.Parse(Console.ReadLine());
if (n % 2 == 0)
{
Console.WriteLine("Entered Number is Divisible by 2 ");
}
else
{
Console.WriteLine("Entered Number is Not Divisible by 2");
}
Console.ReadLine();
}
}
}

Program Explanation
In this C# program, we are reading the number using ‘n’ variable. If
condition is used to check that the modulus of the value of ‘n’ variable by
2 is equal to 0. If the condition is true then execute the statement. Print
the statement as the number is divisible by 2. Otherwise, if the condition
is false, then execute the else statement and print the statement as not
divisible by 2.
This is a C# Program to print the sum of all the multiples of 3 and 5.

Problem Description
This C# Program Prints the Sum of all the Multiples of 3 and 5.

Problem Solution
Here the multiples of 3 and 5 are found and the sum of all the multiples
are calculated and are displayed.

Program/Source Code
Here is source code of the C# Program to Print the Sum of all the
Multiples of 3 and 5. The C# program is successfully compiled and
executed with Microsoft Visual Studio. The program output is also shown
below.

/*
*C# Program to Print the Sum of all the Multiples of 3 and 5
*/
using System;
class program
{
public static void Main()
{
int a, b, i, Sum = 0;
for (i = 1; i < 100; i++)
{
a = i % 3;
b = i % 5;
if (a == 0 || b == 0)
{
Console.Write("{0}\t", i);
Sum = Sum + i;
}
}
Console.WriteLine("\nThe Sum of all the Multiples of 3 or 5 Below
100 : {0}",
Sum);
Console.Read();
}
}

Program Explanation
In this C# program, using for loop compute the multiples of 3 and 5 from
1 to 100. Initialize the value of ‘i’ variable as 1 and check the condition
that the value of ‘i’ variable is less than 100. If the condition is true then
execute the statement. Compute the modulus of the value of ‘i’ variable
by 3 and the modulus of the value of ‘i’ variable by 5.
This is a C# Program to print all the multiples of 17 which are less than
100.

Problem Description
This C# Program Prints all the Multiples of 17 which are Less than 100.

Problem Solution
Here all the multiples of 17 are displayed.

Program/Source Code
Here is source code of the C# Program to Print all the Multiples of 17
which are Less than 100. The C# program is successfully compiled and
executed with Microsoft Visual
Studio. The program output is also shown below.

/*
* C# Program to Print all the Multiples of 17 which are Less than 100
*/
using System;
class program
{
public static void Main()
{
int a,i;
Console.WriteLine("Multiples of 17 are : ");
for (i = 1; i < 100; i++)
{
a = i % 17;
if (a == 0)
{
Console.WriteLine(i);
}
}
Console.Read();
}
}

Program Explanation
In this C# program using for loop, compute the multiples of 17 from 1 to
100. Initialize the value of ‘i’ variable as 1 and check the condition that
the value of ‘i’ variable is less than 100.
This is a C# Program to get a number and display the sum of the digits.

Problem Description
This C# Program Gets a Number and Display the Sum of the Digits.

Problem Solution
The digit sum of a given integer is the sum of all its digits.

Program/Source Code
Here is source code of the C# Program to Get a Number and Display the
Sum of the Digits. The C# program is successfully compiled and
executed with Microsoft Visual Studio. The program output is also shown
below.

/*
* C# Program to Get a Number and Display the Sum of the Digits
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Program
{
class Program
{
static void Main(string[] args)
{
int num, sum = 0, r;
Console.WriteLine("Enter a Number : ");
num = int.Parse(Console.ReadLine());
while (num != 0)
{
r = num % 10;
num = num / 10;
sum = sum + r;
}
Console.WriteLine("Sum of Digits of the Number : "+sum);
Console.ReadLine();

}
}
}

Program Explanation
In this C# program, we are reading a number using ‘num’ variable. Using
while loop computes the sum of the digits. The digit sum of a given
integer is the sum of all its digits.
Compute the modulus of the value of ‘num’ variable by 10. Divide the
value of ‘num’ variable by 10. Compute the summation of the value of
‘sum’ variable with the value of ‘r’ variable. Print the sum of the digits.
This is a C# Program to get a number and display the number with its
reverse.

Problem Description
This C# Program Gets a Number and Display the Number with its Reverse.

Problem Solution
Here we obtain a number from the user and display the digits in the reverse order.

Program/Source Code
Here is source code of the C# Program to Get a Number and Display the Number
with its Reverse. The C# program is successfully compiled and executed with
Microsoft Visual Studio. The program output is also shown below.

/*
* C# Program to Get a Number and Display the Number with its Reverse
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Program
{
class Program
{
static void Main(string[] args)
{
int num, reverse = 0;
Console.WriteLine("Enter a Number : ");
num = int.Parse(Console.ReadLine());
while (num != 0)
{
reverse = reverse * 10;
reverse = reverse + num % 10;
num = num / 10;
}
Console.WriteLine("Reverse of Entered Number is : "+reverse);
Console.ReadLine();

}
}
}

Program Explanation
In this C# program, we are reading the number using ‘num’ variable.
Multiply the value of ‘reverse’ variable by 10 and add this value of
reverse variable with the modulus of the value of ‘reverse’ variable by
10. Compute the division of the value of ‘num’ variable by 10. Print the
number with its reverse order.
This is a C# Program to reverse a number & check if it is a palindrome.

Problem Description
This C# Program Reverses a Number & Check if it is a Palindrome.

Problem Solution
Here first it reverses a number. Then it checks if given number and
reversed numbers are equal. If they are equal, then its a palindrome.

Program/Source Code
Here is source code of the C# Program to Reverse a Number & Check if
it is a Palindrome. The C# program is successfully compiled and
executed with Microsoft Visual Studio. The program output is also shown
below.

/*
* C# Program to Reverse a Number & Check if it is a Palindrome
*/
using System;
class program
{
public static void Main()
{
int num, temp, remainder, reverse = 0;
Console.WriteLine("Enter an integer \n");
num = int.Parse(Console.ReadLine());
temp = num;
while (num > 0)
{
remainder = num % 10;
reverse = reverse * 10 + remainder;
num /= 10;
}
Console.WriteLine("Given number is = {0}", temp);
Console.WriteLine("Its reverse is = {0}", reverse);
if (temp == reverse)
Console.WriteLine("Number is a palindrome \n");
else
Console.WriteLine("Number is not a palindrome \n");
Console.ReadLine();
}
}

Program Explanation
This C# program we are reading an integer using ‘num’ variable.
Compute the modulus of the value of ‘num’ variable by 10 and add the
value with the value of ‘reverse’ variable. Divide the value of ‘num’
variable by 10 and assign to ‘num’ variable.
This is a C# Program to find the sum of two binary numbers.

Problem Description
This C# Program Finds the Sum of two Binary Numbers.

Problem Solution
Here Binary number is a number that can be represented using only two numeric
symbols – 0 and 1. A number in base 2.

Program/Source Code
Here is source code of the C# Program to Find the Sum of two Binary Numbers. The
C# program is successfully compiled and executed with Microsoft Visual Studio. The
program output is also shown below.

/*
* C# Program to Find the Sum of two Binary Numbers */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication
{
class Program
{
static void Main(string[] args)
{
int b1, b2;
int i = 0, rem = 0;
int[] sum = new int[20];
Console.WriteLine("Enter the first binary number: ");
b1 = int.Parse(Console.ReadLine());
Console.WriteLine("Enter the second binary number: ");
b2 = int.Parse(Console.ReadLine());
while (b1 != 0 || b2 != 0)
{
sum[i++] = (b1 % 10 + b2 % 10 + rem) % 2;
rem = (b1 % 10 + b2 % 10 + rem) / 2;
b1 = b1 / 10;
b2 = b2 / 10;
}
if (rem != 0)
sum[i++] = rem;
--i;
Console.WriteLine("Sum of two binary numbers: ");
while (i >= 0)
Console.Write("{0}", sum[i--]);
Console.ReadLine();
}
}
}

Program Explanation
This C# program we are reading the first and second binary numbers using ‘b1’ and
‘b2’ variables respectively. Here Binary number is a number that can be represented
using only two numeric symbols 0 and 1. Using while loop check the condition that
both the values of ‘b1’ or ‘b2’variables are not equal to 0.

You might also like