Tutorial – 1
1. Write a program to print “Hello World”.
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
{
internal class Program
{
static void Main(string[] args)
{
Console.WriteLine("ABHAY SHUKLA");
Console.WriteLine("hello world!!");
Console.ReadLine();
}
}
}
Output:
2.Design your profile page as given below.
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
Name: Ramesh Tamkuity
DOB: 15/10/1991
Address: 4, xyx society, Kalawad Road
City: Rajkot
Pincode: 360 001
State: Gujarat
Country: India
Email:
[email protected] $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp2
{
internal class Program
{
static void Main(string[] args)
{
Console.WriteLine("ABHAY");
Console.WriteLine("Enter Your Name : ");
string name = Console.ReadLine();
Console.WriteLine("Enter Your DOB : ");
string DOB = (Console.ReadLine());
Console.WriteLine("Enter Your Adress : ");
string adress = Console.ReadLine();
Console.WriteLine("Enter Your Pincode : ");
int pincode = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter Your State : ");
string state = Console.ReadLine();
Console.WriteLine("Enter Your Country : ");
string country = Console.ReadLine();
Console.WriteLine("Enter Your Email Adress : ");
string email = Console.ReadLine();
Console.WriteLine("Name : " + name);
Console.WriteLine("DOB : " + DOB);
Console.WriteLine("Adress : " + adress);
Console.WriteLine("Pincode : " + pincode);
Console.WriteLine("State : " + state);
Console.WriteLine("Country : " + country);
Console.WriteLine("Email : " + email);
Console.ReadLine();
}
}
}
Output:
3.Find out whether the given number is odd or even
Code:
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp2
{
internal class Program
{
static void Main(string[] args)
{
Console.WriteLine("Abhay");
Console.WriteLine("Enter One Number : ");
int num =Convert.ToInt32(Console.ReadLine());
if (num%2 == 0 )
{
Console.WriteLine("Number is Even");
}
else
{
Console.WriteLine("Number is odd");
}
Console.ReadLine();
}
}
}
Output:
4. Rearrange the given code to correct the program. The resultant program will be to
input a number and print whether the given number is odd or even.
Code:
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp2
{
internal class Program
{
static void Main(string[] args)
{
Console.WriteLine("Abhay");
int num;
Console.WriteLine("Enter One Number : ");
num =Convert.ToInt32(Console.ReadLine());
if (num%2 == 0 )
{
Console.WriteLine("Number is Even");
}
else
{
Console.WriteLine("Number is odd");
}
Console.ReadLine();
}
}
}
Output:
5. Write output of the program. Also write comment for each line for the following
code.
Code:
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp2
{
internal class Program
{
static void Main(string[] args)
{
Console.WriteLine("Abhay");
int n, fact = 1;
Console.WriteLine("Enter Number : ");
string str = Console.ReadLine(); // taking input from user
n = Convert.ToInt32(str); // convert sring to intrger
for (int i = 1; i <= n; i++)
fact = fact * i; //claculate the fectorial of given number
}
Console.WriteLine("Factorial : {0}", fact); // giving output
Console.ReadLine();
}
}
}
Output:
6. Write missing statement to get the desired output.
Code:
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp2
{
internal class Program
{
static int Sum(int x, int y, int z)
{
Console.WriteLine("Abhay");
int res;
res = x + y + z;
return res;
}
static void Main(string[] args)
{
Console.Write("Enter Number 1: ");
int a = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter Number 2 : ");
int b = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter Number 3 : ");
int c = Convert.ToInt32(Console.ReadLine());
int result = Sum(a, b, c);
Console.WriteLine(result);
Console.Read();
Output:
7. Predict and write the output of the given code.
Code:
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp2
{
internal class Program
{
static void Main(string[] args)
{
Console.WriteLine("Abhay");
int num1, res, i;
Console.WriteLine("Enter a number");
num1 = Convert.ToInt32(Console.ReadLine());
i = 1; //Initialization
//Check whether condition matches or not
while (i <= 10)
{
res = num1 * i;
Console.WriteLine("{0} x {1} = {2}", num1, i, res);
i++; //Increment by one
}
Console.ReadLine();
}
}
}
Output:
8. Write a program to convert given name in upper characters.
Code:
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp2
{
internal class Program
{
static void Main(string[] args)
{
Console.WriteLine("Abhay");
Console.WriteLine("Enter One String : ");
string st = Console.ReadLine();
st = st.ToUpper();
Console.WriteLine(st);
Console.ReadLine();
}
Output:
9. Write a Program to convert given name in toggle case
Code:
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp2
{
internal class Program
{
static void Main(string[] args)
{
Console.WriteLine("Abhay");
Console.WriteLine("Enter One String : ");
string st = Console.ReadLine();
st = st.ToLower();
Console.WriteLine(st);
Console.ReadLine();
}
Output:
10. Write a Program which accepts mobile no as a string from the user and converts the
last 5 digits into X.
Code:
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp2
{
internal class Program
{
static void Main(string[] args)
{
Console.WriteLine("Abhay");
Console.WriteLine("Enter your mobile number : ");
string mobileNumber = Console.ReadLine();
int lastIndex = mobileNumber.Length - 5;
string lastFiveDigits = mobileNumber.Substring(lastIndex);
string newMobileNumber = mobileNumber.Replace(lastFiveDigits, "XXXXX");
Console.WriteLine(newMobileNumber);
Console.ReadLine();
}
}
}
Output:
11. Write a Program which accepts name and gender from the user. Here, gender may
have only 1 character, M or F.
Code:
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp2
{
internal class Program
{
static void Main(string[] args)
{
Console.WriteLine("Abhay");
Console.WriteLine("Enter your name : ");
string name = Console.ReadLine();
Console.WriteLine("Enter your gender (M/F) : ");
string gender = Console.ReadLine();
if (gender == "M")
{
Console.WriteLine("Mr. " + name);
}
else if (gender == "F")
{
Console.WriteLine("Ms. " + name);
}
else
{
Console.WriteLine("invelid choice");
}
Console.ReadLine();
}
}
}
Output:
12. Write a Program which accepts name from the user and prints the same
Code:
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp2
{
internal class Program
{
static void Main(string[] args)
{
Console.WriteLine("Abhay");
Console.WriteLine("Enter your name : ");
string name = Console.ReadLine();
Console.WriteLine(name);
Console.ReadLine();
}
}
Output:
13. Write a Program to prints the following series
0 1 1 2 3 5 8 13 21 34 55
Code:
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp2
{
internal class Program
{
static void Main(string[] args)
{
Console.WriteLine("Abhay");
int n1 = 0, n2 = 1, n3;
Console.Write("0,1");
for (int i = 2; i < 11; i++)
{
n3 = n1 + n2;
Console.Write(",{0}", n3);
n1 = n2;
n2 = n3;
}
Console.ReadLine();
}
}
}
Output:
14.Write a Program which accepts no from the user and print the same in words.
INPUT : 98732
OUTPUT: Nine Eight Seven Three Two
Code:
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp2
{
internal class Program
{
private static string[] digits = { "zero", "one", "two", "three", "four", "five",
"six", "seven", "eight", "nine" };
static void Main(string[] args)
{
Console.WriteLine("Abhay");
int number;
Console.WriteLine("Enter a number: ");
number = int.Parse(Console.ReadLine());
string words = "";
while (number > 0)
{
int digit = number % 10;
words = digits[digit] + " " + words;
number = number / 10;
}
Console.WriteLine("The number in words is: " + words);
Console.ReadLine();
}
}
}
Output:
15. Write a Program to check whether the given no is Armstrong no or not.
Code:
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp2
{
internal class Program
{
static void Main(string[] args)
{
Console.WriteLine("Abhay");
int number, temp, sum = 0;
Console.WriteLine("Enter a number: ");
number = int.Parse(Console.ReadLine());
temp = number;
while (temp > 0)
{
int digit = temp % 10;
sum += digit * digit * digit;
temp = temp / 10;
}
if (number == sum)
{
Console.WriteLine(number + " is an Armstrong number.");
}
else
{
Console.WriteLine(number + " is not an Armstrong number.");
}
Console.ReadLine();
}
}
}
Output:
16.Write a program to display a pattern like a right angle triangle using an asterisk
The pattern like :
*
**
***
****
Code:
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp2
{
internal class Program
{
static void Main(string[] args)
{
Console.WriteLine("Abhay");
int rows;
Console.WriteLine("Enter the number of rows: ");
rows = int.Parse(Console.ReadLine());
for (int i = 1; i <= rows; i++)
{
for (int j = 1; j <= i; j++)
{
Console.Write("*");
}
Console.WriteLine();
}
Console.ReadLine();
}
}
}
Output:
17. Write a Program to generate following output.
1
12
123
1234
Code:
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp2
{
internal class Program
{
static void Main(string[] args)
{
Console.WriteLine("Abhay");
int rows;
Console.WriteLine("Enter the number of rows: ");
rows = int.Parse(Console.ReadLine());
for (int i = 1; i <= rows; i++)
{
for (int j = 1; j <= i; j++)
{
Console.Write(j);
}
Console.WriteLine();
}
Console.ReadLine();
}
}
}
Output:
18. Write a program to make such a pattern like a right angle triangle with the number
increased by 1.
1
23
456
7 8 9 10
Code:
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp2
{
internal class Program
{
static void Main(string[] args)
{
Console.WriteLine("Abhay");
Console.WriteLine("Enter the number of row : ");
int rows = int.Parse(Console.ReadLine());
int number = 1;
for (int i = 1; i <= rows; i++)
{
for (int j = 1; j <= i; j++)
{
Console.Write("{0} ", number++);
}
Console.WriteLine();
}
Console.ReadLine();
}
}
}
Output:
19. Write a program to make such a pattern as a pyramid with an asterisk.
*
**
***
****
Code:
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp2
{
internal class Program
{
static void Main(string[] args)
{
Console.WriteLine("Abhay");
Console.WriteLine("Enter the number of row : ");
int rows = int.Parse(Console.ReadLine());
for (int i = 1; i <= rows; i++)
{
for (int j = rows - i; j >= 1; j--)
{
Console.Write(" ");
}
for (int j = 1; j <= 2 * i - 1; j++)
{
Console.Write("*");
}
Console.WriteLine();
}
Console.ReadLine();
}
}
}
Output:
20. Write a program to make a pyramid pattern with numbers increased by 1.
1
23
456
7 8 9 10
Code:
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp2
{
internal class Program
{
static void Main(string[] args)
{
Console.WriteLine("Abhay");
Console.WriteLine("Enter the number of row : ");
int rows = int.Parse(Console.ReadLine());
for (int i = 1; i <= rows; i++)
{
for (int j = rows - i; j >= 1; j--)
{
Console.Write(" ");
}
for (int j = 1; j <= 2 * i - 1; j++)
{
Console.Write(j);
}
Console.WriteLine();
}
Console.ReadLine();
}
}
}
Output:
21. Write a program to find the sum of the series 5 +55 + 555 + 5555 + .. n terms.
Test Data :
Input the number of terms : 4
Input number : 5
Expected Output :
5 + 55 + 555 + 5555
The Sum is : 6170
Code:
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp2
{
internal class Program
{
static void Main(string[] args)
Console.WriteLine("Abhay");
Console.Write("enter the digit : ");
int n =Convert.ToInt32(Console.ReadLine());
Console.WriteLine();
int m = n;
int sum = 0;
for (int i = 0; i < n; i++)
{
Console.Write(m+" + ");
sum=sum+m;
m = m * 10 + n;
}
Console.WriteLine();
Console.WriteLine("the sum = " + sum);
Console.ReadLine();
}
}
}
22. Write a program to display a pattern like a diamond.
*
***
*****
*******
*********
*******
*****
***
*
Code:
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp2
{
internal class Program
{
static void Main(string[] args)
{
Console.WriteLine("abhay");
Console.WriteLine("Enter number of row : ");
int rows = int.Parse(Console.ReadLine());
Console.WriteLine();
for (int i = 1; i <= rows; i++)
{
for (int j = rows - i; j >= 1; j--)
{
Console.Write(" ");
}
for (int j = 1; j <= 2 * i - 1; j++)
{
Console.Write("*");
}
Console.WriteLine();
}
for (int i = rows - 1; i >= 1; i--)
{
for (int j = rows - i; j >= 1; j--)
{
Console.Write(" ");
}
for (int j = 1; j <= 2 * i - 1; j++)
{
Console.Write("*");
}
Console.WriteLine();
}
Console.ReadLine();
}
}
}
Output: