You are on page 1of 38

C#

1)Hello world

using System;

namespace hello

class HelloWorld

static void Main(string[] args)

Console.WriteLine("Hello World");

2)Hello world with double quotes

using System;

namespace hai

class Hello

static void Main(string[] args)

string str="\"Hello World\"";

Console.WriteLine("{0}",str);

3)Get input

using System;

namespace hai
{

public class Program

public static void Main(string[] args)

Console.WriteLine("Enter your first name");

var x= Console.ReadLine();

Console.WriteLine("Enter your last name");

var y= Console.ReadLine();

Console.WriteLine("Your full name is {0} {1}",x,y);

FUNDAMENTALS

1)Booleanresult

using System;

namespace hai

class pro

static void Main(string[] args)

Console.WriteLine("Enter the value for x");

var x=Convert.ToInt32(Console.ReadLine());

Console.WriteLine("Enter the value for y");

var y=Convert.ToInt32(Console.ReadLine());

if(true)

Console.WriteLine("x is less than y is true");


}

2)User details

using System;

namespace j

class m

static void Main(string[] args)

Console.WriteLine("Enter name");

var name=Console.ReadLine();

Console.WriteLine("Enter age(completed years and months)");

var age=Console.ReadLine();

Console.WriteLine("Enter gender('M' for Male and 'F' for Female)");

var g=Console.ReadLine();

Console.WriteLine("Enter city");

var c=Console.ReadLine();

Console.WriteLine("Enter mobile number");

var m=Console.ReadLine();

Console.WriteLine("Enter pincode");

var p=Console.ReadLine();

Console.WriteLine("Name: "+name);

Console.WriteLine("Age: "+age);

Console.WriteLine("Gender: "+g);

Console.WriteLine("City: "+c);

Console.WriteLine("Mobile: "+m);

Console.WriteLine("Pincode: "+p);
}

3)Registration form

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

namespace ProgFundamentals1 //DO NOT CHANGE the name of namespace

public class Program //DO NOT CHANGE the name of class 'Program'

public static void Main(string[] args) //DO NOT CHANGE 'Main' Signature

//Fill the code here

Console.WriteLine("Enter your name :");

string name = Console.ReadLine();

Console.WriteLine("Enter your age :");

int age = Convert.ToInt32(Console.ReadLine());

Console.WriteLine("Enter your country :");

string country = Console.ReadLine();

Console.WriteLine("Welcome "+name+". Your age is "+age+" and you are from" +country);

}
4)Generate bill details

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

namespace ProgFundamentals2 //DO NOT Change namespace name

public class Program //DO NOT Change class 'Program' name

public static void Main(string[] args) //DO NOT Change 'Main' method Signature

//Implement your code here

int pizzap = 200;

int puffsp = 40;

int coldp = 120;

double total = 0;

Console.Write("Enter the number of pizzas bought : ");

int pizzas=Convert.ToInt32(Console.ReadLine());

total=pizzas*pizzap;

Console.Write("Enter the number of puffs bought : ");

int puff=Convert.ToInt32(Console.ReadLine());

Console.Write("Enter the number of pepsi bought : ");

total=total+(puff*puffsp);

int pep=Convert.ToInt32(Console.ReadLine());

total=total+(pep*coldp);

double gst = (total*12)/100;

double cess = (total*5)/100;

Console.WriteLine("Bill Details");

Console.WriteLine("");
Console.WriteLine("Cost of Pizzas :{0}",(pizzas*pizzap));

Console.WriteLine("Cost of Puffs :{0}",(puff*puffsp));

Console.WriteLine("Cost of Pepsis :{0}",(pep*coldp));

Console.WriteLine("GST 12% :{0}",gst);

Console.WriteLine("CESS 5% :{0}",cess);

Console.WriteLine("Total Price :{0}",total);

5)Validate spelling correctness

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

namespace ProgFundamentals3 //DO NOT CHANGE the name of namespace

