0% found this document useful (0 votes)
31 views10 pages

Lab Sheet For Week 2 and 3

The document provides examples of C# code snippets and questions to test understanding of basic C# programming concepts like variables, data types, conditional statements, functions and methods. The answers given are short C# programs that demonstrate solving the problems by applying the relevant C# features.

Uploaded by

Memes World
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views10 pages

Lab Sheet For Week 2 and 3

The document provides examples of C# code snippets and questions to test understanding of basic C# programming concepts like variables, data types, conditional statements, functions and methods. The answers given are short C# programs that demonstrate solving the problems by applying the relevant C# features.

Uploaded by

Memes World
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

‭ .

For each of the data item given below, choose an appropriate data type and write a C#‬
1
‭statement to declare the variable‬
‭• Variable myAge to store your own age‬
‭• Variable income to keep track of Arun’s personal income‬
‭• Variable temp_c to store temperature in degree Celsius‬
‭• Variable temp_k to store temperature in Kelvin‬
‭• Variable name to store Aum’s full name‬

‭Ans.‬

‭int myAge;‬

‭double income;‬

‭double temp_c;‬

‭double temp_k;‬

‭string name;‬

‭ . Let x, y and z be of type int and ch of type char. Describe the condition that makes each of‬
2
‭the following Boolean expressions true‬

•‭ x > 2 true when x is greater than 2‬


‭• x%2 == 0 true when x is an even number‬
‭• (x%5 == 0) • (x%y == 0) • ((x%y == 0) && (z%y == 0))‬
‭• ch == ’a’ • ((ch >= ’a’) && (ch <= ’z’))‬
‭• ((ch >= ’A’) && (ch <= ’Z’))‬
‭• ((ch >= ’0’) && (ch <= ’9’)) true if ch is a character between ‘0’ and ’9’‬
‭• (ch != ’*’) • !(ch == ’*’)‬

‭Ans.‬

‭ .‬
1 ‭ > 2: True when x is greater than 2.‬
x
‭2.‬ ‭x % 2 == 0: True when x is an even number.‬
‭3.‬ ‭(x % 5 == 0): True when x is divisible by 5.‬
‭4.‬ ‭(x % y == 0): True when x is divisible by y.‬
‭5.‬ ‭((x % y == 0) && (z % y == 0)): True when both x and z are divisible by y.‬
‭6.‬ ‭ch == 'a': True when ch is equal to the character 'a'.‬
‭7.‬ ‭((ch >= 'a') && (ch <= 'z')): True when ch is a lowercase letter.‬
‭8.‬ ‭((ch >= 'A') && (ch <= 'Z')): True when ch is an uppercase letter.‬
‭9.‬ ‭((ch >= '0') && (ch <= '9')): True if ch is a character between '0' and '9'.‬
‭ 0.‬‭(ch != '*'): True when ch is not equal to '*'.‬
1
‭11.‬‭!(ch == '*'): True when ch is not equal to '*'.‬

‭ . Run the code snippet given below and fill the answers of the question using System ; class‬
3
‭Test { static void Main () { double x = 3.0 , y = 2.0; int a = 10 , b = 2; -------------------------(fill this‬
‭line with code to be executed mentioned below) Console.ReadLine (); } } Console.WriteLine(a);‬
‭--------- 10 Console.WriteLine(x+a); Console.WriteLine(a/b); Console.WriteLine(y/x);‬
‭Console.WriteLine(y%x); Console.WriteLine((a+b)/b%a); Console.WriteLine(9.0/5.0*(a-x));‬
‭Console.WriteLine(x+y-x*y%x); Console.WriteLine(57%50/25);‬

‭Ans.‬

‭using System;‬

‭class Lab1 {‬
‭static void Main() {‬
‭double x = 3.0, y = 2.0;‬
‭int a = 10, b = 2;‬

‭ onsole.WriteLine(a);
C // Output: 10‬
‭Console.WriteLine(x + a); // Output: 13‬
‭Console.WriteLine(a / b); // Output: 5‬
‭Console.WriteLine(y / x); // Output: 0.6666666666666666‬
‭Console.WriteLine(y % x); // Output: 2‬
‭Console.WriteLine((a + b) / b % a); // Output: 1‬
‭Console.WriteLine(9.0 / 5.0 * (a - x)); // Output: -9.0‬
‭Console.WriteLine(x + y - x * y % x); // Output: 6‬
‭Console.WriteLine(57 % 50 / 25); // Output: 0‬

‭Console.ReadLine();‬
‭}‬
‭}‬

‭ . Using if else construct write C# program to check whether a number is‬


4
‭• even or odd‬
‭• positive or negative‬
‭• multiple of 6 or not‬
‭• multiple of 100 or not‬

‭Ans.‬
‭using System;‬

