0% found this document useful (0 votes)
14 views21 pages

Interview Programs

The document contains various C# programming examples demonstrating fundamental concepts such as checking for odd/even numbers, swapping values, calculating factorials, identifying Armstrong and prime numbers, generating Fibonacci series, reversing numbers and strings, checking for palindromes, and printing patterns. Each example includes code snippets and sample outputs to illustrate the functionality. The document serves as a comprehensive guide for beginners to understand basic programming techniques in C#.

Uploaded by

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

Interview Programs

The document contains various C# programming examples demonstrating fundamental concepts such as checking for odd/even numbers, swapping values, calculating factorials, identifying Armstrong and prime numbers, generating Fibonacci series, reversing numbers and strings, checking for palindromes, and printing patterns. Each example includes code snippets and sample outputs to illustrate the functionality. The document serves as a comprehensive guide for beginners to understand basic programming techniques in C#.

Uploaded by

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

Odd Even Number Program in C#

Even number is divisible by 2 but Odd number is not divisible by 2. Here we apply this concept

to write even odd program in C#. To display any message on screen we use WriteLine() the

static method of Console class.


Check Number is Odd or Even Program in C#

using System;

public class Odd_Even

public static void Main()

int num1, rem1;

Console.Write("Enter any Number: ");

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

rem1 = num1 % 2;

if (rem1 == 0)

Console.WriteLine("{0} is an even number.\n",num1);

else

Console.WriteLine("{0} is an odd number.\n",num1);

Output

Enter any Number: 10

10 is an even number
Swap Two Numbers Program in C#

There are two method to swap any two numbers, using third variable and without using third

variable. here we write this code using third variable and without using third variable.

Swap Two Numbers Using third variable Program in C#

using System;
public class Swap_Demo
{
public static void Main(string[] args)
{
int a=10, b=20, temp;
Console.WriteLine("Before swap a= "+a+" b= "+b);
temp=a; //temp=10
a=b; //a=20
b=temp; //b=10
Console.Write("After swap a= "+a+" b= "+b);
}
}

Output

Before Swap a= 10 b=20

After Swap a= 20 b=10

Swap Two Numbers without using third variable Program in C#

using System;

public class Swap_Demo


{
public static void Main(string[] args)
{
int a=10, b=20;
Console.WriteLine("Before swap a= "+a+" b= "+b);
a=a+b; //a=30 (10+20)
b=a-b; //b=10 (30-20)
a=a-b; //a=20 (30-10)
Console.Write("After swap a= "+a+" b= "+b);
}
}

Output
Before Swap a= 10 b=20

After Swap a= 20 b=10

Swap Two Numbers without using third variable Program in C#

using System;
public class Swap_Demo
{
public static void Main(string[] args)
{
int a, b;
Console.Write("\nInput the First Number: ");
a = int.Parse(Console.ReadLine());
Console.Write("\nInput the Second Number: ");
b = int.Parse(Console.ReadLine());
Console.WriteLine("Before swap a= "+a+" b= "+b);
a=a+b; //a=30 (10+20)
b=a-b; //b=10 (30-20)
a=a-b; //a=20 (30-10)
Console.Write("After swap a= "+a+" b= "+b);
}
}

Output

Input the First Number: 10

Input the Second Number: 20

Before Swap a= 10 b=20

After Swap a= 20 b=10

Find Factorial of any Number Program in C#

There are two method to find factorial of any number first is using increment operator and

other is using decrement operator.

Factorial Program in C#

using System;
namespace factorial
{
class Factorial
{
static void Main(string[] args)
{
int i, number, fact;
Console.WriteLine("Enter any Number: ");
number = int.Parse(Console.ReadLine());
fact = number;
for (i=number-1; i>=1; i--)
{
fact = fact * i;
}
Console.WriteLine("\nFactorial of Given Number is: "+fact);
Console.ReadLine();
}
}
}

Output

Enter any Number: 5

Factorial of Given Number is: 120

Armstrong Number Program in C#

To find Armstrong number program we apply same concept like C and C++ programming.

Some Armstrong numbers are 153 etc.

Check Given Number is Armstrong or not in C#

using System;

class Armstrong
{
public static void Main()
{
int number, remainder, sum = 0;
Console.Write("Enter any Number: ");
number = int.Parse(Console.ReadLine());
for (int i = number; i > 0; i = i / 10)
{
remainder = i % 10;
sum = sum + remainder*remainder*remainder;
}
if (sum == number)
{
Console.Write("This is an Armstrong Number");
}
else
{
Console.Write("This is not an Armstrong Number");
}
Console.ReadLine();
}
}

Output

Enter any Number: 153

This is an Armstrong Number

Output

Enter any Number: 133

This is not an Armstrong Number

Prime Number Program in C#

Prime number is only divisible by 1 and number it self. Here we write code for this in C#. To

display any message on screen we use WriteLine() the static method of Console class.
Prime Number Program in C#

using System;
namespace example
{
class Prime_Number
{
public static void Main()
{
Console.Write("Enter a Number : ");
int num;
num = Convert.ToInt32(Console.ReadLine());
int k;
k = 0;
for (int i = 1; i <= num; i++)
{
if (num % i == 0)
{
k++;
}
}
if (k == 2)
{
Console.WriteLine("Entered Number is a Prime Number and the Largest Factor is {0}",num);
}
else
{
Console.WriteLine("Not a Prime Number");
}
Console.ReadLine();
}
}
}

Output

Enter a Number : 23

Entered Number is a Prime Number and the Largest Factor is 23

Table of any Number Program in C#

To calculate or find table of any number we multiply given number bu 1 to 10. For write this

code we use given formula.


Formula

Table=num*1;

Table=num*2;

Table=num*3;

...................

...................

Table=num*10;

Calculate Table of any Number Program in C#

using System;

namespace Table_Pro
{
class Program
{
static void Main(string[] args)
{
int num;
Console.WriteLine("Enter any Number for Table: ");
num = Convert.ToInt32(Console.ReadLine());
for (int i = 1; i <= 10; i++)
{
Console.WriteLine("{0}*{1}={2}", num, i, i * num);
}
}
}
}

Output

Enter any Number for Table:5

5*1 =5

5*2 =10

5*3 =15

5*4 =20

5*5 =25
5*6 =30

5*7 =35

5*8 =40

5*9 =45

5*10 =50

Output

Enter any Number for Table: 10

10*1 =10

10*2 =20

10*3 =30

10*4 =40

10*5 =50

10*6 =60

10*7 =70

10*8 =80

10*9 =90

10*10 =100

Fibonacci Series Program in C#

Fibonacci Series add two previous term and get next term its first two terms are 0 and 1.

Fibonacci series are; 0 1 1 2 3 5 8 13.....

Fibonacci Series Program in C#

using System;

namespace fibonaci
{
class Program
{
static void Main(string[] args)
{
int i, count, term1 = 0, term2 = 1, term3 = 0;
Console.Write("Enter the Limit: ");
count = int.Parse(Console.ReadLine());
Console.WriteLine(term1);
Console.WriteLine(term2);
for (i=0; i<=count; i++)
{
term3 = term1 + term2;
Console.WriteLine(term3);
term1 = term2;
term2 = term3;
}
Console.ReadLine();
}
}
}

Output

Enter the Limit: 34

13

21

34

Output

Enter the Limit: 13


0

13

Reverse a Number Program in C#

To reverse any number we need to calculate remainder of number and the apply formula for

reverse any number.

Formula

Remainder = Number % 10;


Reverse = (Reverse * 10) + remainder;
Number = Number / 10;

Reverse a Number Program in C#

using System;

namespace Reverse_Num
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter any Number: ");
int num = int.Parse(Console.ReadLine());
int rev = 0;
while(num>0)
{
int rem = Number % 10;
rev = (rev * 10) + rem;
num = num / 10;
}
Console.WriteLine("Reverse Number is: {0}",rev);
Console.ReadLine();
}
}
}

Output

Enter any Number: 154

Reverse Number is: 451

Output

Enter any Number: 247

Reverse Number is: 742

Reverse a String Program in C#

To reverse any string we no need any new concept just apply same concept like C and C++

Program.

Formula

Remainder = Number % 10;


Reverse = (Reverse * 10) + remainder;
Number = Number / 10;

Reverse a String Program in C#

using System;

namespace reverseString
{
class Program
{
static void Main(string[] args)
{
string str = "", reverse = "";
int Length = 0;
Console.WriteLine("Enter any String: ");
//Getting String(word) from Console
str = Console.ReadLine();
//Calculate length of string str
Length = str.Length - 1;
while(Length>=0)
{
reverse = reverse + str[Length];
Length--;
}
//Displaying the reverse string
Console.WriteLine("Reverse String is: {0}",reverse);
Console.ReadLine();
}
}
}

Output

Enter any String: Raj

Reverse String is: jaR

Output

Enter any String: Hitesh

Reverse String is: hsetiH

Palindrome Number Program in C#

To check number is palindrome or not first reverse number if resultant number is same like

original number then it is palindrome number other wise nor palindrome.

Palindrome Number Program in C#

using System;