public class Program //DO NOT CHANGE the name of class 'Program'

public static void Main(string[] args) //DO NOT CHANGE 'Main' Signature

//Fill the code here

Console.WriteLine("Enter letter 1 : ");

char a=Console.ReadLine()[0];

Console.WriteLine("Enter letter 2 : ");

char b=Console.ReadLine()[0];

Console.WriteLine("Enter letter 3 : ");


char c=Console.ReadLine()[0];

Console.WriteLine("Enter letter 4 : ");

char d=Console.ReadLine()[0];

Console.WriteLine("Enter letter 5 : ");

char e=Console.ReadLine()[0];

Console.WriteLine("Enter letter 6 : ");

char f=Console.ReadLine()[0];

Console.WriteLine("Enter letter 7 : ");

char g=Console.ReadLine()[0];

Console.WriteLine("Enter letter 8 : ");

char h=Console.ReadLine()[0];

Console.WriteLine("Enter letter 9 : ");

char i=Console.ReadLine()[0];

Console.WriteLine("Enter letter 10 : ");

char j=Console.ReadLine()[0];

if(a=='c')

Console.WriteLine("Enter letter 1 : "+a);

else

Console.WriteLine("Enter letter 1 : "+a);

Console.WriteLine("The spelling is wrong");

if(b=='a')

Console.WriteLine("Enter letter 1 : "+b);

else

Console.WriteLine("Enter letter 1 : "+b);


Console.WriteLine("The spelling is wrong");

if(c=='s')

Console.WriteLine("Enter letter 1 : "+c);

else

Console.WriteLine("Enter letter 1 : "+c);

Console.WriteLine("The spelling is wrong");

if(d=='a')

Console.WriteLine("Enter letter 1 : "+d);

else

Console.WriteLine("Enter letter 1 : "+d);

Console.WriteLine("The spelling is wrong");

if(e=='b')

Console.WriteLine("Enter letter 1 : "+e);

else

Console.WriteLine("Enter letter 1 : "+e);

Console.WriteLine("The spelling is wrong");

if(f=='l')

{
Console.WriteLine("Enter letter 1 : "+f);

else

Console.WriteLine("Enter letter 1 : "+f);

Console.WriteLine("The spelling is wrong");

if(g=='a')

Console.WriteLine("Enter letter 1 : "+g);

else

Console.WriteLine("Enter letter 1 : "+g);

Console.WriteLine("The spelling is wrong");

if(h=='n')

Console.WriteLine("Enter letter 1 : "+h);

else

Console.WriteLine("Enter letter 1 : "+h);

Console.WriteLine("The spelling is wrong");

if(i=='c')

Console.WriteLine("Enter letter 1 : "+i);

else

{
Console.WriteLine("Enter letter 1 : "+i);

Console.WriteLine("The spelling is wrong");

if(j=='a')

Console.WriteLine("Enter letter 1 : "+j);

else

Console.WriteLine("Enter letter 1 : "+j);

Console.WriteLine("The spelling is wrong");

6)Draw patterns

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

namespace ProgFundamentals4 //DO NOT CHANGE the name of namespace

public class Program //DO NOT CHANGE the name of class 'Program'

public static void Main(string[] args) //DO NOT CHANGE 'Main' Signature

Console.WriteLine("Enter any alphabet : ");


char c=Convert.ToChar(Console.ReadLine());

//Get the value

Console.WriteLine("Enter number of repeats : ");

int r=Convert.ToInt32(Console.ReadLine());

//Get the value

//Implement your code here

int ch=r/2+1;

for(int i=1;i<=ch;i++)

for(int j=1;j<=(ch-i);j++)

Console.Write(" ");

for(int k=1;k<=i;k++)

Console.Write("{0}",c);

for(int l=(i-1);l>=1;l--)

Console.Write("{0}",c);

Console.WriteLine();

7)Grade calculation
using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

namespace ProgFundamentals5 //DO NOT CHANGE the name of namespace

public class Program //DO NOT CHANGE the name of class 'Program'

public static void Main(string[] args) //DO NOT CHANGE 'Main' Signature

//Implement your code here

Console.WriteLine("Enter English marks:");

int e=Convert.ToInt32(Console.ReadLine());

Console.WriteLine("Enter Math marks:");

int m=Convert.ToInt32(Console.ReadLine());

Console.WriteLine("Enter Language marks:");

int l=Convert.ToInt32(Console.ReadLine());

if(m>0&&e>0&&l>0&&e<=100&&m<=100&&l<=100)

if(e>=80&&e<=100)

Console.WriteLine("English Grade : A+");

else if(e>=60&&e<80)

Console.WriteLine("English Grade : A");

else if(e>=40&&e<60)
{

Console.WriteLine("English Grade : B");

else

Console.WriteLine("English Grade : FAIL");

if(m>=80&&m<=100)

Console.WriteLine("Math Grade : A+");

else if(m>=60&&m<80)

Console.WriteLine("Math Grade : A");

else if(m>=40&&m<60)

Console.WriteLine("Math Grade : B");

else

Console.WriteLine("Math Grade : FAIL");

if(l>=80&&l<=100)

Console.WriteLine("Language Grade : A+");

else if(l>=60&&l<80)

Console.WriteLine("Language Grade : A");

}
else if(l>=40&&l<60)

Console.WriteLine("Language Grade : B");

else

Console.WriteLine("Language Grade : FAIL");

else

Console.WriteLine("Mark cannot be less than 0");

8)Print even numbers from the ggiven number

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

namespace ProgFundamentals8 //DO NOT CHANGE the name of namespace

public class Program //DO NOT CHANGE the name of class 'Program'

public static void Main(string[] args) //DO NOT CHANGE 'Main' Signature

{
// Fill the code here

int x=0;

Console.WriteLine("Enter starting number : ");

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

int i=x;

while(x<(i+19))

if(x%2==0)

x+=2;

Console.Write(x+" ");

else

x+=1;

Console.Write(x+" ");

9)Clearance sale

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

namespace ProgFundamentals7 //DO NOT CHANGE the name of namespace


{

public class Program //DO NOT CHANGE the name of class 'Program'

public static void Main(string[] args) //DO NOT CHANGE 'Main' Signature

//Implement your code here

double one, two,three;

string f2="no";

Console.WriteLine("Enter the name of the product :");

string productname = Console.ReadLine();

Console.WriteLine("Enter the price of the product :");

double price = Convert.ToDouble(Console.ReadLine());

Console.WriteLine("Is the product on SALE(yes/no) :");

string f = Console.ReadLine();

Console.WriteLine("Enter number of product sold in day 1");

double day1=Convert.ToDouble(Console.ReadLine());

Console.WriteLine("Enter number of product sold in day 2");

double day2=Convert.ToDouble(Console.ReadLine());

Console.WriteLine("Enter number of product sold in day 3");

double day3=Convert.ToDouble(Console.ReadLine());

if(f.Equals(f2))

one = price*day1;

two = price*day2;

three = price*day3;

else

one = (price*day1)-((price*day1*7)/100);

price = price-((price*7)/100);

two = (price*day2)-((price*day2*7)/100);
price = price-((price*7)/100);

three = (price*day3)-((price*day3*7)/100);

Console.WriteLine(productname);

Console.WriteLine("Day 1 sales total : "+one);

Console.WriteLine("Day 2 sales total : "+two);

Console.WriteLine("Day 3 sales total : "+three);

10)Member registration

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

namespace ProgFundamentals9 //DO NOT CHANGE the name of namespace

public class Program //DO NOT CHANGE the name of class 'Program'

public static void Main(string[] args) //DO NOT CHANGE 'Main' Signature

// Fill the code here

string[] name= new string[10];

string[] place=new string[10];

string[] ms=new string[10];

string cnf="y";

int i=0;
while(cnf.Equals("y"))

