trial
C# PRACTICALS
Wednesday, 12 July 2023
Saturday, 4 June 2016
WELCOME TO SPACE COMPUTER INSTITUTE'S
C# PRACTICALS
PRACTICAL NO.1----->
Reading strings from the keyboard
using System;
class Prog3_1
{
public static void Main()
{
Console.Write ("Enter Your First Name : "); // Displaying to write first name
string name1 = Console.ReadLine (); // Saving first name in name1
Console.Write ("Enter Your Last Name : "); // Displaying to write last name
string name2 = Console.ReadLine (); // Saving first name in name2
Console.WriteLine ("Hello Mr." + name1 +" " + name2); // Displaying both first & last names
Console.ReadLine (); // Since to stop the console for displaying last line, we use this to accept a
keystroke frm user. (Similar to getch() in C)
}
}
OUTPUT
Enter Your First Name:Space
Enter Your Last Name: Computer
Hello Mr. Space Computer
_______________________________________________________________________________
PRACTICAL NO.2----->
Assigning values to variables
using System;
class SampleMath
{
public static void Main()
{
double x = 2.0; // declaring a variable named x of type double & assigning it value 2.0
double y = 3.0; // declaring a variable named y of type double & assigning it value 3.0
double z ; // declaring a variable named z of type double
z = x + y;
Console.WriteLine("x = " + x + ", y = " + y + " & z = " + z);
Console.ReadLine();
}
}
OUTPUT
X = 2.0, Y = 3.0, Z = 5.0
______________________________________________________________________________
PRACTICAL NO.3----->
Diamond pattern on the console screen
using A = System.Console;
class Pattern
{
public static void Main()
{
A.WriteLine (" X ");
A.WriteLine (" XXX ");
A.WriteLine ("XXXXX");
A.WriteLine (" XXX ");
A.WriteLine (" X ");
A.ReadLine ();
}
}
OUTPUT
X
XX
XXX
XX
X
_______________________________________________________________________________
PRACTICAL NO.4----->
Declaration & Additions of variables
using System;
class DeclareAndDisplay
{
public static void main()
{
float x; // Declaring x of float type
float y; // Declaring y of float type
int m; // Declaring m of integer type
x = 75.86F;
y = 43.48F;
m = x + y; // This line will create an ERROR. Reason given below.
Console.WriteLine("m = x + y = 75.86 + 43.48 = " +m);
}
}
//*********************** Comment on the output *****************
//We declared 2 float type variables.
//Added them
//Saved the result in an Integer variable
//Since the result of addition of 2 float numbers is a float only ...
//We cannot save that value in an integer variable.
//C# has strict check for data conversions taking place.
//It does not automatically converts a larger data type to smaller one since it will create a loss of data.
//For this purpose, we need to explicitly make the integer variable 'm' to float type.
//If 'm' is also a float variable, then the output would have been like this ...
//m = x + y = 75.86 + 43.48 = 119.34
_____________________________________________________________________________
PRACTICAL NO.5----->
Demonstrating Boxing & Unboxing
using System;
class Boxing
{
public static void main(string[] a)
{
// ************************ BOXING **************************
int m = 10;
object om = m; // creates a box to hold m
m = 20;
Console.WriteLine("************************* BOXING ********************");
Console.WriteLine("m = " + m); // m = 20
Console.WriteLine("om = " +om);// om = 10
Console.ReadLine();
// *********************** UNBOXING ***********************
int n = 10;
object on = n; // box n (creates a box to hold n)
int x = (int)on; // unbox on back to an int
Console.WriteLine("************************* UNBOXING ********************");
Console.WriteLine("n = " + n); // n = 20
Console.WriteLine("on = " +on);// on = 10
Console.ReadLine();
}
}
_____________________________________________________________________________
PRACTICAL NO.6----->
Demonstrating addition of byte type variables
using System;
class addition
{
public static void Main()
{
byte b1;
byte b2;
int b3; // We are required to declare b3 as byte BUT its declared as int. The reason is given below.
b1 = 100;
b2 = 200;
// Normally this is the addition statement
// b3 = b1 + b2;
// However it gives an error that cannot convert 'int' to 'byte'.
// When b2 & b3 are added, we get an integer value which cannot be stored in byte b1
// Thus we will declare b3 as integer type & explicitly convert b2 & b3 to int.
b3 = (int)b1 + (int)b2;
Console.WriteLine("b1 = " + b1);
Console.WriteLine("b2 = " + b2);
Console.WriteLine("b3 = " + b3);
Console.ReadLine();
}
}
OUTPUT
b1 = 100
b2 = 200
b3 = 300
_______________________________________________________________________________
PRACTICAL NO.7----->
Implementing some custom console output
using System;
class Demo
{
public static void Main()
{
Console.WriteLine("Hello, \"Ram\"!");
// Output ‐‐‐> Hello, "Ram"!
// Reason ‐‐> Due to the \" character, the characters Ram is in double quotes
Console.WriteLine("*\n**\n***\n****\n");
//Reason ‐‐> Due to the \n character, we get each set of * in a new line.
Console.ReadLine();
}
}
OUTPUT
Hello, “Ram” !
*
**
***
________________________________________________________________________________
PRACTICAL NO.8----->
Printing a home like figure in the console
using System;
class Home
{
public static void Main()
{
Console.WriteLine(" / \\ ");
Console.WriteLine(" / \\ ");
Console.WriteLine(" / \\ ");
Console.WriteLine(" ‐‐‐‐‐‐‐‐‐ ");
Console.WriteLine(" \" \" ");
Console.WriteLine(" \" \" ");
Console.WriteLine(" \" \" ");
Console.WriteLine("\n\n This is My Home.");
Console.ReadLine();
}
}
OUTPUT
/ \
/ \
/ \
‐‐‐‐‐‐‐‐‐‐‐‐‐
“ ”
” ”
” ”
_______________________________________________________________________________
PRACTICAL NO.9----->
Computation of Integer Values taken from console
using System;
class integerdemo
{
public static void Main()
{
string s1,s2;
int a,b;
Console.Write("Enter no 1 # "); // Display to enter no. 1
s1 = Console.ReadLine (); // save the number in a string variable s1
a = int.Parse (s1); // the string s1 is converted into int type variable
Console.Write("Enter no 2 # "); //Display to enter no. 2
s2 = Console.ReadLine (); // save the number in a string variable s2
b = int.Parse (s2); // the string s2 is cinverted into int type variable
// Here er converted both the string variables to int because we wanted to do
// integer / numeric manipulation with the inputted string variables
Console.WriteLine(""); // Blank line
Console.WriteLine("********************* Integer manipulations
**********************");
Console.WriteLine(""); // Blank line
// Integer manipulations
Console.WriteLine("No1 + No2 = " + (a+b));
Console.WriteLine("No1 - No2 = " + (a-b));
Console.WriteLine("No1 / No2 = " + (a/b));
Console.WriteLine("No1 * No2 = " + (a*b));
Console.WriteLine("No1 % No2 = " + (a%b));
Console.ReadLine();
}
}
Output:
Enter no 1 # 25
Enter no 2 # 15
********************* Integer manipulations **********************
No1 + No2 = 40
No1 - No2 = 10
No1 / No2 = 1
No1 * No2 =375
No1 % No2 = 10
____________________________________________________________________________
PRACTICAL NO.10----->
Average of 3 numbers
using System;
class average
{
public static void Main()
{
float a = 25;
float b = 75;
float c = 100;
float avg = (a+b+c)/3;
Console.WriteLine("The average of 25, 75 & 100 = " + avg);
Console.ReadLine();
}
}
Output:
The average of 25, 75 & 100 = 6.6666666
_________________________________________________________________________________
PRACTICAL NO.11----->
Finding circumference & area of a circle
using System;
class circle
{
public static void Main()
{
float radius = 12.5F;
float circumfrence, area;
float pi = 3.1487F;
circumfrence = 2 * pi * radius;
area = pi * radius * radius;
Console.WriteLine("The Radius of the circle = " + radius);
Console.WriteLine(""); //Blank Line
Console.WriteLine("Its Circumfrence = " + circumfrence);
Console.WriteLine("Its Area = " + area);
Console.ReadLine();
}
}
Output:
The Radius of the circle = 12.5
Its Circumference = 78.7175
Its area = 491.9844
_______________________________________________________________________________
PRACTICAL NO.12----->
Converting Rs. To Paisa
using System;
class Money
{
public static void Main()
{
float RsF;
string s;
Console.Write("Enter the amount in Rs. : ");
s = Console.ReadLine();
RsF = float.Parse(s);
Console.WriteLine("Amount in paise = " +(RsF*100));
Console.ReadLine();
}
}
Output:
Enter the amount in Rs. : 15
Amount in paise = 1500
______________________________________________________________________________
PRACTICAL NO.13----->
Adding odd & even nos from 0 – 20 & adding nos. divisible by 7 between 100 – 200
using System;
class SumOfOdds
{
public static void Main()
{
int x=0, sumodd=0, sumeven=0, sumdiv7 = 0 ,totalno7 = 0, i;
// here ...
// "sumodd" will contain sum of all odd the numbers from 1 - 20
// "sumeven" will contain sum of all even the numbers from 1 - 20
// "sumdiv7" will contain the sum of all numbers from 100 - 200 divisible by 7
// "totalno7" will contain the total no. of all numbers from 100 - 200
divisible by 7
// "i" is a variable used in loops
// "x" is a temporary variable which check for the conditions imposed on it
// checking for the odd & even numbers
for (i=0 ; i<=20 ; i++)
{
x = i % 2;
if (x != 0)
{
sumodd = sumodd + i;
}
if (x == 0)
{
sumeven = sumeven + i;
}
}
//checking for the sum & no. of numbers divisible by 7
x = 0; // resetting the value of 'x'
for (i=100; i<=200;i++)
{
x = i % 7;
if (x == 0)
{
sumdiv7 = sumdiv7 + i;
totalno7 = totalno7 + 1;
}
}
Console.WriteLine("Sum of all odd numbers from 1 - 20 = " + sumodd + "\n");
Console.WriteLine("Sum of all even numbers from 1 - 20 = " + sumeven + "\n");
Console.WriteLine("Sum of all numbers from 100 - 200, divisible by 7 = " +
sumdiv7 + "\n");
Console.WriteLine("Total numbers from 100 - 200, divisible by 7 = " +
totalno7 + "\n");
Console.ReadLine();
}
}
Output:
Sum of all odd numbers from 1 - 20 = 100
Sum of all even numbers from 1 - 20 = 110
Sum of all numbers from 100 - 200, divisible by 7 = 2107
Total numbers from 100 - 200, divisible by 7 = 14
________________________________________________________________________________
PRACTICAL NO.14----->
Computing marks of students
using System;
class MarksRange
{
public static void Main()
{
int i, count80 = 0, count60 = 0, count40 = 0, count0 = 0;
float [] marks =
{57.5F,45.9F,98.01F,56.4F,46.5F,80,82,67,76,49,91,55,78,79,19.5F,25.8F,35,36,35,28,25.8F,4
6,55,59,68,97,85,48.5F,67,84};
for (i = 0; i<=29; i++)
{
if(marks[i] > 80 && marks [i] < 101)
{
count80 = count80 + 1;
}
else if(marks [i] > 60 && marks[i] < 81)
{
count60 = count60 + 1;
}
else if(marks [i] > 40 && marks[i] < 61)
{
count40 = count40 + 1;
}
else
{
count0 = count0 + 1;
}
}
Console.WriteLine("Students in the range of 81 - 100 : "+ count80);
Console.WriteLine("Students in the range of 61 - 80 : "+ count60);
Console.WriteLine("Students in the range of 41 - 60 : "+ count40);
Console.WriteLine("Students in the range of 0 - 40 : "+ count0);
Console.ReadLine();
}
}
Output:
Students in the range of 81 - 100 : 6
Students in the range of 61 - 80 : 7
Students in the range of 41 - 60 : 10
Students in the range of 0 - 40 : 7
________________________________________________________________________________
PRACTICAL NO.15----->
Selecting students on the basis of some given criteria on marks
using System;
class Admission
{
public static void Main()
{
float mksMaths, mksPhysics, mksChemistry, mksTotal, MathsPhysics;
int response;
beginning:
Console.WriteLine(""); // Blank Line
Console.WriteLine(" ********** Students Enrollment Checking Criteria
********** ");
Console.WriteLine(""); // Blank Line
Console.Write("Enter the marks in Maths : ");
mksMaths = float.Parse(Console.ReadLine());
Console.Write("Enter the marks in Chemistry : ");
mksChemistry = float.Parse(Console.ReadLine());
Console.Write("Enter the marks in Physics : ");
mksPhysics = float.Parse(Console.ReadLine());
mksTotal = (float)(mksMaths + mksChemistry + mksPhysics);
MathsPhysics = (float)(mksMaths + mksPhysics);
if ((mksMaths >= 60 && mksPhysics >= 50 && mksChemistry >= 40) || (mksTotal >=
200 || (mksMaths + mksPhysics) >= 150))
{
Console.WriteLine("Congratulations !!! The candidate is selected ... ");
}
else
{
Console.WriteLine("Sorry, The candidate is rejected ... Better luck for
next year.");
}
Console.WriteLine(""); // Blank Line
Console.Write("Enter 1 for next candidate, 0 to exit : ");
response = int.Parse(Console.ReadLine());
if (response == 1)
goto beginning;
else
goto end;
end:
Console.ReadLine();
}
}
Output:
********** Students Enrollment Checking Criteria **********
Enter the marks in Maths : 50
Enter the marks in Chemistry : 40
Enter the marks in Physics : 35
Sorry, The candidate is rejected ... Better luck for next year.
Enter 1 for next candidate, 0 to exit : 1
********** Students Enrollment Checking Criteria **********
Enter the marks in Maths : 70
Enter the marks in Chemistry : 80
Enter the marks in Physics : 85
Congratulations !!! The candidate is selected ...
Enter 1 for next candidate, 0 to exit : 0
__________________________________________________________________________
Subscribe to:
Posts (Atom)