namespace Palindrome
{
class Program
{
static void Main(string[] args)
{
int num, rem, sum = 0, temp;
Console.Write("Enter any number: ");
num = Convert.ToInt32(Console.ReadLine());
temp = num;
while (num>0)
{
rem = num % 10; //find remainder by dividing with 10
num = num / 10; //find quotient by dividing with 10
sum = sum * 10 + rem;
}
if (temp == sum)
{
Console.WriteLine("Number is Palindrome");
}
else
{
Console.WriteLine("Number is not a palindrome");
}
Console.ReadLine();
}
}
}

Output

Enter any Number: 234

Number is not a palindrome

Output

Enter any Number: 231

Number is palindrome

Palindrome String Program in C#


To check string is palindrome or not first reverse string if resultant string is same like original

string then it is palindrome string other wise nor palindrome.

Palindrome String Program in C#

using System;

namespace palindrome
{
class Program
{
static void Main(string[] args)
{
string s,revs="";
Console.WriteLine(" Enter string");
s = Console.ReadLine();
for (int i=s.Length-1; i>=0; i--) //String Reverse
{
revs += s[i].ToString();
}
if (revs == s)
{
Console.WriteLine("String is Palindrome");
}
else
{
Console.WriteLine("String is not Palindrome");
}
Console.ReadKey();
}
}
}

Output

Enter any String: Hello

String is not a palindrome

Output

Enter any String: Mam

String is palindrome
Print Star Pattern Program in C#

In C# Programming Print star pattern code is very simple to write just follow same concept like

C only you need to change syntax.

Print Star Pattern in C#

using System;

public class StarPattern


{
public static void Main(string[] args)
{
for (int row = 1; row <= 5; ++row)
{
for (int col = 1; col <= row; ++col)
{
Console.Write("*");
}
Console.WriteLine();
}
}
}

Output

**
***

****

*****

Print Star Pattern Program in C#

using System;

public class StarPattern


{
public static void Main(string[] args)
{
for (int row=8; row>=1; --row)
{
for (int col=1; col<=row; ++col)
{
Console.Write("*");
}
Console.WriteLine();
}
}
}

Output

*****

****

***

**

Print Star Pattern Program in C#

using System;

public class StarPattern


{
public static void Main(string[] args)
{
int val = 5;
int i, j, k;
for (i=1; i<=val; i++)
{
for (j=1; j<=val-i; j++)
{
Console.Write(" ");
}
for (k=1; k<=i; k++)
{
Console.Write("*");
}
Console.WriteLine("");
}
Console.ReadLine();
}
}

Output

**

***

****

*****

Print Number Pattern Program in C#

In C# this code is very simple to write just follow same concept like C and C++ programming

only you need to change syntax.


Print Number Pattern Program in C#

using System;

public class NumberPattern


{
public static void Main()
{
int no = 5;
for (int i = 1; i <= 5; i++)
{
for (int j = 1; j <= i; j++)
{
Console.Write(i);
}
Console.WriteLine();
}
Console.ReadKey();
}
}

Output

22

333

4444

55555

Print Number Pattern Program in C#


using System;

public class NumberPattern


{
public static void Main(string[] args)
{
int i, j, rows, k = 1;
Console.Write("Enter number of rows: ");
rows = Convert.ToInt32(Console.ReadLine());
for (i = 1; i <= rows; i++)
{
for (j = 1; j <= i; j++)
Console.Write("{0} ", k++);
Console.Write("\n");
}
}
}

Output

Enter number of rows: 4

23

456

7 8 9 10

Print Number Pattern Program in C#

using System;

public class NumberPattern


{
public static void Main(string[] args)
{
int i,j,k,l,n;
Console.Write("Enter the Range: ");
n= int.Parse(Console.ReadLine());
for(i=1; i<=n; i++)
{
for(j=1; j<=n-i; j++)
{
Console.Write(" ");
}
for(k=1;k<=i;k++)
{
Console.Write(k);
}
for(l=i-1;l>=1;l--)
{
Console.Write(l);
}
Console.Write("\n");
}
}
}

Output

Enter the Range: 4

121

12321

1234321

Output

Enter the Range: 5

121

12321

1234321

123454321

Print Alphabet Pattern in C#


In C# this code is very simple to write just follow same concept like C and C++ programming

only you need to change syntax.

Print Alphabet Pattern in C#

using System;

public class AlphabetPattern


{
public static void Main(string[] args)
{
char ch='A';
int i, j, k, m;
for(i=1; i<=5; i++)
{
for(j=5; j>=i; j--)
Console.Write(" ");
for(k=1;k<=i;k++)
Console.Write(ch++);
ch--;
for(m=1;m<i;m++)
Console.Write(--ch);
Console.Write("\n");
ch='A';
}
}
}

Output

ABA

ABCBA

ABCDCBA

ABCDEDCBA

You might also like