Console.Write("Enter Name : ");

name[i]=Console.ReadLine();

Console.Write("Enter Place : ");

place[i]=Console.ReadLine();

Console.Write("Enter marital status(y/n) :");

ms[i]=Console.ReadLine();

Console.Write("Do you wish to continue(y/n) :");

cnf=Console.ReadLine();

i++;

for(int j=0; j<i;j++)

Console.WriteLine("Confirming Information");

Console.WriteLine(name[j]);

Console.WriteLine(place[j]);

if(ms[j].Equals("y"))

Console.WriteLine("Married : y");

else

Console.WriteLine("Married : n");

11)Maximum enrollment
using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

namespace Departments //DO NOT CHANGE the name of namespace

public class Program //DO NOT CHANGE the name of class 'Program'

public static void Main(string[] args) //DO NOT CHANGE 'Main' Signature

Console.WriteLine("Total students placed in CS ");

int c=Convert.ToInt32(Console.ReadLine());

//Add code here

Console.WriteLine("Total students placed in MECH ");

int m=Convert.ToInt32(Console.ReadLine());

//Add code here

Console.WriteLine("Total students placed in MET ");

int me=Convert.ToInt32(Console.ReadLine());

//Add code here

//Implement the logic here

if(c>m&&c>me)

Console.WriteLine("Highest placement CS");

else if(m>me&&c<m)

{
Console.WriteLine("Highest placement MECH");

else if(me>m&&c<me)

Console.WriteLine("Highest placement MET");

else if(c==m)

Console.WriteLine("Highest placement CS");

Console.WriteLine("Highest placement MECH");

else if(me==m)

Console.WriteLine("Highest placement MECH");

Console.WriteLine("Highest placement MET");

else if(me==m)

Console.WriteLine("Highest placement MET");

Console.WriteLine("Highest placement CS");

12)Capture score

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;
namespace Score //DO NOT CHANGE the name of namespace

public class Program //DO NOT CHANGE the name of class 'Program'

public static void Main(string[] args) //DO NOT CHANGE 'Main' Signature

//Implement code here

Console.Write("Player Name : ");

string p1= Console.ReadLine();

Console.Write("Player Name : ");

string p2= Console.ReadLine();

Console.Write("player Name : ");

string p3= Console.ReadLine();

Console.Write("player Name : ");

string p4= Console.ReadLine();

Console.Write("player Name : ");

string p5= Console.ReadLine();

Console.Write("player Name : ");

string p6= Console.ReadLine();

int b=0, d=0, t=0;

string bn= "Bill", dn="Daisy", tn="Tommy";

if(p1.Equals (bn))

b++;

if(p2.Equals (bn))

b++;

if(p3.Equals (bn))
{

b++;

if(p4.Equals (bn))

b++;

if(p5.Equals (bn))

b++;

if(p6.Equals (bn))

b++;

if(p1.Equals (bn) && p2.Equals (bn))

b++;

if(p2.Equals (bn) && p3.Equals (bn))

b++;

if(p3.Equals (bn) && p4.Equals (bn))

b++;

if(p4.Equals (bn) && p5.Equals (bn))

b++;

}
if(p5.Equals (bn) && p6.Equals (bn))

b++;

if(p1.Equals (tn))

t++;

if(p2.Equals (tn))

t++;

if(p3.Equals (tn))

t++;

if(p4.Equals (tn))

t++;

if(p5.Equals (tn))

t++;

if(p6.Equals (tn))

t++;

if(p1.Equals(tn) && p2.Equals(tn))

t++;
}

if(p2.Equals(tn) && p3.Equals(tn))

t++;

if(p3.Equals(tn) && p4.Equals(tn))

t++;

if(p4.Equals(tn) && p5.Equals(tn))

t++;

if(p5.Equals(tn) && p6.Equals(tn))

t++;

if(p1.Equals(dn))

d++;

if(p2.Equals(dn))

d++;

if(p3.Equals(dn))

