0% found this document useful (0 votes)
12 views24 pages

Journal C

The document provides code examples demonstrating various C# programming concepts including: 1. Printing "Hello World" using command line arguments. 2. Using switching, looping, and branching statements like switch cases, for loops, while loops, and if/else conditions. 3. Passing parameters by value and reference and swapping numbers. 4. Using explicit and implicit keywords. 5. Calculating factorials, prime factors, and checking if a number is even or odd. 6. Demonstrating arrays and inheritance including single, multilevel, and hierarchical inheritance. 7. Demonstrating interfaces. The document contains 11 code examples covering core C# programming topics

Uploaded by

Sanika
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
12 views24 pages

Journal C

The document provides code examples demonstrating various C# programming concepts including: 1. Printing "Hello World" using command line arguments. 2. Using switching, looping, and branching statements like switch cases, for loops, while loops, and if/else conditions. 3. Passing parameters by value and reference and swapping numbers. 4. Using explicit and implicit keywords. 5. Calculating factorials, prime factors, and checking if a number is even or odd. 6. Demonstrating arrays and inheritance including single, multilevel, and hierarchical inheritance. 7. Demonstrating interfaces. The document contains 11 code examples covering core C# programming topics

Uploaded by

Sanika
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 24

1.Write a C# program that print hello world using command line argument.

Code :

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

namespace hello
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello ");
}
}
}

Output :
2. Write a console application program to demonstrate switching ,looping ,branching
statement.
 Switching statement
Code :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace switching
{
class Program
{
static void Main(string[] args)
{
int day = 4;
switch (day)
{
case 1:
Console.WriteLine("Monday");
break;
case 2:
Console.WriteLine("Tuesday");
break;
case 3:
Console.WriteLine("Wednesday");
break;
case 4:
Console.WriteLine("Thursday");
break;
case 5:
Console.WriteLine("Friday");
break;
case 6:
Console.WriteLine("Saturday");
break;
case 7:
Console.WriteLine("Sunday");
break;
}
}
}
}
Output :

Lopping statement
Code
 For loop:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace forloop
{
class Program
{
static void Main(string[] args)
{
for (int i = 0; i< 5; i++)
{
Console.WriteLine(i);
}
}
Output:

 While loop :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace whileloop
{
class Program
{
static void Main(string[] args)
{
int i = 0;
while (i< 5)
{
Console.WriteLine(i);
i++;
}
}
}
}
Output :

Branching statement :
 If-else :
Code:

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

namespace ifelsestate
{
class Program
{
static void Main(string[] args)
{
int time = 22;
if (time < 10)
{
Console.WriteLine("Good morning.");
}
else if (time < 20)
{
Console.WriteLine("Good day.");
}
else
{
Console.WriteLine("Good evening.");
Console.Readline();
}
}
}
}

Output :
3.write a console application for swapping of 2 numbers using pass by value.
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace swapping
{
class Program
{
static void Main(string[] args)
{
int a = 5, b = 10;
Console.WriteLine("Before swap a= " + a + " b= " + b);
a = a * b;
b = a / b;
a = a / b;
Console.Write("After swap a= " + a + " b= " + b);
Console.Readline();
}
}
}
Output :
4. write a console application for swapping of 2 numbers using pass by reference.
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace passbyreference
{
class Program
{
public void swap(ref int x, ref int y)
{
int temp;

temp = x;
x = y;
y = temp;
}
static void Main(string[] args)
{
Program n = new Program();

int a = 100;
int b = 200;

Console.WriteLine("Before swap, value of a : {0}", a);


Console.WriteLine("Before swap, value of b : {0}", b);

n.swap(ref a, ref b);

Console.WriteLine("After swap, value of a : {0}", a);


Console.WriteLine("After swap, value of b : {0}", b);

Console.ReadLine();

}
}
}

Output:
5.write a C# program that uses explicit keyword.
Code :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace explicitprg
{
class Program
{
static void Main(string[] args)
{
int num = 23;
object obj = num;// implicit conversion
int i = (int)obj;// explicit conversion
Console.WriteLine("Value of ob object is :" + obj);
Console.WriteLine("Value of i is:"+i);
Console.ReadLine();
}
}
}
Output :
6.write a C# program that uses implicit keyword.
Code :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace implicitprg
{
class Program
{
static void Main(string[] args)
{
int num = 23;
object obj = num;// implicit conversion
int i = (int)obj;// explicit conversion
Console.WriteLine("Value of ob object is :" + obj);
Console.WriteLine("Value of i is:"+i);
Console.ReadLine();
}
}
}
Output :
7.write C# program to display factorial of number.
Code :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace factnum
{
class Program
{
static void Main(string[] args)
{
int i, number, fact;
Console.WriteLine("Enter the 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 :
8. write C# program to display prime factors of entered number.
Code :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace primefactor
{
class Program
{
static void Main(string[] args)
{
int n, i;
Console.WriteLine("Enter the Number:");
n = int.Parse(Console.ReadLine());
Console.WriteLine("Factors:");
for (i = 1; i<= n; i++)
{
if (n % i == 0)
{
Console.WriteLine(i);
}
}
Console.ReadLine();
}
}
}
Output :
9.write C# program check entered number is even or odd.
Code :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace evenorodd
{
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.ReadLine();
}
else
{
Console.Write("Entered Number is an Odd Number");
Console.ReadLine();
}
}
}
}
Output :
10.write C# program to demonstrate array.
Code :

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

namespace demonstrate
{
class Program
{
public class GFG
{

static void Main(string[] args)


{
int n = 10;
int[] arr = new int[n];
int i;

for (i = 0; i < n; i++)


arr[i] = i + 1;

for (i = 0; i < n; i++)


Console.Write(arr[i] + " ");
Console.WriteLine();

int x = 50;
int pos = 5;

int[] newarr = new int[n + 1];


for (i = 0; i < n + 1; i++)
{
if (i < pos - 1)
newarr[i] = arr[i];
else if (i == pos - 1)
newarr[i] = x;
else
newarr[i] = arr[i - 1];
}

for (i = 0; i < n + 1; i++)


Console.Write(newarr[i] + " ");
Console.WriteLine();
Console.ReadLine();
}
}
}
}
Output:
11. Write a C# program to demonstrate inheritance.
Single inheritance
Code :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace singleinheritance
{
class A
{
public void msg()
{
Console.WriteLine("this is a class method");
}
}
class B : A
{
public void info()
{
Console.WriteLine("this is b class method");
}
}
class Program
{
static void Main(string[] args)
{
B objb = new B();
objb.msg();
objb.info();
Console.ReadLine();
}
}
}
Output :
Multilevel inheritance
Code :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace multilevelinherit
{
class A
{
public void msg()
{
Console.WriteLine("Yashwantrao Chavan Mahavidyalya ");
}
}
class B : A
{
public void info()
{
Console.WriteLine("UrunIslampur");
}
}
class C : B
{
public void display()
{
Console.WriteLine("Tal.Walwa,Dist.Sangli");
}
}
class Program
{
static void Main(string[] args)
{
C objc = new C();
objc.msg();
objc.info();
objc.display();
Console.ReadLine();
}
}
}
Output :

Hierarchical inheritance
Code :

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

namespace hierarchicalinheritance
{
class A
{
public void msg()
{
Console.WriteLine("this is a class method");
}
}
class B : A
{
public void info()
{
Console.WriteLine("this is b class method");
}
}
class C : A
{
public void display()
{
Console.WriteLine("this is c class method");
}
}
class D : A
{
public void show()
{
Console.WriteLine("this is d class method");
}
}
class Program
{
static void Main(string[] args)
{
B objb = new B();
C objc = new C();
D objd = new D();
objb.msg();
objb.info();
objc.msg();
objc.display();
objd.msg();
objd.show();
Console.ReadLine();
}
}
}
Output :
12.Write C# program to demonstrate interface.
Code :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace multipleinterface
{
interface calc1
{
int add(int a, int b);
}
interface calc2
{
int sub(int x, int y);
}
class Calculation : calc1, calc2
{
public int result1;
public int add(int a, int b)
{
return result1 = a + b;
}
public int result2;
public int sub(int x, int y)
{
return result2 = x - y;
}
}
class Program
{
static void Main(string[] args)
{
Calculation c = new Calculation();
c.add(8, 2);
c.sub(20, 10);
Console.WriteLine("Multiple Inheritance concept Using Interfaces :\n ");
Console.WriteLine("Addition: " + c.result1);
Console.WriteLine("Substraction: " + c.result2);
Console.ReadKey();

}
}
}
Output :
13.Write C# program to demonstrate abstract class.
Code :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace abstractclass
{
abstract class areaclass
{
abstract public int area();
}
class square :areaclass
{
int side = 0;
public square(int n)
{
side = n;
}
public override int area()
{
return side * side;
}
}
class Program
{
static void Main(string[] args)
{
square S =new square(6);
Console.WriteLine("area=" + S.area());
Console.ReadLine();
}
}
}
Output:

You might also like