Obtains Four Int Values From The User and Displays The Product
Obtains Four Int Values From The User and Displays The Product
Obtains Four Int Values From The User and Displays The Product
Program :
using System; class Program { static void Main(string[] args) { int firstNumber, secondNumber, thirdNumber, fourthNumber; Console.WriteLine("Give me a number:"); firstNumber = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("Give me another number:"); secondNumber = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("Give me another number:"); thirdNumber = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("Give me another numbe2r:"); fourthNumber = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("The product of {0}, {1}, {2}, and {3} is {4}.", firstNumber, secondNumber, thirdNumber, fourthNumber, firstNumber * secondNumber * thirdNumber * fourthNumber); } }
OUTPUT:
If you have two integers stored in variables var1 and var2, what Boolean test can you perform to see if one or the other (but not both) is greater than 10?
Program :
using System; class Program { static void Main(string[] args) { Console.Write("Enter 1st integer:"); int var1 = Convert.ToInt32 (Console.ReadLine());
bool boolanswer; boolanswer = (var1 > 10 && var2 > 10); if (boolanswer) { Console.WriteLine("Condition is False: \n var1 greater than 10 & var2 greater than 10"); } }
OUTPUT:
Write programs using conditional statements and loops: i. Generate Fibonacci series.
Program :
using System; public class Program { static void Main(string[] args) { Console.Write("Enter value of n: "); int n=Convert.ToInt32(Console.ReadLine()); int num = Convert.ToInt32(n); int f1=0,f2=0,f3=1; for(int i=1;i<=num;i++){ Console.WriteLine(f3); f1=f2; f2=f3; f3=f1+f2; } }}
OUTPUT :
OUTPUT:
Console.WriteLine("Enter no.:"); int n = int.Parse(Console.ReadLine()); for (int i = 2; i < n; i++) { if (n % i == 0) { Console.WriteLine(n + " is not prime no.:"); } else { Console.WriteLine(n + " is prime no.:"); } } }
OUTPUT:
using System; class Program { static void Main(string[] args) { int no,counter,counter1,check; Console.WriteLine("PRIME NO. SERIES"); Console.WriteLine("\nINPUT THE VALUE OF N: "); no=Convert.ToInt32(Console.ReadLine()); Console.WriteLine("\n\nTHE PRIME NO. SERIES B/W 1 TO "+no+" :");
for(counter = 1; counter <= no; counter++){ check = 0; for(counter1 = counter-1; counter1 > 1 ; counter1--) if(counter%counter1 == 0){ check++; break; } if(check == 0) Console.WriteLine("\t"+counter); } Console.WriteLine("\n"); }}
OUTPUT:
using System; class Program { static void Main(string[] args) { int n1= 0; Console.WriteLine("Enter No."); int n = Convert.ToInt32(Console.ReadLine()); int r = 0;
while (n > 0) { int r1 = n % 10; n1 = n1 + r1; r = (r * 10) + r1; n = n / 10; } Console.WriteLine("Reverse={0}", r); Console.WriteLine("Addition={0}", n1); } }
OUTPUT:
case 'A': case 'e': case 'E': case 'i': case 'I': case 'o': case 'O': case 'u': case 'U': count++; break; default: break; }} Console.WriteLine("No. of vowels={0}", count); }
OUTPUT :
OUTPUT :
OUTPUT :
Operator Overloading
PROGRAM :
using System; class Distance { int m, cm; public Distance(int m, int cm) { this.m = m + cm / 100; this.cm = cm % 100;} public void Show() { Console.WriteLine("{0} m {1} cm", m, cm); } public static bool operator <(Distance d1, Distance d2) { return (d1.m * 100 + d1.cm < d2.m * 100 + d2.cm); } public static bool operator >(Distance d1, Distance d2) { return (d1.m * 100 + d1.cm > d2.m * 100 + d2.cm); } public static Distance operator +(Distance d1, Distance d2) { return new Distance(d1.m + d2.m, d1.cm + d2.cm);
} public static explicit operator int(Distance d) { return d.m * 100 + d.cm; }} class Program { static void Main(string[] args) { Distance d1 = new Distance(3, 2); Distance d2 = new Distance(10, 205); Distance d3; d3 = d1 + d2; if (d1 < d2) d1.Show(); else d2.Show(); d3.Show(); int k = (int)d3; Console.WriteLine(k); }}}
OUTPUT :
Inheritance
PROGRAM :
using System; class Inherit_A { public void display() { Console.WriteLine(" Grand Father "); } class Inherit_B : Inherit_A { public void display1() { Console.WriteLine(" Father"); } } class Inherit_C : Inherit_B { public void display2() { Console.WriteLine(" Child"); } } static void Main(string[] args) { Inherit_C c = new Inherit_C(); c.display(); c.display1(); c.display2(); } }
OUTPUT :
Constructor overloading
PROGRAM :
using System; class ConOver { private int num1, num2, total; public ConOver() {
num1 = num2 = total = 0; } public ConOver(int n1,int n2) { num1 = n1; num2 = n2; total = 0; } public void AddNum() { Console.WriteLine("\n Number1 : " + num1 + "\tNumber2 : " + num2); total = num1 + num2; Console.WriteLine("\n Addition : " + total); } static void Main(string[] args) { ConOver a = new ConOver(55, 25); ConOver a1 = new ConOver(25, 25); a.AddNum(); a1.AddNum(); }}
OUTPUT :
Interfaces
PRORGRAM :
using System; interface Addition { int Add(); } interface Multiplication { int Mul(); } class Computation:Addition,Multiplication {
int x,y; public Computation(int x,int y) { this.x=x; this.y=y; } public int Add(){ return(x+y); } public int Mul() { return(x*y); }} class Prog { static void Main(string[] args) { Computation c=new Computation(14,5); Addition add=(Addition)c; Console.WriteLine("Addition="+add.Add()); Multiplication mul=(Multiplication)c; Console.WriteLine("Multiplication="+mul.Mul()); }}
OUTPUT :
PROGRAM :
using System; delegate int ArithOperation(int x,int y); class Arithmetic { public static int Add(int a,int b) { return(a+b); } public static int Sub(int a,int b) { return(a-b); } } class Prog { static void Main(string[] args) { ArithOperation Operation=new ArithOperation(Arithmetic.Add); ArithOperation Operation1=new ArithOperation(Arithmetic.Sub); int result=Operation(20,20); int result1=Operation1(55,5); Console.WriteLine("Addition="+result); Console.WriteLine("Substraction="+result1); }}
OUTPUT :