d++;

if(p4.Equals(dn))

{
d++;

if(p5.Equals(dn))

d++;

if(p6.Equals(dn))

d++;

if(p1.Equals (dn) && p2.Equals(dn))

d++;

if(p2.Equals (dn) && p3.Equals(dn))

d++;

if(p3.Equals (dn) && p4.Equals(dn))

d++;

if(p4.Equals (dn) && p5.Equals(dn))

d++;

if(p5.Equals (dn) && p6.Equals(dn))

d++;

Console.WriteLine("Bill score : {0}",b);


Console.WriteLine("Tommy Score : {0}",t);

Console.WriteLine("Daisy Score : {0}",d);

13)Test your logic

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

namespace ProgFundamentals12 //DO NOT CHANGE the name of namespace

public class Program //DO NOT CHANGE the name of class 'Program'

public static void Main(string[] args) //DO NOT CHANGE 'Main' Signature

// Fill the code here

Console.Write("Number of Rows : ");

int row=Convert.ToInt32(Console.ReadLine());

Console.Write("Number of Symbols : ");

int symbol = Convert.ToInt32(Console.ReadLine());

for(int i=0;i<(row-1);i++)

for(int j=0;j<symbol;j++)

Console.Write("/\\");
}

Console.WriteLine();

Console.Write("|");

for(int k=0;k<(symbol*2-2);k++)

Console.Write(" ");

Console.WriteLine("|");

for(int j=0;j<symbol;j++)

Console.Write("/\\");

CLASSES AND OBJECTS

1)Display product properties

using System;

class pro

static void Main(string[] args)

Product obj = new Product();

Console.WriteLine("Enter product id");

obj.ProductId=Convert.ToInt32(Console.ReadLine());

Console.WriteLine("Enter product name :");

obj.ProductName=Console.ReadLine();

Console.WriteLine("Enter product price");


obj.Price = double.Parse(Console.ReadLine());

class Product

private int productId;

private string productName;

private double price;

