C# Program
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication2
{
public class A
{
public A()
{
Console.WriteLine("A");
}
public virtual void method()
{
Console.WriteLine("AM");
}
}
public class B : A
{
public B()
{
Console.WriteLine("B");
}
public new void method()
{
Console.WriteLine("BM");
}
}
class Program
{
static void Main(string[] args)
{
// A obj = new A();
// obj.method();
// Output--A,AM
// B obj = new B();
// obj.method();
// Output--A,B,BM
// A obj = new B();
// obj.method();
// Output--A,B,AM
B obj = new A();// We can't create instance like this from child to parent.
obj.method();
Console.Read();
}
}
}
===================================
Reverse a number
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter a Number");
int numb = int.Parse(Console.ReadLine());
int reverse = 0;
while (numb > 0)
{
int rem = numb % 10;
reverse = (reverse * 10) + rem;
numb = numb / 10;
}
Console.WriteLine("Reverse number={0}", reverse);
Console.ReadLine();
}
}
Output
----------
Enter a Number
4567
Reverse number=7654
Reverse a String
class Program
{
static void Main(string[] args)
{
string str = "", reverse = "";
int Length = 0;
Console.WriteLine("Enter a Word");
//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 word
Console.WriteLine("Reverse word is {0}",reverse);
Console.ReadLine();
}
}
Output
----------
Enter a Word
Gupta
Reverse number=atpuG
Palindrome Program
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();
}
}
//Output--535 is a prime number(if its reverse also 535 only)
Armstrong Number
An Armstrong number of three digits is an integer such that the sum of the cubes of its digits is equal to the number itself.
class Program
{
static void Main(string[] args)
{
int number, remainder, sum = 0;
Console.Write("enter the 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("Entered Number is an Armstrong Number");
}
else
Console.Write("Entered Number is not an Armstrong Number");
Console.ReadLine();
}
}
Output
----------
Enter the Number : 371
Entered Number is an Armstrong Number
Explantion:3Cube+7cube+1cube=371
Sum of given Number
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();
}
//Output--Enter a Number : 123
//Sum of Digits of the Number : 6
Prime Numbers Between 1 to 10
class Program
{
static void Main(string[] args)
{
bool isPrime = true;
Console.WriteLine("Prime Numbers : ");
for (int i = 2; i <= 10; i++)
{
for (int j = 2; j <= 10; j++)
{
if (i != j && i % j == 0)
{
isPrime = false;
break;
}
}
if (isPrime)
{
Console.Write("\t" + i);
}
isPrime = true;
}
Console.ReadKey();
}
}
//output--2 3 5 7
strong number
A number is called strong number if sum of the factorial of its digit is equal to number itself.
Ex : 1! + 4! + 5! = 1 + 24 + 120 = 145
Code:
static void Main(string[] args)
{
Int32 num, i, f, r, sum = 0, temp;
Console.WriteLine("enter the number");
num = Convert.ToInt16((Console.ReadLine()));
temp=num;
while (num != 0)
{
i = 1; f = 1;
r = num % 10;
while (i <= r)
{
f = f * i;
i++;
}
sum = sum + f;
num = num / 10;
}
if (sum == temp)
Console.WriteLine("Strong No", temp);
else
Console.WriteLine("not Strong NO", temp);
Console.ReadLine();
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication2
{
public class A
{
public A()
{
Console.WriteLine("A");
}
public virtual void method()
{
Console.WriteLine("AM");
}
}
public class B : A
{
public B()
{
Console.WriteLine("B");
}
public new void method()
{
Console.WriteLine("BM");
}
}
class Program
{
static void Main(string[] args)
{
// A obj = new A();
// obj.method();
// Output--A,AM
// B obj = new B();
// obj.method();
// Output--A,B,BM
// A obj = new B();
// obj.method();
// Output--A,B,AM
B obj = new A();// We can't create instance like this from child to parent.
obj.method();
Console.Read();
}
}
}
===================================
Reverse a number
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter a Number");
int numb = int.Parse(Console.ReadLine());
int reverse = 0;
while (numb > 0)
{
int rem = numb % 10;
reverse = (reverse * 10) + rem;
numb = numb / 10;
}
Console.WriteLine("Reverse number={0}", reverse);
Console.ReadLine();
}
}
Output
----------
Enter a Number
4567
Reverse number=7654
Reverse a String
class Program
{
static void Main(string[] args)
{
string str = "", reverse = "";
int Length = 0;
Console.WriteLine("Enter a Word");
//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 word
Console.WriteLine("Reverse word is {0}",reverse);
Console.ReadLine();
}
}
Output
----------
Enter a Word
Gupta
Reverse number=atpuG
Palindrome Program
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();
}
}
//Output--535 is a prime number(if its reverse also 535 only)
An Armstrong number of three digits is an integer such that the sum of the cubes of its digits is equal to the number itself.
class Program
{
static void Main(string[] args)
{
int number, remainder, sum = 0;
Console.Write("enter the 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("Entered Number is an Armstrong Number");
}
else
Console.Write("Entered Number is not an Armstrong Number");
Console.ReadLine();
}
}
Output
----------
Enter the Number : 371
Entered Number is an Armstrong Number
Explantion:3Cube+7cube+1cube=371
Sum of given Number
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();
}
//Output--Enter a Number : 123
//Sum of Digits of the Number : 6
Prime Numbers Between 1 to 10
class Program
{
static void Main(string[] args)
{
bool isPrime = true;
Console.WriteLine("Prime Numbers : ");
for (int i = 2; i <= 10; i++)
{
for (int j = 2; j <= 10; j++)
{
if (i != j && i % j == 0)
{
isPrime = false;
break;
}
}
if (isPrime)
{
Console.Write("\t" + i);
}
isPrime = true;
}
Console.ReadKey();
}
}
//output--2 3 5 7
strong number
A number is called strong number if sum of the factorial of its digit is equal to number itself.
Ex : 1! + 4! + 5! = 1 + 24 + 120 = 145
Code:
static void Main(string[] args)
{
Int32 num, i, f, r, sum = 0, temp;
Console.WriteLine("enter the number");
num = Convert.ToInt16((Console.ReadLine()));
temp=num;
while (num != 0)
{
i = 1; f = 1;
r = num % 10;
while (i <= r)
{
f = f * i;
i++;
}
sum = sum + f;
num = num / 10;
}
if (sum == temp)
Console.WriteLine("Strong No", temp);
else
Console.WriteLine("not Strong NO", temp);
Console.ReadLine();
}
Number sort
int temp = 0;
int[] arr = new int[] { 20, 65, 98, 71, 64, 11, 2, 80, 5, 6, 100, 50, 13, 9, 80, 454 };
for (int i = 0; i < arr.Length; i++)
{
for (int j = i + 1; j < arr.Length; j++)
{
if (arr[i] > arr[j])
{
temp = arr[j];
arr[j] = arr[i];
arr[i] = temp;
}
}
Console.WriteLine(arr[i]);
}
No comments:
Post a Comment