C#ass 2
C#ass 2
C#ass 2
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Cquestion1
{
class Program
{
static void Main(string[] args)
{
//accepting two numbers
Console.Write("\nENter first number: ");
int a = int.Parse(Console.ReadLine());
Console.Write("\nEnter second number: ");
int b = int.Parse(Console.ReadLine());
bool result1=true;
bool result2 = false;
//checking if one of the numbers is equal to 50 or if both of them add up to 50
if (a == 50 || a + b == 50 || b == 50)
{
Console.WriteLine("\n"+result1);
}
else
{
Console.WriteLine("\n"+result2);
}
Console.ReadKey();
}
}
}
Question 2
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Cquestion2
{
class Program
{
static void Main(string[] args)
{
//accepting name to remove one character
Console.Write("\nEnter the string: ");
string name = Console.ReadLine();
//accepting the index of a character to remove
Console.Write("\nEnter index of the character you want to remove: ");
int num = int.Parse(Console.ReadLine());
//removing the character
string new_string = name.Remove(num,1);
Console.WriteLine("\nThe new string is "+new_string);
Console.ReadKey();
}
}
}
Question 3
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Cquestion3final
{
class Program
{
static void Main(string[] args)
{
//accepting the string
Console.Write("\nEnter the string: ");
string name = Console.ReadLine();
//checking if the string starts with c#
if (name.StartsWith("C#") || name.StartsWith("c#"))
{
Console.WriteLine(name);
}
else
{
//adding C# at the end of the string if it doesn't start with C#
Console.WriteLine("\nNew name is "+name+"C#");
}
Console.ReadKey();
}
}
}
Question 4
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Cquestion4
{
class Program
{
static void Main(string[] args)
{
//accepting three numbers
Console.WriteLine("\nEnter three numbers:");
int a = int.Parse(Console.ReadLine());
int b = int.Parse(Console.ReadLine());
int c = int.Parse(Console.ReadLine());
//if statement to check the greatest number
if (a > b && a > c)
{
Console.WriteLine("\n"+a+" is the biggest.");
}
else
if (b > a && b > c)
{
Console.WriteLine("\n"+b+" is the biggest.");
}
else
{
Console.WriteLine("\n"+c+" is the biggest.");
}
Console.ReadKey();
}
}
}
Question 5
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Cquestion5
{
class Program
{
Console.WriteLine("\n\nFinal attempt.");
Console.Write("\nUser_ID:");
int c = int.Parse(Console.ReadLine());
Console.Write("\nPassword:");
string d = Console.ReadLine();
string new_d = password.ToLower();
if (c != id || d != passwrd||d!=new_d)
{
Console.WriteLine("\n\nLogin rejected!!");
}
else
{
Console.WriteLine("\nYou loged in!!!!");
}
}
else
{
Console.WriteLine("\nYou loged in!!!");
}
}
else
{
Console.WriteLine("\nYou loged in!!!!");
}
Console.ReadKey();
}
}
}
Question 6
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Cquestion6final
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("enter a character");
string charr = Console.ReadLine();
switch (charr)
{
//cases for vowels
case "a":
case "e":
case "i":
case "o":
case "u":
charr.ToLower();
Console.WriteLine("The character is vowel");
break;
//default for other characters
default:
Console.WriteLine("It's other character");
break;
}
Console.ReadKey();
}
}
}
Question 7
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace cquestion7
{
class Program
{
static void Main(string[] args)
{
//creating an array
string[] arr = { "John", "James", "Peter", "Joseph" };
//displaying elements of the array
Console.WriteLine("The array elements are as follows: ");
foreach (string i in arr)
{
Console.Write(" "+i);
}
Console.ReadKey();
}
}
}
Question 8
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace cquestion8
{
class Program
{
static void Main(string[] args)
{
//creating an array
int[] arr1 = { 2, 6, 9, 67, 1, 3 };
int sum=0;
//calculating sum of the array elements
foreach (int i in arr1)
{
sum += i;
}
//displaying sum of the elements
Console.WriteLine("The sum of array elements is: "+sum+".");
Console.ReadKey();
}
}
}
Question 9
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CQuestion9
{
class Program
{
static void Main(string[] args)
{
int[] first_array = { 2, 3, 4, 5, 6, 7, 8, 9, 0 };
int[] second_array = new int[first_array.Length];
//copying first array elemens into second array elements
Array.Copy(first_array, second_array, first_array.Length);
//displaying second array elements coppied from first array
Console.WriteLine("The elements copied from first array to second array are
as follows:");
foreach (int i in second_array)
{
Console.WriteLine(i);
}
Console.ReadLine();
}
}
}
Question 10
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace cquestion10
{
class Program
{
//creating a function
static void Mymethody()
{
//accepting student details
Console.Write("\nEnter name: ");
string name = Console.ReadLine();
Console.Write("\nEnter surname: ");
string surname = Console.ReadLine();
Console.Write("\nEnter student number: ");
string stud_num = Console.ReadLine();
Console.WriteLine("\nNAME\t\tSURNAME\t\tSTUDENT_NUMBER\n"+name+"\t\
t"+surname+"\t\t"+stud_num);
}
static void Main(string[] args)
{
//calling a function
Mymethody();
Console.ReadKey();
}
}
}
Question 11
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace cquestion11
{
class Program
{
//creating function for addition
static int sum(int a,int b)
{
return a + b;
}
static void Main(string[] args)
{
Console.Write("Enter first number: ");
int c = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter second number: ");
int d = Convert.ToInt32(Console.ReadLine());
//displaying the sum and adding accepted numbers to the function "sum"
Console.WriteLine("\nThe sum of " + c + " and " + d + " is " + sum(c, d)+".");
Console.ReadKey();
}
}
}
Question 12
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace cquestion12
{
class Program
{
static void Main(string[] args)
{
//creating an array
int[] arrNum = { 9, 1, 3, 5, 8, 103, 13, 56, 90, 0, 100 };
//sorting an array
Array.Sort(arrNum);
//displaying sorted array ellements
Console.WriteLine("Sorted array elements are: ");
foreach (int i in arrNum)
{
Console.WriteLine(" "+i);
}
Console.ReadKey();
}
}
}
Question 13
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace cquestion13
{
class Program
{
static void Main(string[] args)
{
Console.Write("Enter the number to display in the triangle: ");
int a = int.Parse(Console.ReadLine());
//accepting the width of the triangle
Console.Write("Enter the width of the triangle: ");
int b = int.Parse(Console.ReadLine());
for (int i = b; i >=1;i--)