10 Assignment Lab Programs With Answers in C#
10 Assignment Lab Programs With Answers in C#
If else if :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace _1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("enter student name:");
string sname = Console.ReadLine();
Console.WriteLine("enter m1 marks");
int m1 = int.Parse(Console.ReadLine());
Console.WriteLine("enter m2 marks");
int m2 = int.Parse(Console.ReadLine());
Console.WriteLine("enter m3 marks");
int m3 = int.Parse(Console.ReadLine());
int tot = m1 + m2 + m3;
int avg = tot / 3;
Console.WriteLine("avg is" + avg);
if(m1<35||m2<35||m3<35)
{
Console.WriteLine("student " + sname + " is fail");
}
else if (avg >= 60)
{
Console.WriteLine("student "+sname + " having first class");
}
else if (50 <= avg && avg<60)
{
Console.WriteLine("student "+sname + " having second class");
}
else
{
Console.WriteLine("student "+sname+" having third class");
}
Console.ReadLine();
}
}
}
Switch case:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace average
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("enter student name:");
string sname = Console.ReadLine();
Console.WriteLine("enter m1 marks");
int m1 = int.Parse(Console.ReadLine());
Console.WriteLine("enter m2 marks");
int m2 = int.Parse(Console.ReadLine());
Console.WriteLine("enter m3 marks");
int m3 = int.Parse(Console.ReadLine());
int tot = m1 + m2 + m3;
int avg = tot / 3;
Console.WriteLine("avg is" + avg);
switch (m1<35||m2<35||m3<35)
{
case true:
Console.WriteLine("student" + sname + " is fail");
break;
case false:
switch(avg>=35&&avg<50)
{
case true:
Console.WriteLine("student "+sname+"having third class");
break;
case false:
switch (avg >=50&&avg< 60)
{
case true:
Console.WriteLine("student"+sname+" having second
class");
break;
case false:
Console.WriteLine("student"+sname+" having first
class");
break;
}
break;
}
break;
}
Console.ReadLine();
}
}
}
OUTPUR
Task2:-Write a console program to accept user age and display the user status?
namespace user_age_status
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("enter u r age:");
int a = int.Parse(Console.ReadLine());
if (a >= 58)
{
Console.WriteLine("senior citizen:");
}
else if (a >= 25)
{
Console.WriteLine("working citizen");
}
else if (a >= 24)
{
Console.WriteLine("college student");
}
else if (a >= 4)
{
Console.WriteLine("school boy");
}
else if (a >= 1)
{
Console.WriteLine("playing kid");
}
else
{
Console.WriteLine("Invalid age");
}
Console.ReadLine();
}
}
}
OUTPUT:
Task3:-Write a console program to find the 2nd biggest number among 3 numbers?
namespace _2nd_biggest
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("enter first number:");
int a = int.Parse(Console.ReadLine());
Console.WriteLine("enter second number:");
int b = int.Parse(Console.ReadLine());
Console.WriteLine("enter third number:");
int c = int.Parse(Console.ReadLine());
if (a > b && a > c)
{
if(b>c)
{
Console.WriteLine(b+" b is second biggest number");
}
else
{
Console.WriteLine(c+" c is second biggest number");
}
}
if (b>c && b>a)
{
if (c > a)
{
Console.WriteLine(c + " c is second biggest number");
}
else
{
Console.WriteLine(a + " a is second biggest number");
}
}
if(c>a && c>b)
{
if (a > b)
{
Console.WriteLine(a + " a is second biggest number");
}
else
{
Console.WriteLine(b + " b is second biggest number");
}
}
if (a ==b&&b== c)
{
Console.WriteLine("a &b & c three values are same");
}
if(a==b&&b!=c)
{
Console.WriteLine("a & b two values are same");
}
else if(b == c&&c!=a)
{
Console.WriteLine("b & c two values are same");
}
else if (c == a&&a!=b)
{
Console.WriteLine("c & a two values are same");
}
Console.ReadLine();
}
}
}
OUTPUT:
Task4:-Write a console program to find the smallest number among 3 numbers?
namespace smallest_among_three
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("enter first number:");
int a = int.Parse(Console.ReadLine());
Console.WriteLine("enter second number:");
int b = int.Parse(Console.ReadLine());
Console.WriteLine("enter third number:");
int c = int.Parse(Console.ReadLine());
if (a > b && a > c)
{
if (b > c)
{
Console.WriteLine(c + " c is smallest number");
}
else
{
Console.WriteLine( b+ " bis smallest number");
}
}
if (b > c && b > a)
{
if (c > a)
{
Console.WriteLine(a + " a is smallest number");
}
else
{
Console.WriteLine(c + " c is smallest number");
}
}
if (c > a && c > b)
{
if (a > b)
{
Console.WriteLine(b + " b is smallest number");
}
else
{
Console.WriteLine(a + " a is smallest number");
}
}
if (a == b && b == c)
{
Console.WriteLine("a &b & c three values are same");
}
if (a == b && b != c)
{
Console.WriteLine("a & b two values are same");
}
else if (b == c && c != a)
{
Console.WriteLine("b & c two values are same");
}
else if (c == a && a != b)
{
Console.WriteLine("c & a two values are same");
}
Console.ReadLine();
}
}
}
OUTPUT:
Task5:-Write a console program accept a letter from the user?
Display it is vowel or consonant?
namespace aeiou1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("enter u r character:");
char ch = char.Parse(Console.ReadLine());
char vowel1='a',vowel2='e',vowel3='i',vowel4='o',vowel5='u';
if(ch==vowel1||ch==vowel2||ch==vowel3||ch==vowel4||ch==vowel5)
{
Console.WriteLine(ch+" is vowel");
}
else
{
Console.WriteLine(ch+" is consonant");
}
Console.ReadLine();
}
}
}
OUTPUT:
O/p
55555
4444
333
22
1
Sol:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace logic_numbers1
{
class Program
{
static void Main(string[] args)
{
for (int i = 5; i >= 1; i--)
{
for (int j = 1; j <= i; j++)
{
Console.Write(i + " ");
}
Console.WriteLine();
}
Console.ReadLine();
}
}
}
OUTPUT:
123456789
12345678
1234567
123456
12345
1234
123
12
1
namespace logic_number2
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("enter u r range:");
int n = int.Parse(Console.ReadLine());
for (int i = n; i >= 1; i--)
{
for (int j = 1; j <= i; j++)
{
Console.Write(j + " ");
}
Console.WriteLine();
}
Console.ReadLine();
}
}
}
OUTPUT:
Task 8:- Write a console program to display the numbers like below within the range?
O/p
15
1
23
456
7 8 9 10
11 12 13 14 15
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace logical_number3
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("enter u r range:");
int n = int.Parse(Console.ReadLine());
int a = 1;
for(int i=1;i<=5;i++)
{
for(int j=1;j<=i;j++)
{
if(a<=n)
Console.Write(a + " ");
a++;
}
Console.WriteLine();
}
Console.ReadLine();
}
}
}
OUTPUT:
Sol:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace biggest_number_by_use_array
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter four numbers");
int[] a = new int[4];
for (int i = 0; i < 4; i++)
{
a[i] = Convert.ToInt32(Console.ReadLine());
}
Array.Sort(a);
Console.WriteLine("Largest number is: " + a[a.Length - 1]);
Console.ReadLine();
}
}
}
OUTPUT:
namespace calculateelectric_bill
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("enter customer name:");
string cname = Console.ReadLine();
Console.WriteLine("enter customer units:");
int cunits = int.Parse(Console.ReadLine());
restart:
Console.WriteLine("enter customer type:" + "\n" + "enter 1 for Industry:" +
"\n" + "enter 2 per Residential:" + "\n" + "enter 3 for Agriculture:");
char ctype=char.Parse(Console.ReadLine());
switch(ctype)
{
case '1':
Console.WriteLine(cname+" Industry bill is:"+ cunits*8+"Rs");
break;
case '2':
Console.WriteLine(cname+" Residential bill is:"+ cunits*6+"Rs");
break;
case '3':
Console.WriteLine(cname+" Agricuture bill is:"+ cunits*2+"Rs");
break;
default:
Console.WriteLine("invalid input:");
goto restart;
}
Console.ReadLine();
}
}
}
Output: