0% found this document useful (0 votes)
30 views14 pages

C#ass 2

The document contains code snippets from 11 coding questions/exercises. Each snippet defines a C# class with a Main method that accepts user input, performs some operation (e.g. checking conditions, manipulating strings/arrays), and outputs results. The operations include checking if numbers satisfy conditions, removing characters from strings, comparing strings, finding the greatest number, validating login, identifying vowels, displaying array elements, copying arrays, and defining a method to accept and output student details.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views14 pages

C#ass 2

The document contains code snippets from 11 coding questions/exercises. Each snippet defines a C# class with a Main method that accepts user input, performs some operation (e.g. checking conditions, manipulating strings/arrays), and outputs results. The operations include checking if numbers satisfy conditions, removing characters from strings, comparing strings, finding the greatest number, validating login, identifying vowels, displaying array elements, copying arrays, and defining a method to accept and output student details.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

Question 1

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
{

static void Main(string[] args)


{
//assigning variables
int id = 123;
string passwrd = "pass";

//accepting user id and password


Console.Write("Enter user id: ");
int user_id = int.Parse(Console.ReadLine());
Console.Write("Enter password: ");
string password = Console.ReadLine();
string new_pass = password.ToLower();

//checking if the user id and password are correct


if (user_id != id || password != passwrd||password!=new_pass)
{
Console.WriteLine("Password or User ID is wrong, Please try again");
Console.Write("User_ID:");
int a = int.Parse(Console.ReadLine());
Console.Write("Password:");
string b = Console.ReadLine();
string new_b = password.ToLower();
if (a != id ||b!=passwrd || b != new_b)
{

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--)

for (int j = 1; j <= 1*i; j++)


{
//displaying a triangle upside down
Console.Write(a);
}
Console.WriteLine();
}
Console.ReadKey();
}
}
}

You might also like