Practical Assignment No 1 To 9
Practical Assignment No 1 To 9
Practical Assignment No 1 To 9
1) Write a program to read two strings from keyboard using the readline statement and
display them on one line using two write statements ??
2) Write a program that assigns two double type value to two variables. Compute their
sum,assigns the result to a 3rd variable & display all the 3 values in one line of
output ?
Using System;
Class example
{
static void main()
{
double a,b,c;
Console.writeline("Enter the number");
a = Double.Parse(Console.ReadLine());
b = Double.Parse(Console.ReadLine());
c = a + b;
Console.writeLine("Sum is "+a" "+b+"is"+c);
}
}
*
* * *
* * * * *
* * *
*
Using System;
Class example
{
public static void main()
{
int i,j,k,s=10,b=1;
for (i=1;i<=5;i++)
{
for(k=1;k<=5;k++)
{
rad = Double.Parse(Console.ReadLine());
{
Console.Write("");
for(j=1;j<=b;j++)
{
Console.write("*");
}
if(i<3)
{
s=s-2;
b=b+2;
}
else
{s=s+2;
b=b-2;
}
Console.WriteLine();
}
}
Using System;
Class Area
{
double rad,ar;
static void main()
{
Console.Writeline("Enter radius");
rad = Double.Parse(Console.ReadLine());
ar = 3.142 * rad * rad
Console.Writeline("Area is" + ar);
}
}
Accept the values for five subjects & print their total & average using command line argument?
Using system;
Class example
{
Static void main(String args[])
{
int m1,m2,m3,m4,m5,tot;
double avg;
m1 = int.parse(args[0]);
m2 = int.parse(args[0]);
m3 = int.parse(args[0]);
m4 = int.parse(args[0]);
m5 = int.parse(args[0]);
tot = m1+m2+m3+m4+m5;
avg = tot/5;
Console.writeLine(“Average is”+avg);
Console.writeLine(“Total is”+avg);
}
}
Using System;
Class example
{
Static void main(String[] ar)
String name=” ”, add=” ”;
int sal ,age;
name = ar[0];
age = int.Parse(ar[1]);
add.ar[2];
sal = int.Parse(ar[4]);
Console.writeLine(“Employee Details”);
Console.writeLine(“Name is”+name+”age”+age+” “+ “Address”+add+”salary”+sal);
}
}
Write a program to calculate the area of rectangle using command line argument ?
Using system;
Class example
{
Public static void main(String[] ar)
{
int len,wild;
Double ar;
len = int.Parse(ar[0]);
wid = int.Parse(ar[1]);
ar = len*wid;
Console.writeline(“Area=”+ar);
}
5) Write a program that takes the line of text “John F Kennedy” as a command line input
& display the following output Kennedy John F ?
Using System;
Class PGR
{
String s1,s2,s3;
s1= ar[0];
s2=ar[1];
s3=ar[2];
Console.write(s3);
Console.write(s1);
Console.write(s2);
}
}
6) Define the two classes one with a method to display the string “C sharp”& the other to
display the string “Programming”. Write a program using these classes to display a
single line output as follows C# programming.
Using system;
Class A
{
String s1 = “C Sharp”;
Public void display()
{
Console.write(s1);
}
}
Class B
{
String s2 = “Programming”;
Public void display()
{
Console.write(s2);
}
}
Class C
{
Public static void main()
{
A a1 = new A();
B b1 = new B();
Console.writeLine(a1.display()+b1.display());
}
}
7) Write a program to print any 10 number & their addition using command line ?
Using System;
Class example
{
Public static void main(String[] ar)
{
int a,s=0;
for (int i=1 , i<=10 , i++)
{
a = int.Parse(ar[i]);
s=s+2;
b=b-2;
}
Console.writeline();
}
}
Practical Assignment 2
Solu:
using System;
class A
{
public static void Main()
{
int m=100;
int n=200;
long l=min;
Console.WriteLine(“l= “+l);
}
}
O/P:
l=300.
Solu:
using System;
class checknum
{
public static void Main()
{
double zero=0;
Console.WriteLine(“The result of division by zero in C# is: {0}”,(0/zero));
}
}
Output:
The result of division by zero in C#
Prog:
using Sysytem;
class DataType Sample
{
public static void Main()
{
Console.WriteLine(“Integer Literal:{0}”,58);
Console.WriteLine(“Double Literal:{0}”,1.61803398134565);
Console.WriteLine(“Exponential Notation Decimal Literal:{0}”,6023E23F);
Console.WriteLine(“Hexadecimal Literal:{0}”,0x003A);
}
}
Output:
Integer Literal: 58
Double Literal: 1.6180339887498
Exponential Notation for Decimal Literal: 6.023E+23
Hexadecimal Literal: 58.
Solu:
using System;
class Average
{
public static void Main(string[] args)
{
int num1=int.Parse(arg[0]);
int num2=int.Parse(arg[1]);
int num3=int.Parse(arg[2]);
double avg=(num1+num2+num3)/3;
Console.WriteLine(“Average: “+avg);
}
}
5) WAP to read the price of an item in decimal form & print the output in paise.
Solu:
using Sysytem;
class A
{
public static void Main()
{
double rs, paise;
Console.WriteLine(“Enter the amount in rupees: “);
rs=double.Parse(Console.ReadLine());
paise=rs*100;
Console.WriteLine(“Paise= “+paise);
}
}
6) WAP to convert the given temperature in Fahrenheit to Celsius using the foll.
conversion formula:
C=(F-32)/1.8
Solu:
using System;
class Temp
{
public static void Main()
{
Console.WriteLine(“Enter the temperature: “);
double ft=double.Parse(Console.ReadLine());
double ct=(ft-32)1.8;
Console.WriteLine(“The temperature in Celsius: “+ct);
}
}
Practical Assignment No 3
code -
using system;
class prog1
{
public static void main()
{
for(int r=1;r<=3;r++)
{
for(int c=1;c<=r;c++)
{
console.write("*");
}
console.writeline();
}
}
}
code -
using system;
class prog2
{
public static void main()
{
int k=3;
for(int i=3;i>=1;i--)
{
if(i==1)
k=3;
for(int j=1;j<=k;j++)
{
console.writeline("*");
k--;
console.writeline();
}
}
}
}
code -
using system;
class prog3
{
public static void main()
{
for(int r=3;r>=1;r--)
{
for(int c=1;c<=r;c++)
{
console.write("*");
}
console.writeline();
}
}
}
4) Print the pattern :
*
***
*
code -
using system;
class prog4
{
public static void main()
{
int j<=1,spc=1;
for(int i=1;i<=3;i++)
{
if(if==2)
spc=1;
else
{
for(int spc=1;spc<=spc;spc--)
console.write(" ");
spc--;
}
if(jc>3)
j<=i;
for(int j=1;j<=jc;j++)
console.write("*");
jc+=2;
console.writeline();
}
}
}
code -
using system;
class prog5
{
public static void main()
{
int r,c;
for(r=1;r<=5;r=r+2)
{
for(c=1;c<=r;c++)
{
console.writeline("*");
}
console.writeline("/n");
}
}
}
code -
using system;
class prog6
{
public static void main()
{
int n,a=1;
n=int.parse(args[0]);
for(int r=1;r<=n;r++)
{
for(int c=1;c<=r;c++)
{
console.write(a);
a++;
}
console.writeline();
}
}
}
Print the pattern :
###
##
#
code -
using system;
class prog7
{
public static void main()
{
for(int r=3;r>=1;r--)
{
for(int c=1;c<=r;c++)
{
console.write("#");
}
console.writeline();
}
}
}
code -
using system;
class prog8
{
int i,j;
for(i= 0;i<=3;i++)
{
for(j=o;j<=i;j++)
{
if(j==i||i==j+2)
{
console.write("1");
}
else
{
console.write("0");
}
}
console.writeline();
}
}
}
1
121
1331
14641
code -
using system;
class prog9
{
int i,j;
for(i= 0;i<5;i++)
{
for(j=o;j<=i;j++)
{
if(j==0||i==0)
{
console.write(b+" ");
}
else
b=b*(i-j+1)/j;
console.write(b+" ");
}
}
console.writeline();
}
}
}
code -
using system;
class prog10
{
public static void main()
{
string s="Mumbai";
int len=s.length;
while(len>0)
{
console.writeline(s.substring(0.len));
len--;
}
}
}
Practical No 4
1) Write a program to print following outputs using FOR loop.
A] 1
22
333
4444
55555
Using system;
class A
{
public static void main (string []ar)
{
int i,j;
for (i=1; i<6; i++)
{
for (j=1;j<=i ;i++)
{
Console.Write (i+ “ ”);
}
Console.Writeline ();
}
}
}
B] $ $ $ $ $
$$$$
$$$
$$
$
using system;
class B
{
public static void Main ()
{
int i, j, k ;
for (i=1; i<=5 ; i++)
{
for (k=1; k<=i; k++)
{
Console.Write (“ ”);
}
for(j=5; j>=i; j--)
{
Console.Write (“ $ ”);
}
Console.WriteLine ();
}
}
}
C] 1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
using System;
class C
public static void Main ()
{
int i, j, num=5, k;
for (i=1; i<=num; i++)
{
for (k=num; k>=i; k--)
{
Console.Write (“ ”);
}
for (j=1; j<=i; j++)
{
Console.Write (“ ” =i)
}
Console.Write ();
}
}
}
2) Write a program using While loop to reverse the digit.
Solution
using System;
class D
{
public static void Main ()
{
int num, rem;
Console.Write (“Enter an integer No”);
num=int.Parse (console.readline());
while (num>0)
{
rem = num % 10;
Console.Write (num);
num = num / 10;
}
}
}
Solution
using System;
class E
{
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.Write (“Factorial = “ + fact”);
}
else
{
Console.WriteLine(“You entered 0, not valid”);
}
Console.Readline ();
}
}
Solution
using System;
class E
{
public static void Main ()
{
int num, rem, i, counter = 0, temp, sum = 0;
Console.Write (“Enter an integer number”);
num = int.Parse (console.Readline());
temp = num;
while(num>0)
{
rem = num % 10;
num = num / 10;
if (num<=10)
{
break;
}
else
{
{
counter = counter + 1;
}
}
Console.Writeline (“Number of digits are = “+ (counter + 1));
rem = 0;
num = temp;
for (i = 0; i<=counter; i++);
{
rem = num % 10
sum = sum + rem;
num = num / 10;
}
Console.Writeline (“Sum = ” + sum);
Console.Readline ();
}
}
Solution
using System;
class F
{
public static void Main ()
{
int f = 1, s = 1, t, no, count = 0;
Console.Write (“Enter the numbers”);
no = int.Parse (console.Readline());
Console.Write (“Fibonacci series:” “t++” +s);
do
{
t = t + s;
Console.Write (“” +t);
t = s;
s = t;
count = count + 1;
}
while ((count + 3) < = no);
}
Solution
using System;
class G
{
public static void Main ()
{
int P = 1000, n;
float r = 0.1F;
double V;
Console.Writeline (“*** Investment Equation *** “ );
Console.Writeline (“ ”);
Console.Writeline (“Principle (P) , Rate (R) , No of Yrs (n) , Value of Money (V) \n”);
V = P * (1 + r);
for (n = 1; n<= 10; n + 1);
{
Console.Writeline ( “ ” + p, “ ” + r, “ ” + n, “ ” + v”)
P = p + 1000;
r = r + 0.01F;
y = P * (1 + r);
}
Console.Readline ();
}
}
7) Wap that accepts five values in US dollars, one at a time and convert each value
entered o Indian rupees.
Solution
Using System;
class H
{
public static void Main ()
{
float dol ,rs ,current;
int I;
Console.Write (“ what is current value of $ as per Indian rupees…: “);
current = float.Parse (Console.Readline());
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 (“ ”);
}
}
Practical Assignment 5
1) WAP to add odd & even nos. from 0-20 & adding nos. divisible by 7 between 100-
200.
Solu:
using System;
class J
{
public static void Main(string[] agrs)
{
int x=0, sumodd=0, sumeven=0, sumdiv7=0, totalno7=0, I;
for(i=0;i<=20;i++)
{
x=i%2;
if(x!=0)
{
sumodd=sumodd+I;
}
if(x==0)
{
sumeven=sumeven+I;
}
}
x=0;
for(i=100;i<=200;i++)
{
x=i%7;
if(x==0)
{
sumdiv7=sumdiv7+i;
totalno7=totalno7+i;
}
}
Console.WriteLine(“Sum of all odd nos.: “+sumodd);
Console.WriteLine(“Sum of all even nos,: “+sumeven);
Console.WriteLine(“Sum of all no. from 100-200, divisible by 7=
“+sumdiv7);
Console.WriteLine(“Total no. from 100-200, divisible by 7= “+totalno7);
Console.ReadLine();
}
}
Solu:
using System;
class K
{
public static void Main(string[] args)
{
int response;
float a,b,c,d,m,n,temp;
double x1,x2;
EnterNewValueAgain:
Console.WriteLine( “ “);
Console.Write(“Enter the value of a: “);
a=float.Parse(Console.ReadLine());
Console.Write(“Enter the value of b: “);
b=float.Parse(Console.ReadLine());
Console.Write(“Enter the value of c: “);
c=float.Parse(Console.ReadLine());
Console.Write(“Enter the value of d: “);
d=float.Parse(Console.ReadLine());
temp=a*d-b*c;
if(temp==0)
{
Console.WriteLine(“ “);
Console.WriteLine(“The denominator equals to 0 cannot proceed
further”);
Console.WriteLine(“Do you want to enter new value(1 For yes/ 0
For no)?”);
Response=int.Parse(Console.ReadLine());
if(response==0)
{
goto Exit;
}
else
{
goto EnterNewValueAgain;
}
}
else
{
Console.WriteLine(“Enter the value of m: “);
m=float.Parse(Console.ReadLine());
Console.WriteLine(“Enter the value of n: “);
n=float.Parse(Console.ReadLine());
x1=((m*d)+(b*n))/((a*d)-(c*b));
x2=((n*a)+(m*c))/((a*d)-(c*b));
Console.WriteLine(“Value of x1= “+x1);
Console.WriteLine(“Value of x2= “+x2);
Console.WriteLine(“ “);
Console.WriteLine(“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 EnterNewValueAgain;
}
}
Exit:
Console.ReadLine();
}
}
Solu:
using System;
class M
{
public static void Main()
{
int i,count80=0, count60=0, count40=0, count0=0;
float[] marks={57.5f,45.9f,98.01f,56.4 f,46.5 f,80,82,67,76,49,91,55,78,79,19.5
f,25.8 f,35,36,35,28,25.8 f,46,55,59,68,97,85,48.5 f,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 range of 81-100: “+count80);
Console.WriteLine(“Students in range of 61-80: “+count60);
Console.WriteLine(“Students in range of 41-60: “+count40);
Console.WriteLine(“Students in range of 0-40: “+count0);
}
}
Solu:
using System;
class N
{
public static void Main()
{
{
Float mksmaths, mksphysics, mkschemistry, mkstotal;
Float mathsphysics;
Console.writeline(“enter the marks in maths :”);
Mksmaths=float.parse (console.readline() );
Consle.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(cmksmaths>=60 &&mksphysics)=50 && mkschemistry>=40)|| (mkstotal>200 ||
(mksmaths+mksphysics)>=150))
{
Console.writeline (“Congratulations!!! The candidate is selected”);
}
Else
{
console.writeline(“sorry”);
}
}
}
Using system;
Class o
{
Public static void main ()
{
Int i, j, k=1;
For (i=1; k=13; i++)
{
For (j=1; j<i+1; j++)
{
Console.write (k+ “”);
K++;
}
Console.writeline();
}
}
}
6) A cloth showroom
Using system;
Class seasonaldiscount
{
Public static void Main ()
{
Int amt;
Float mill_disc,dhand_disc, discountedAmt;
Console.write(“enter the purchase amount :”);
Amt= int.parse(console.readline() );
If(amt>=0 && amt<+100)
{
Mill_disc=amt<=100)
{
Mil_disc=amt*0.0f;
Hand_disc=amt*0.05f;
Console.writeline(“ \n You made a purchased :” +amt +”Rs.””);
Console.writeline(“ \n you are eligible to receive a discount of \n” +( amt –mill_disct”rs.(0%) on
mill cloth & \n”+(amt – hand_disc + “Rs.(5%) on handloaam items.”;
DiscountAmt=amt –(mill_disc +hand_disc);
Console.writeline(“ \n All the discount ,You need to pay a sum of “ +discountAmt +”instead of ”
of pay a sum of “ + amt +”,\n thus making a profit of ”of +(mill_disc +hand_disc) +”Rs.”);
}
Else if (
amt>=101 && amt <=200)
{
Mill_disc=amt*0.05f;
Hnad_disc=amt*0.75F;
Console.writeline (“\n \n You made a purchase of:” + amt +”Rs.”);
Console.writeline(“\n You ar eligible to recive a discount of : \n” + (amt_mill_disc) +”
Rs. = (5%) on mill cloth & \n”+ (amt-hand_disc) +”RS. = (7.5%) on hanloom items.”);
discountedAMt=amt –(mill_disc + Hand_disc);
console.writeline(“\n After all Discounts ,You need to pay a sum of “ + DiscountAmt +” instead
of “ +,
\n thus making a profit of “+ (Mill_disc + Hand_disc) +”Rs.”);
Else if (amt >+201 && amt<=300)
{
Mill_disc=amt*0.75F;
Hand_disc=amt* 0.1F
Console.writeline(“ \n you made a purchase of ;” +amt +””Rs.”);
Console.writeline (\n You are eligible to receive a discount of: \n” + (amt –mill_disc) +
“Rs.=(7.5%)
On mill cloth & \n” +(amt-mill_disc) +” Rs.=(10%) on handloom items”);
DiscountedAmt=amt – (mill_disc +hand-disc
Console.writeline(“\n After all the discounts you need to pay a sum of” +discountAmt +
“instead of “ +amt +”,\n thus making a profit of “ + (milldisc + handdisc) +”Rs.”);
}
Else If (amt>300)
{
Mill_disc=am*0.1F;
Hand_disc=amt*0.15F
Console.writeline(“\n You made a purchase of:” + amt+ “);
Console.writeline(“\n You are eligible to receive a discount of \n” + (amt-mill_disc) +”
Rs. = (10%) on Mill cloth &\n” + (amt+disc)+”Rs.=(15%) on handloom items”);
DiscountedAmt=amt-(mill_disc+hand_disc);
Console.writeline(“\n after all the discounts you need to pay a sum of “ +DiscountAmt +”instead
of “ +amt +”,
\n thus making a profit of “+ (mill_disc +hand_disc) +”Rs.”);
}
}
}
Using system;
Class changing values of Y
{
Public static void Main ()
{
Int x, y;
Console.write (“Eneter the Value of x :”);
X=int.parse.(console.readline() );
Console.writeline(“Changing values of Y by nested If “);
Console.writeline(“ ”);
If (x>0)
{
console.writeline (“y+1”);
}
}
Else
{
Console.wrieline (y=0”);
}
Console.writeline (“ “);
Console.writeline(“Changing values of Y by Else If statements”);
If (x == 0)
{
Console.writeline(“Y=0”);
}
Else If (x>0)
{
Console.writeline (“y=1”);
}
Else
{
Console.writeline(y+-1”);
}
Console.write(“ “);
Console.writeline (“changing values of Y by Conditional operator”);
Console.writeline(“ ”);
Y+(x! =0)? ((X>0)? 1: -1): 0;
Console.writeline(“ Y= ”+Y);
Console.readline ();
}
}
Practical Assignment No. 6
Using System;
Class LS
{
Public static void man()
{
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<c)
{
Smallest=a;
}
Else
{
Smallest=c;
}
}
Else
{
If(c<b)
{
Smallest=c;
}
Else
{
Smallest=b;
}
}
Console.Writeline(“Largest No.:”+Largest);
Console.Writeline(“Smallest No.:”+Smallest);
Console.Readline();
}
}
2) Write a void type method that takes two int type value parameters and one int type
out parameter and return the product of two value parameters through the output
parameter. WAP to test its working.
Using System;
Class ProductCal
{
static void Product(int x, int y,out int p)
{
P=x*y;
}
Public static void Main()
{
Product(10,20,out int m);
Console.WriteLine(“Product is:”+ m);
}
}
3) Write a method that would calculate the value of money for a given period of years abd
print the result giving the following details on one line of output. The method takes principle
amt, interest rate, period as input and does not return any value.
Solution:
Using System;
Class Money
{
public static void Main()
{
Double r, v;
Console.Write(“Enter the principle amount”);
Int p=int.Parse(Console.ReadLine());
Console.Write(“Enter the interest rate”);
r=double.Parse(Console.ReadLine());
Console.Write(“Enter the year”);
N=int.Parse(Console.ReadLine());
For(int i=1;i<=n;i++)
{
V=p*(i+r);
}
Console.WriteLine(“Final value=”+v);
}
}
Practical Assignment No. 7
1) WAP to merge one-dimentional arrays into a single sorted array that contains every items
from array A & B, in ascending order.
Using System;
Class SA
{
Public static void main()
{
Int [] A={127,157,240,550,510};
Int [] B={275,157,750,255,150};
Int length=(A.length+B.length);
Int [] C=new int[length];
Int i=0;j=0,k;
Console.Writeline (“Sorted array list:”);
For (k=0;k<=(i+j);k++);
{
If (A[i]<=B[j])
{
C[k]=A[i];
Console.write(C[k]+ “ “);
If (i<4)
{
I++;
}
}
Else
{
c[k]=b[j];
console.write(c[k]= “ “);
if (j<4)
{
J++;}
}
For (i=0;I<length;i++)
{
Console.writeline (c[i]+ “ “);
}
}
}
}
3) WAP that counts a number of occurances of a particular character in a line of text.
Using System;
Class Prog
{
Public static void main()
{
Strinf user;
Char a=’a’;
Int c=0;
Console.Writeline (“Enter a String”);
User Console.Readline();
Console.Writeline (“Enter a Character to check”);
C=convert.tochar(console.Readline());
For(int i=0; i< user.length; i++)
{
If (user[i]==a)
{
C#;
}
}
If (c>a)
{
Console.Writeline (“The character {0} has {1} occourances” a,c);
}
}
}
Using System;
Using System Collections;
Class AL
{
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]);
Console.Writeline(“The items before sorting:”);
For (int i=0; i<n.count; i++)
{
Console.Writeline(i+ “:” + n[i]);
}
Console.Readline();
}
}
4) WAP that reads a line of text containing three words and then replace all the blank space
with an underscore(_)
Solution:
Using System;
Class string
{
public static void Main()
{
String s=” “;
Console.Write(“Enter any 3 Strings”);
S=Console.ReadLine();
s.Replace(“ “,_);
Console.WriteLine(“The new string is” +s);
}
}
5) WAP that reads the name INDIRA GANDHI into two separate string object and then
concatenate them into a new string using
a) + operater,
b) Append method
Solution:
Using System.text;
Using System;
Class string
{
public static void Main()
{
String s1=new StringBuilder(“INDIRA”);
String s2=new StringBuilder(“GANDHI”);
String s3=s1+s2;
Console.WriteLine(“ “+s3);
S3=s1.Appened(“s2”);
Console.WriteLine(“ “+s3);
}
}
PRACTICAL ASSIGNMENT 8
using System;
using System.Collections;
class ArrayList
{
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]);
Console.WriteLine ("The items in the Array List before sorting are : ");
for (int i =0; i< n.Count; i++)
{
Console.Write (i + " : " +n[i]);
}
n.Sort();
Console.WriteLine ("The items in the Array List after sorting are : ");
for (int i =0; i< n.Count; i++)
{
Console.Write (i + " : " +n[i]);
}
Console.ReadLine();
}
}
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
2) Reversing array by creating a method reverse .
using System;
public class ReverseArray
{
public string Reverse(params string [] arr)
{
string [] j;
string [] k;
Console.Write("The array list without reversing is : ");
foreach (int i in arr)
{
Console.Write(" "+i);
j = new string[i]; // Save all the contents in the array 'j'
i++;
}
for (int a = 0; a < j.Length ; a ++)
{
k[a] = j[a]; // Saving the array in another array
}
for (int i = 0; i < j.Length ; i++)
{
j[i] = k[k.Length]; // Here we are reversing the array elements
k.Length --;
}
Console.Write("The reversed array now has : ");
foreach (int i in j)
{
Console.Write(" "+j); // Print the elements of the array 'j'
i++;
}
}
}
using System;
class CountWords
{
public static void Main()
{
string s = "";
Console.Write("Enter the string : ");
s = Console.ReadLine();
string [] words = s.Split(null);
int i = words.Length;
Console.WriteLine("The total number of words in the entered string : "+i);
Console.ReadLine();
}
}
Output:
Enter the string : Daljit is making programs
The total number of words in the entered string : 4
4) 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 : ");
for (int i =0; i< nCount; i++)
{
Console.WriteLine((i+1) + " " +n[i]);
}
Console.WriteLine();
n.Remove(2);
n.Add(3) = "Daljit";
n.Add(5) = "End";
Console.WriteLine ("The items in the Shopping List After modifying are : ");
for (int i =0; i< nCount; i++)
{
Console.WriteLine((i+1) + " " +n[i]);
}
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
PRACTICAL ASSIGNMENT 9
using System;
class SortArray
{
public static void Main()
{
int [] A={127,157,240,550,510};
int [] B={275,157,750,255,150};
int CLength=(A.Length +B.Length);
int [] C=new int[CLength];
int i=0,j=0,k;
Console.Writeline (“Sorted array list : ”);
for(k=0;k<=(i+j);k++)
{
if(A[i]<=B[j])
{
C[k]=A[i];
Console.Write (C[k] + " ");
if(i<4)
{
i++;
}
}
else
{
C[k]=B[j];
Console.Write (C[k] + " ");
if(j<4)
{
j++;
}
}
}
for(i=0;i<CLength;i++)
{
Console.Write(C[i] + " ");
}
Console.ReadLine();
}
}
Output:
Sorted array list : 127 150 157 157 240 255 275 510 550 750
2) Finding largest array element and average of array elements via methods.
using System;
class ArrayFunction
{
public static void Main()
{
long Largest;
double Average;
int c;
int num;
int[] array1;
Console.Write("Enter the number of Elements in an Array : ");
c=int.Parse(Console.ReadLine());
array1=new int[c];
for (int i=0 ; i>c ;i++)
{
Console.WriteLine("Enter the element " + i);
num=int.Parse(Console.ReadLine());
array1[i]=num;
}
foreach (int i in array1)
{
Console.Write(" " + i);
}
Console.WriteLine ();
Largest = Large(array1);
Average = Avg(array1);
Console.WriteLine ("\n The largest element in the array is " + Largest);
Console.WriteLine ("The Average of elements in the array is " +Average);
Console.ReadLine();
}
static int Large (params int [] arr)
{
int temp=0;
for ( int i = 0; i < arr.Length; i++)
{
if (temp <= arr[i])
{
temp = arr[i];
}
}
return(temp);
}
static double Avg (params int [] arr)
{
double sum=0;
for ( int i = 0; i < arr.Length; i++)
{
sum = sum + arr[i];
}
sum = sum/arr.Length;
return(sum);
}
}
Output:
Enter the number of Elements in an Array : 5
Enter the element 1 : 5
Enter the element 2 : 7
Enter the element 3 : 3
Enter the element 4 : 1
Enter the element 5 : 8
largest element in the array is 8
The Average of elements in the array is 4.8
3) Finding the max and min of 3 numbers .
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;
}
}
Console.WriteLine("\n\n The Largest Number = " +largest);
Console.WriteLine("\n The Smallest Number = " +smallest);
Console.ReadLine();
}
}
Output:
Enter No 1 : 15
Enter No 2 : 54
Enter No 3 : 21
The Largest Number = 54
The Smallest Number = 15
4) Calculate standard deviation and mean of the array elements.
using System;
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;
Console.Write("Array List consists of : ");
for (int i = 0; i < n; i ++)
{
Console.Write(nos[i] + " ");
}
for (int i = 0; i < n; i ++)
{
sum = sum + nos[i];
}
for (int i = 0; i < n; i ++)
{
sumofsq = sumofsq + (nos[i]*nos[i]);
}
mean = sum / n;
deviation = Math.Sqrt(sumofsq / 8.0);
Console.WriteLine("\n\n Sum = " +sum);
Console.WriteLine("\n Mean = " +mean);
Console.WriteLine("\n Deviation = " +deviation );
Console.ReadLine();
}
}
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