‭class NumberCheck {‬
‭static void Main() {‬

‭ onsole.Write("Enter a number: ");‬


C
‭int number = int.Parse(Console.ReadLine());‬

‭if (number % 2 == 0) {‬
‭Console.WriteLine($"{number} is even.");‬
‭} else {‬
‭Console.WriteLine($"{number} is odd.");‬
‭}‬

‭if (number > 0) {‬


‭Console.WriteLine($"{number} is positive.");‬
‭} else if (number < 0) {‬
‭Console.WriteLine($"{number} is negative.");‬
‭} else {‬
‭Console.WriteLine($"{number} is zero.");‬
‭}‬

‭if (number % 6 == 0) {‬
‭Console.WriteLine($"{number} is a multiple of 6.");‬
‭} else {‬
‭Console.WriteLine($"{number} is not a multiple of 6.");‬
‭}‬

‭if (number % 100 == 0) {‬


‭Console.WriteLine($"{number} is a multiple of 100.");‬
‭} else {‬
‭Console.WriteLine($"{number} is not a multiple of 100.");‬
‭}‬
‭}‬
‭}‬

‭ . Write a C# program which attempts to identify the quadrant of the input (x, y) coordinates. If‬
5
‭the input coordinates happen to be on either X-axis or Y-axis, the program will display “I don’t‬
‭know.” Sample Output Please input X: -50 Please input Y: 10 (-50, 10) is in Q2 To take the user‬
i‭nput int i = int.Parse(Console.ReadLine()); // reading an integer data float f =‬
‭float.Parse(Console.ReadLine());// reading a floating point data‬

‭Ans.‬

‭using System;‬

‭ amespace QuadrantIdentifier‬
n
‭{‬
‭class Program‬
‭{‬
‭static void Main(string[] args)‬
‭{‬
‭int x, y;‬

‭ onsole.Write("Please input X: ");‬


C
‭x = int.Parse(Console.ReadLine());‬

‭ onsole.Write("Please input Y: ");‬


C
‭y = int.Parse(Console.ReadLine());‬

i‭f (x > 0 && y > 0)‬


‭{‬
‭Console.WriteLine("({0}, {1}) is in Q1", x, y);‬
‭}‬
‭else if (x < 0 && y > 0)‬
‭{‬
‭Console.WriteLine("({0}, {1}) is in Q2", x, y);‬
‭}‬
‭else if (x < 0 && y < 0)‬
‭{‬
‭Console.WriteLine("({0}, {1}) is in Q3", x, y);‬
‭}‬
‭else if (x > 0 && y < 0)‬
‭{‬
‭Console.WriteLine("({0}, {1}) is in Q4", x, y);‬
‭}‬
‭else if (x == 0 || y == 0)‬
‭{‬
‭Console.WriteLine("I don't know.");‬
‭}‬
‭}‬
‭}‬
‭}‬
‭ . A cellular phone company has a promotion plan for its customers. The air time fee is‬
6
‭calculated as‬
‭follows‬
‭• Each of the first two minutes costs 5 paise (per minute)‬
‭• Each of the remaining minutes costs 2 paise (per minute)‬
‭Take the number of minutes from the user, and computes the total air time fee.‬
‭Sample output‬
‭Enter the number of minutes: 1‬
‭The air time fee is 5 paise.‬
‭Enter the number of minutes: 5‬
‭The air time fee is 16 paise.‬

‭Ans.‬

‭using System;‬

‭class AirTimeFeeCalculator {‬
‭static void Main() {‬

‭ onsole.Write("Enter the number of minutes: ");‬


C
‭int numberOfMinutes = int.Parse(Console.ReadLine());‬

‭int airTimeFee;‬

‭if (numberOfMinutes <= 2) {‬


‭// Each of the first two minutes costs 5 paise‬
‭airTimeFee = numberOfMinutes * 5;‬
‭} else {‬

‭airTimeFee = 2 * 5 + (numberOfMinutes - 2) * 2;‬


‭}‬

‭Console.WriteLine($"The air time fee is {airTimeFee} paise.");‬


‭}‬
‭}‬

‭ . Write a BMI calculator program which will find your body status. Use the following rule‬
7
‭BMI = Weight(in kg)/ height*height (in meter)‬
‭ ample output‬
S
‭Enter your weight: 65‬
‭Enter your height: 1.75‬
‭Your BMI is 21.22.‬
‭You are normal.‬

‭Ans.‬

‭using System;‬

‭ lass BMICalculator‬
c
‭{‬
‭static void Main(string[] args)‬
‭{‬
‭double weight, height, bmi;‬

‭ onsole.Write("Enter your weight (in kg): ");‬


C
‭weight = double.Parse(Console.ReadLine());‬

‭ onsole.Write("Enter your height (in meters): ");‬


C
‭height = double.Parse(Console.ReadLine());‬

‭bmi = weight / (height * height);‬

‭Console.WriteLine("Your BMI is: {0:0.00}", bmi);‬

/‭/ Interpret BMI and display body status‬


‭if (bmi < 18.5)‬
‭{‬
‭Console.WriteLine("You are underweight.");‬
‭}‬
‭else if (bmi < 25)‬
‭{‬
‭Console.WriteLine("You are normal.");‬
‭}‬
‭else if (bmi < 30)‬
‭{‬
‭Console.WriteLine("You are overweight.");‬
‭}‬
‭else‬
‭{‬
‭Console.WriteLine("You are obese.");‬
‭}‬
‭}‬
‭}‬