public string ProductName{

get{return productName;}

set{productName = value;}

public int ProductId

get{return productId;}

set{productId = value;}

public double Price

get

if(price<0)

price = 0;

return price;

else

{
return price;

set{price = value;}

public void Display()

Console.WriteLine("Product ID:"+ProductId);

Console.WriteLine("ProductName:"+ProductName);

Console.WriteLine("Product Price:"+Price);

2)Find the age of a person

using System;

using System.IO;

public class Person

//Fill code here

private string firstName;

private string lastName;

private DateTime dob;

private string age;

public string FirstName

get{return firstName;}

set{firstName = value;}

public string LastName

{
get{return lastName;}

set{lastName = value;}

public DateTime Dob{

get{return dob;}

set{dob = value;}

public string Adult

get{

if(GetAge(dob)>=18)

return "Adult";

else

return "False";

public int GetAge(DateTime dob)

int y = new DateTime(DateTime.Now.Subtract(dob).Ticks).Year;

return y;

public void DisplayDetails()

Console.WriteLine("First Name: "+firstName);

Console.WriteLine("Last Name: "+lastName);

Console.WriteLine("Age: "+GetAge(dob));

Console.WriteLine(Adult);

public class Progarm

{
public static void Main(String[] args)

Person p = new Person();

Console.WriteLine("Enter first name");

p.FirstName = Console.ReadLine();

Console.WriteLine("Enter last name");

p.LastName = Console.ReadLine();

Console.WriteLine("Entre date of birth in yyyy/mm/dd format");

p.Dob = Convert.ToDateTime(Console.ReadLine());

p.DisplayDetails();

3)Account details

//Implement code here

using System;

class program

static void Main(string[] args)

Console.WriteLine("Enthe account id");

int Id1 = Convert.ToInt32(Console.ReadLine());

Console.WriteLine("Enter account type");

string AccountType1 = Console.ReadLine();

Console.WriteLine("Enter account balance");

double Balance1 = double.Parse(Console.ReadLine());

Account obj = new Account(Id1, AccountType1, Balance1);

obj.GetDetails();

class Account
{

private int id;

private string accountType;

private double balance;

public int Id

get{return id;}

set{id=value;}

public string AccountType

get{return accountType;}

set{accountType = value;}

public double Balance

get

if(balance<0)

balance=0;

return balance;

else

return balance;

set{balance = value; }

public Account(){}
public Account (int Id, string AccountType, double Balance)

this.Id = Id;

this.AccountType = AccountType;

this.Balance = Balance;

public void GetDetails()

Console.WriteLine("Account Id: "+Id);

Console.WriteLine("Account Type: "+AccountType);

Console.WriteLine("Balance: "+Balance);

public bool WithDraw(double amount)

if(balance<amount)

return false;

else

Balance = Balance-amount;

return true;

4)Calculatorprogram

using System;

public class CalculatorProgram

{
public int Addition(int a, int b)

return(a+b);

public int Subtraction(int a, int b)

return(a-b);

public int Multiplication(int a, int b)

return(a*b);

public double Division(int a,int b,out double remainder)

remainder = a%b;

return (a/b);

public class Program{

static void Main()

CalculatorProgram p =new CalculatorProgram();

Console.WriteLine("Enter the operator");

string opr = Console.ReadLine();

Console.WriteLine("Enter the operand");

int a = Convert.ToInt32(Console.ReadLine());

int b = Convert.ToInt32(Console.ReadLine());

if(opr=="+")

Console.WriteLine("result of {0} + {1} is{2}",a,b,p.Addition(a,b));

}
if(opr=="-")

Console.WriteLine("result of {0} - {1} is{2}",a,b,p.Subtraction(a,b));

if(opr=="*")

Console.WriteLine("result of {0} + {1} is{2}",a,b,p.Multiplication(a,b));

if(opr=="/")

double div=p.Division(a,b, out double remainder);

Console.WriteLine("result of {0} / {1} is {2}",a,b,div);

Console.WriteLine("Remainder ={0}",remainder);

STRINGS & ARRAYS

1)Stringconcatenate

using System;

class con

static void Main(string[] args)

Console.WriteLine("Enter first name");

string u=Console.ReadLine();

Console.WriteLine("Enter last name");

string v=Console.ReadLine();

Console.WriteLine("Full name : "+u+" "+v);

}
2)Reverse a sentence

using System;

using System.Linq;

using System.Threading.Tasks;

using System.IO;

namespace Program

class Program

static void Main(string[] args)

int temp,i;

Console.WriteLine("Enter a string");

string s =Console.ReadLine();

string[] a =s.Split(' ');

int k = a.Length - 1;

temp = k;

for( i=k; temp>=0; k--)

Console.Write(a[temp] + "" + ' ');

--temp;

Console.ReadKey();

3)Change a word

using System;

class pro
{

static void Main(string[] args)

Console.WriteLine("Enter a string");

string u = Console.ReadLine();

string t="the";

if(u.Contains(t))

Console.WriteLine(u.Replace("the","that"));

else

Console.WriteLine("Word 'the' not found");

4)Rectangulararray

using System;

public class Program //DO NOT change the class name

static void Main(string[] args) //DO NOT change the 'Main' method signature

//Fill code here

Console.WriteLine("Enter a number:");

int n=Convert.ToInt32(Console.ReadLine());

int[,] arr = new int[50,50];

arr = GetArray(n);

for(int k = 0;k<n;k++)

{
for(int l=0;l<n;l++)

Console.Write(string.Format("{0}", arr[k,l]));

Console.Write(Environment.NewLine);

public static int[,] GetArray(int num)

int[,] a =new int[num,num];

for(int i=0;i<num;i++)

for(int j=0;j<num;j++)

if(i==j)

a[i,j] = 0;

else if(j>i)

a[i,j] = 1;

else

a[i,j]=-1;

return a;

You might also like