C# Balaguruswamy Solved Programs
C# Balaguruswamy Solved Programs
Chapter # 4
Sr.no Topic Date Sign
1. Illustrating the Concept of Declaration of variables 16‐1‐2007
2. Declaration & Additions of variables 16‐1‐2007
3. Program with a function 16‐1‐2007
4. Demonstrating Boxing & Unboxing 16‐1‐2007
5. Demonstrating addition of byte type variables 16‐1‐2007
6. Implementing some custom console output 16‐1‐2007
7. Printing a home like figure in the console 16‐1‐2007
8. Executing some console statements 16‐1‐2007
Chapter # 3
(Overview of C#)
3.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: Daljit
Enter Your Last Name: Singh
Hello Mr. Daljit Singh
3.2 ‐ Changing String order
using System;
class Prog3_2
{
public static void Main(String [] args)
{
Console.Write(args[2] + args[0] + args[1]);
}
}
3.3 – More than one class
using System;
class ClassOne
{
public void One() // A function named One
{
Console.Write("C Sharp ");
}
}
class Mainly
{
public static void Main() // A function named Main (Main Function)
{
ClassOne demoObj = new ClassOne (); //Creating ojecct of ClassOne
demoObj.One (); // Will display ‐‐‐> C Sharp
Console.Write ("Programming"); // Will display ‐‐‐> Programming
// Both "C Sharp" & "Programming" will be displayed in a single line due to this line ‐‐‐‐>
Console.Write("C Sharp ");
Console.ReadLine ();
}
}
OUTPUT
C Sharp Programming
3.4 – 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
3.5 – 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
Chapter # 4
(Literals, Variables & Data Types)
4.1 – Illustrating the Concept of Declaration of variables
class Variable_Concepts
{
public static void Main ()
{
char ch = 'A'; // Declaring a Character variable with value = 'A'
byte a = 50; // Declaring a byte variable with value = 50
int b = 123456789; // Declaring an Integer variable with value = 123456789
long c = 1234567654321; // Declaring a Long type variable with value = 1234567654321
bool d = true; // Declaring a Boolean type variable with TRUE value
float e = 0.000000345F; // Declaring a float type variable with value = 0.000000345. The value ends with a
'F' resembeling a float data type
float f = 1.23e5F; // Declaring a float type exponential variable with value = 1.23E5 = 123000. The value
contains the character 'e' resembeling an exponential value.Also, the value ends with a 'F' resembeling a float
data type.
}
}
4.2 – 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
4.3 – Program with a function
class ABC
{
static int m;
int n;
void fun(int x, ref int y, out int z, int [] a)
{
int j = 10;
}
}
// ***************** Comment on Output *******************
// The out parameter 'z' must be assigned to before the control leaves the current method
4.4 ‐ 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();
}
}
4.5 – 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
4.6 – 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” !
*
**
***
4.7 – 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
/ \
/ \
/ \
‐‐‐‐‐‐‐‐‐‐‐‐‐
“ ”
” ”
” ”
4.8 – Executing some console statements
using System;
class Demo
{
public static void Main()
{
int m = 100;
long n = 200;
long l = m + n;
Console.WriteLine("l = "+ l);
Console.ReadLine();
// No error in the program.
}
}
OUTPUT
l = 300
Chapter # 5
Sr.no Topic Date Sign
1. Computation of Integer Values taken from console 30/1/2007
2. Computation of Float Values taken from console 30/1/2007
3. Average of 3 numbers 30/1/2007
4. Finding circumference & area of a circle 30/1/2007
5. Checking for validity of an expression 30/1/2007
6. Converting Rs. To Paisa 30/1/2007
7. Converting temp. from Fahrenheit to Celsius 30/1/2007
8. Determining salvage value of an item 30/1/2007
9. Reading & displaying the computed output of a real no. 30/1/2007
10. Evaluating distance travelled by a vehicle 30/1/2007
11. Finding the EOQ(Economic Order Quantity) & TBO(Time between Orders) 30/1/2007
12. Finding the frequencies for a range of different capacitance. 30/1/2007
Chapter # 6
Sr.no Topic Date Sign
1. Adding odd & even nos from 0 – 20 & adding nos. divisible by 7 between 100 ‐ 200 6/1/07
2. Finding a solution of linear equation 6/1/07
3. Computing marks of students 6/1/07
4. Selecting students on the basis of some given criteria on marks 6/1/07
5. Printing Floyd’s triangle 6/1/07
6. Computing seasonal discount of a showroom 6/1/07
7. Reading ‘x’, Correspondingly Printing ‘y’ 6/1/07
Chapter # 5
(Operators & Expressions)
5.1 # Computation of Integer Values taken from console
using System;
class integerdemo
{
public static void Main()
{
string s1,s2;
int a,b;
// Integer manipulations
Console.ReadLine();
}
}
Output:
Enter no 1 # 25
Enter no 2 # 15
No1 + No2 = 40
No1 - No2 = 10
No1 / No2 = 1
No1 % No2 = 10
5.2 # Computation of Float Values taken from console
using System;
using System;
class floatdemo
{
public static void Main()
{
string s1,s2;
float a,b;
// Integer manipulations
Console.ReadLine();
}
}
Output:
Enter no 1 # 25.64
Enter no 2 # 15.87
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
5.4 # 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;
}
}
Output:
The Radius of the circle = 12.5
5.5 # Checking for validity of an expression
using System;
class CheckExpression
{
public static void Main()
{
int x,y,a,b;
x - y = 100;
// gives error
//"The left-hand side of an assignment must be a variable, property or
indexer"
x - (y = 100);
// gives error
//"Only assignment, call, increment, decrement, and new object expressions
// can be used as a statement"
}
}
5.6 # 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
class Temperature
{
public static void Main()
{
float fahrenheit,celcius;
string s;
Console.Write("Enter the temperature in fahrenheit : ");
s = Console.ReadLine();
fahrenheit = float.Parse(s);
celcius = (float)((fahrenheit-32)/1.8);
Console.WriteLine("The Temperature in celcius = " +celcius);
Console.ReadLine();
}
}
Output:
Enter the temperature in fahrenheit : 98
class depreciation
{
public static void Main()
{
float depreciation, PurchasePrice, Yrs, SalvageValue;
string d,p,y;
// string variables are to store the values inputted in the console
// each string variable has its character as that of the corresponding
// starting character of float type variable
Console.ReadLine();
}
}
Output:
Enter the Depreciation : 50
SalvageValue = 3456.4564
5.11 # Evaluating distance travelled by a vehicle
using System;
class Distance
{
public static void Main()
{
float distance,u,t,a;
string u1,t1,a1,reply;
// u = Initial velocity
// t = Time intervals
// a = Acceleration
// reply is the value used to check for again restart the program with
different values
int replyforrestart,counter;
// replyforrestart will take values either 0 or 1.
// 1 means restart for next set of values, 0 means exit the program
// counter is used for checking the no. of times the set of values occurs
counter = 1;
// For the first run, counter = 1
startfromhere: // The program will restart from here for another set of
values.
reply = Console.ReadLine();
replyforrestart = int.Parse(reply);
if (replyforrestart == 1)
{
counter = counter+ 1;
Console.WriteLine(""); // Blank Line
Console.WriteLine("
************************************************************************ ");
goto startfromhere;
}
else
{
// Do nothing ... Simply program exits
}
}
}
Output:
******** This will calculate the distance travelled by a vehicle **********
Set of value = 1
************************************************************************
Set of value = 2
using System;
class InventoryManagement
{
public static void Main()
{
float dr,sc,cpu;
//dr = Demand rate, sc = setup costs, cpu = cost per unit
double EOQ,TBO;
// EOQ = Economic Order Quaitity
// TBQ = Optimal Time Between orders
Console.ReadLine();
}
}
Output:
Enter the Demand Rate : 150
5.12 # Finding the frequencies for a range of different capacitance.
using System;
class ElectricalCircuit
{
public static void Main()
{
float L,R,C,Frequency;
// L = Inductance
// R = Resistance
// C = Capacitance
//double Frequency;
Frequency = (float)(Math.Sqrt((1/L*C)-((R*R)/(4*C*C))));
Console.WriteLine("For Capacitance " + C + ", The Frequency = " +
Frequency);
}
Console.ReadLine();
}
}
Output:
****** Calculating frequencies for different values of Capacitance ******
6.1 # 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
Console.ReadLine();
}
}
Output:
Sum of all odd numbers from 1 - 20 = 100
class LinearEquations
{
public static void Main()
{
int response;
float a,b,c,d,m,n, temp;
double x1,x2;
EnterNewValuesAgain:
if (temp == 0)
{
Console.WriteLine(""); // Blank Line
Console.WriteLine("The denominator equals to zero (0); Cannot proceed
further ...");
Console.Write("Do You want to enter new values (1 For Yes / 0 For No) ?
");
response = int.Parse(Console.ReadLine());
if (response == 0)
{
goto Exit;
}
else
{
goto EnterNewValuesAgain;
}
}
else
{
// Reading the value of m
Console.Write("Enter the value of m : ");
m = float.Parse(Console.ReadLine());
// Reading the value of n
Console.Write("Enter the value of n : ");
n = float.Parse(Console.ReadLine());
Console.Write("Do You want to enter new values (1 For Yes / 0 For No) ?
");
response = int.Parse(Console.ReadLine());
if (response == 0)
{
goto Exit;
}
else
{
goto EnterNewValuesAgain;
}
Exit:
Console.WriteLine(""); // Blank Line
Console.WriteLine("Thank You For using this small program ... :)");
Console.ReadLine();
}
}
Output:
********************** Linear Equation *********************
Value of x1 = 0.4561
Value of x2 = 0.364821
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};
{
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.ReadLine();
}
}
Output:
Students in the range of 81 - 100 : 6
class Admission
{
public static void Main()
{
float mksMaths, mksPhysics, mksChemistry, mksTotal, MathsPhysics;
int response;
beginning:
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.");
}
if (response == 1)
goto beginning;
else
goto end;
end:
Console.ReadLine();
}
}
Output:
Sorry, The candidate is rejected ... Better luck for next year.
6.8 # Floyd’s Triangle
using System;
class FloydsTriangle1
{
public static void Main()
{
int i,j,k=1;
Console.WriteLine(" ************ Floyd's Triangle - Normal Numeric Mode
**************** ");
Console.ReadLine();
}
}
Output:
************ Floyd's Triangle - Normal Numeric Mode ****************
2 3
4 5 6
7 8 9 10
11 12 13 14 15
16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31 32 33 34 35 36
37 38 39 40 41 42 43 44 45
46 47 48 49 50 51 52 53 54 55
56 57 58 59 60 61 62 63 64 65 66
67 68 69 70 71 72 73 74 75 76 77 78
79 80 81 82 83 84 85 86 87 88 89 90 91
6.9 # Computing seasonal discount of a showroom
using System;
class SeasonalDiscount
{
public static void Main()
{
int amt;
float Mill_disc,Hand_disc, DiscountedAmt;
}
else if (amt >= 201 && amt <= 300)
{
Mill_disc = amt * 0.75F;
Hand_disc = amt * 0.1F;
}
else if (amt > 300)
{
Mill_disc = amt * 0.1F;
Hand_disc = amt * 0.15F;
Console.ReadLine();
}
}
Output:
*************** Seasonal Discount of a Mall **************
nAfter all the discounts, you need to pay a sum of 95, instead of 100, thus making a
profit of 5Rs.
6.10 # Reading ‘x’, Correspondingly Printing ‘y’
using System;
class ChangingValuesOfY
{
public static void Main()
{
int x,y;
if (x != 0)
{
if (x > 0)
{
Console.WriteLine("Y = 1");
}
if (x < 0)
{
Console.WriteLine("Y = -1");
}
}
else
{
Console.WriteLine("Y = 0");
}
if (x == 0)
{
Console.WriteLine("Y = 0");
}
else if(x > 0)
{
Console.WriteLine("Y = 1");
}
else
{
Console.WriteLine("Y = -1");
}
Console.WriteLine(""); // Blank Line
y = (x != 0)?((x>0)?1:-1):0;
Console.WriteLine("Y = "+y);
Console.ReadLine();
}
}
Output:
Enter the value of x : 5
Y = 1
Y = 1
Y = 1
CHAPTER # 7
Sr.no Topic Date Sign
class ReverseNumber
{
public static void Main()
{
int num,rem,i,counter=0,temp;
// num : Contains the actual number inputted via the console
// rem : remainder of the number 'num' when divided by 10
// i : loop variable
// counter : determines the no. of digits in the inputted number 'num'
// temp : temporary variable used to save the value of 'num' (Explained
further)
temp = num;
// Here we are saving 'num' in 'temp' coz its value after determining the no.
of digits will loose.
// So after its work is done, 'num' will contain value = 0
// The value of 'num' is resetted to its original value later from 'temp'
variable
rem = 0;
// resetting the value of remainder 'rem'
num = temp;
// resetting the lost value of 'num' from 'temp'
Console.ReadLine();
}
}
Output:
Enter an integer number (Not more than 9 digits) : 3547786
class Factorial
{
public static void Main()
{
int no,i,fact=1;
Console.Write("Enter a number to find its factorial : ");
no = int.Parse(Console.ReadLine());
if (no != 0)
{
for (i = no; i>=1; i--)
{
fact = fact * i;
}
Console.WriteLine("Factorial = " +fact);
}
else
{
Console.WriteLine("You entered 0, not valid.");
}
Console.ReadLine();
}
}
Output:
Factorial = 362880
7.3 #Calculating the sum of digits of the given number
using System;
class SumOfNumbers
{
public static void Main()
{
int num,rem,i,counter=0,temp,sum=0;
// num : Contains the actual number inputted via the console
// rem : remainder of the number 'num' when divided by 10
// i : loop variable
// counter : determines the no. of digits in the inputted number 'num'
// temp : temporary variable used to save the value of 'num' (Explained
further)
temp = num;
// Here we are saving 'num' in 'temp' coz its value after determining the no.
of digits will loose.
// So after its work is done, 'num' will contain value = 0
// The value of 'num' is resetted to its original value later from 'temp'
variable
rem = 0;
// resetting the value of remainder 'rem'
num = temp;
// resetting the lost value of 'num' from 'temp'
}
}
Output:
Enter an integer number (Not more than 9 digits) : 65478457
Number of digits : 8
Sum of digits : 46
7.4 # Printing & adding Fibonacci series
using System;
class Fibonacci
{
public static void Main()
{
int first = 1, second = 1, third, no, count = 0;
long sum = 2;
// 'first', 'second', 'third' are the first, second & third numbers in the
fibonacci series
// 'first' & 'second' are both initialised to 1
// sum of 'first' & 'second' are added to the 'third' variable
// 'sum' will contain the sum of all the digits in the fibonacci series. It is
initialies to 2 coz sum of first 2 digits is 2
// 'no' is the number inputted from the console up till which the fibonacci
series is displayed
// 'count' counts the number of digits in the fibonacci series
Console.Write("Enter the number uptill which you want the fibonacci numbers :
");
no = int.Parse(Console.ReadLine());
do
{
third = first + second;
// adding 'third' = 'first' + 'second'
Console.Write(" "+third);
// display the 'third' digit in the series
first = second;
// make 'first' digit, the 'second' one
second = third;
// make 'second' digit, the 'third' one
count = count + 1;
// increment the counter
}
while((count + 3) <= no);
// we entered the 'no' from the console & also the first 2 digits are not from
this loop
// thus we added +3 here to the 'count' variable so that we get the exact
specified no. of digits.
// if we didnt added 3, then the series will go beyond the specified number of
digits from the console via 'no'
Output:
Enter the number uptill which you want the fibonacci numbers : 8
Fibonacci Series : 1 1 2 3 5 8 13 21 34
7.5 #Investment Equation
using System;
class Investment
{
public static void Main()
{
int P=1000,n;
float r=0.1F;
double V;
Console.ReadLine();
}
}
Output:
7.7 # Converting $ into Rs.
using System;
class DollarToRupees
{
public static void Main()
{
float dol,rs,current;
int i;
for (i=1;i<=5;i++)
{
Console.Write("Enter value " + i + " in Dollars : ");
dol = float.Parse(Console.ReadLine());
rs = dol * current;
Console.WriteLine(dol + " $ = " +rs + "Rs.");
Console.WriteLine(""); // Blank Line
}
Console.ReadLine();
}
}
Output:
7.10 #Demonstrating use of break, continue & goto
using System;
class BreakContiuneGoto
{
public static void Main()
{
int n = 10;
while(n<200)
{
if(n<100)
{
if(n<50)
{
goto lessthan50;
}
Console.Write(" " +n);
n = n + 20;
continue;
}
lessthan50:
{
Console.Write(" " +n);
n = n + 10;
continue;
}
if(n==50)
{
Console.WriteLine("");
n = n + 10;
continue;
}
Output:
10 20 30 40 50 60 70 80 90 110 120 130 140 150 160 170 180 190
7.6 - PRINTING TRIANGLES INTO VARIOUS FORMATS
a)
using System;
class DollarDesign
{
public static void Main()
{
int no=1,i,j;
Output:
1
22
333
4444
55555
b)
using System;
class TriangleDollar
{
public static void Main()
{
int i,j,k;
string d="$";
for(i=1;i<=5;i++)
{
for(k=1;k<=i;k++)
Console.Write(" ");
for(j=5;j>=i;j--)
{
Console.Write ("$",+j); // Enter the space with a '$' sign
// This is another syntax of Console.Write method. Here the digit after the comma ‘,’
signifies the position of the first character ‘$’ on the output screen.
}
class StdDeviation
{
public static void Main()
{
float [] nos = {3.5F,57,2,6,24,14,95,23,74,23};
int n = nos.Length;
float sum = 0.0F,sumofsq = 0.0F, mean;
double deviation;
mean = sum / n;
deviation = Math.Sqrt(sumofsq / 8.0);
Output:
Array List consists of : 3.5 57 2 6 24 14 95 23 74 23
Sum = 321.5
Mean = 32.15
Deviation = 49.5381797202
8.13 & 8.14 - FINDING THE MAXIMUM & MINIMUM OF 3 NUMBERS ENTERED
using System;
class LargestSmallest
{
public static void Main()
{
int a,b,c,largest,smallest;
Console.Write("Enter No 1 : ");
a = int.Parse(Console.ReadLine());
Console.Write("Enter No 2 : ");
b = int.Parse(Console.ReadLine());
Console.Write("Enter No 3 : ");
c = int.Parse(Console.ReadLine());
if (a > b)
{
if(a > c)
{
largest = a;
}
else
{
largest = c;
}
}
else
{
if(c>b)
{
largest = c;
}
else
{
largest = b;
}
}
if (a < b)
{
if(a < c)
{
smallest = a;
}
else
{
smallest = c;
}
}
else
{
if(c<b)
{
smallest = c;
}
else
{
smallest = b;
}
}
Output:
Enter No 1 : 15
Enter No 2 : 54
Enter No 3 : 21
using System;
class ArrayFunction
{
public static void Main()
{
long Largest;
double Average;
int c;
int num;
int[] array1;
c=int.Parse(Console.ReadLine());
array1=new int[c];
Console.WriteLine ();
Largest = Large(array1);
Average = Avg(array1);
return(sum);
}
}
Output:
Enter the number of Elements in an Array : 5
Output:
Sorted array list : 127 150 157 157 240 255 275 510 550 750
9.11 - ACCEPTING A LIST OF 5 ITEMS
using System;
using System.Collections;
class ShoppingList
{
public static void Main(string []args)
{
ArrayList n = new ArrayList ();
n.Add(args[0]);
n.Add(args[1]);
n.Add(args[2]);
n.Add(args[3]);
n.Add(args[4]);
n.Sort();
Console.WriteLine ("The items in the Shopping List are : ");
Console.WriteLine ("The items in the Shopping List After modifying are : ");
Console.ReadLine();
}
}
Output:
The items in the Shopping List are : Karan Girish Neha Gaurav Raju
The items in the Shopping List After modifying are : Karan Girish Raju Daljit End
10.8 – COUNTING NUMBER OF WORDS IN A STRING
using System;
class CountWords
{
public static void Main()
{
string s = ""; // Declare 's' of string type
Console.ReadLine();
}
}
Output:
Enter the string : Daljit is making programs
using System;
public class ReverseArray
{
public string Reverse(params string [] arr)
{
string [] j;
string [] k;
foreach (int i in j)
{
Console.Write(" "+j); // Print the elements of the array 'j'
i++;
}
}
}
10.9 – READ AN ARRAY & SORT IT
using System;
using System.Collections; // We need to implement collection class
class ArrayList
{
public static void Main(string []args)
{
ArrayList n = new ArrayList ();
Console.WriteLine ("The items in the Array List before sorting are : ");
Console.WriteLine ("The items in the Array List after sorting are : ");
Output:
The items in the Array List before sorting are : Rajawnt Karan Girish Zeenat Daljit
The items in the Array List before sorting are : Daljit Girish Karan Rajawnt Zeenat