‭ . Write a C# program to determine whether the input number is an integer. (Hint: Use the‬
8
‭method Math.Round())‬

‭Ans.‬

‭using System;‬

‭class IntegerCheck {‬
‭static void Main() {‬

‭ onsole.Write("Enter a number: ");‬


C
‭double inputNumber = double.Parse(Console.ReadLine());‬

‭if (Math.Round(inputNumber) == inputNumber) {‬


‭Console.WriteLine($"{inputNumber} is an integer.");‬
‭} else {‬
‭Console.WriteLine($"{inputNumber} is not an integer.");‬
‭}‬
‭}‬
‭}‬

‭ . Write a C# program using switch case to translate a letter to a number according to a‬


9
‭given mapping table (from a cell-phone’s dial pad).‬

‭ ample Output‬
S
‭Please input a letter: A‬
‭The corresponding number of A is 2.‬
‭Please input a letter: *‬
‭There is no corresponding number for *.‬

‭Ans.‬

‭using System;‬

‭ lass LetterToNumberTranslator‬
c
‭{‬
‭static void Main(string[] args)‬
‭{‬
‭char letter;‬

‭ onsole.Write("Please input a letter: ");‬


C
‭letter = char.ToUpper(Console.ReadLine()[0]);‬

‭ witch (letter)‬
s
‭{‬
‭case 'A':‬
‭case 'B':‬
‭case 'C':‬
‭Console.WriteLine("The corresponding number is 2.");‬
‭break;‬
‭case 'D':‬
‭case 'E':‬
‭case 'F':‬
‭Console.WriteLine("The corresponding number is 3.");‬
‭break;‬
‭case 'G':‬
‭case 'H':‬
‭case 'I':‬
‭Console.WriteLine("The corresponding number is 4.");‬
‭break;‬
‭case 'J':‬
‭case 'K':‬
‭case 'L':‬
‭Console.WriteLine("The corresponding number is 5.");‬
‭break;‬
‭case 'M':‬
‭case 'N':‬
‭case 'O':‬
‭Console.WriteLine("The corresponding number is 6.");‬
‭break;‬
‭case 'P':‬
‭case 'Q':‬
‭case 'R':‬
‭case 'S':‬
‭Console.WriteLine("The corresponding number is 7.");‬
‭break;‬
‭case 'T':‬
‭case 'U':‬
‭case 'V':‬
‭Console.WriteLine("The corresponding number is 8.");‬
‭break;‬
‭ ase 'W':‬
c
‭case 'X':‬
‭case 'Y':‬
‭case 'Z':‬
‭Console.WriteLine("The corresponding number is 9.");‬
‭break;‬
‭default:‬
‭Console.WriteLine("There is no corresponding number for {0}.", letter);‬
‭break;‬
‭}‬
‭}‬
‭}‬

‭Q10.‬

‭Ans.‬

‭using System;‬

‭ lass BookShippingCostCalculator‬
c
‭{‬
‭static void Main(string[] args)‬
‭{‬
‭string shippingType;‬
‭double weightInGrams, cost;‬

‭ onsole.Write("Enter the shipping type (Regular or Express): ");‬


C
‭shippingType = Console.ReadLine().ToUpper();‬
‭Console.Write("Enter the package weight in grams: ");‬
‭weightInGrams = double.Parse(Console.ReadLine());‬

i‭f (shippingType == "REGULAR")‬


‭{‬
‭if (weightInGrams <= 2000)‬
‭{‬
‭cost = weightInGrams * 0.50;‬
‭}‬
‭else‬
‭{‬
‭cost = 1000 + (weightInGrams - 2000) * 0.75;‬
‭}‬
}‭ ‬
‭else if (shippingType == "EXPRESS")‬
‭{‬
‭cost = CalculateRegularCost(weightInGrams) + 50;‬
‭}‬
‭else‬
‭{‬
‭Console.WriteLine("Invalid shipping type.");‬
‭return;‬
‭}‬

i‭nt rupees = (int)cost;‬


‭int paise = (int)((cost - rupees) * 100);‬

‭Console.WriteLine("The shipping cost is {0} rupees and {1} paise.", rupees, paise);‬
‭}‬

‭ tatic double CalculateRegularCost(double weightInGrams)‬


s
‭{‬
‭if (weightInGrams <= 2000)‬
‭{‬
‭return weightInGrams * 0.50;‬
‭}‬
‭else‬
‭{‬
‭return 1000 + (weightInGrams - 2000) * 0.75;‬
‭}‬
‭}‬
‭}‬

You might also like