Practical Assignment No. 1 1) Write a program to read two strings from keyboard using the readline statement and display them on one line using two write statements ?? Ans Using System; Class String; { public static void main() { String s1="" , s2=""; s1 = Console.ReadLine(); s2 = Console.ReadLine(); Console.write(s1); Console.write(s2); } } 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); } } 3) Write a progrma to display following pattern
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Practical Assignment No. 1
1) Write a program to read two strings from keyboard using the readline statement and display them on one line using two write statements ??
Ans Using System;Class String;{public static void main(){String s1="" , s2="";s1 = Console.ReadLine();s2 = Console.ReadLine();Console.write(s1);Console.write(s2);
}}
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);}}
3) Write a progrma to display following pattern
** * *
* * * * ** * *
*
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();}}
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 ?
1) WAP to implement the following:int m=100;int n=200;long l=min;Console.WriteLine(“l= “);If the output is an error message then correct the error & execute the program.
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.
2) What is the output of the following program.
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));
4) WAP to compare & display the average of the number.
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
1) Print the pattern : *
* * * * *
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();}}}
2) Print the pattern : * * *
* * * * *
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();}}}}
3) Print the pattern : * * *
* * *
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();}}}
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+" ");}elseb=b*(i-j+1)/j;console.write(b+" ");}}
console.writeline();}}}
Print the pattern : Mumbai
Mumba Mumb Mum Mu M
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 2 2 3 3 3 4 4 4 4 5 5 5 5 5
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 ();}}}
using System;class Cpublic 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;}}}
3) Write a program that compute table of factorial of numbers.
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 ();}}
4) Write a program to compute the sum of digits of a given integer of a number.
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 % 10sum = sum + rem;num = num / 10;}Console.Writeline (“Sum = ” + sum);
Console.Readline ();}}
5) Wap to print Fibonacci series.
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);}
6) Wap to evaluate the following investment equation. V = P (l + r) 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();}
}
2) WAP to find solution of linear equation.
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();
}}
3) WAP for list of marks ranging from 0 to 100.a) in the range of 81 to 100.b) in the range of 61 to 80.c) in the range of 41 to 80.d) in the range of 0 to 40.
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);
}}
4) Admission to a professional course.a. marks in mathematics >=60.b. marks in physics >=50c. marks in chemistry >=40d. total in all 3 sub. >=200e. total in maths and physics >=150.
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”);}}}
5) WAP to print Floyd’s triangle
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.1FConsole.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-discConsole.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.15FConsole.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.”);}}}
7) WAP that will read the value of X and evaluate following Y= {1 for x>0 {0 for x=0 {-1 for x<0 Using a nested If statement
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
1) WAP to find maximum and minimum of 3 numbers entered.
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.
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);}
}}
3) WAP that counts the number of words in a text document.
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++;
}
}
}
3) Counting number of words in a string .
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 RajuThe items in the Shopping List After modifying are : Karan Girish Raju Daljit End
PRACTICAL ASSIGNMENT 9
1) SORTING 2 ARRAYS AND MERGING INTO 1.
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++) {
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 : 5Enter the element 2 : 7Enter the element 3 : 3Enter the element 4 : 1Enter the element 5 : 8 largest element in the array is 8The 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 : 15Enter No 2 : 54Enter No 3 : 21 The Largest Number = 54The 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(); }}