You are on page 1of 29

Q1. Write a program to print C# Programming on screen.

PROGRAM :using System; class abc { public static void Main() { Console.WriteLine("This is my First C# program"); } }

Output:-

Q2. Write a program to add two numbers. PROGRAM :using System; class abc1 { public static void Main() { int a,b; Console.WriteLine("Enter the first number=> "); a=int.Parse(Console.ReadLine()); Console.WriteLine("Enter the second number=> "); b=int.Parse(Console.ReadLine()); Console.WriteLine("The addition => "+(a+b)); } }

Output:-

Q3. Write a program of Fibonacci series. PROGRAM :using System; class abc2 { public static void Main() { int n,a=0,b=1,temp; Console.WriteLine("Enter the Limits = "); n=int.Parse(Console.ReadLine()); for(int i=1;i<=n; i++) { temp=a; a=b; b=temp+a; Console.WriteLine(a); } } }

Output:-

Q4. Write a program to find even and odd number. PROGRAM :using System; class abc3 { public static void Main() { int n; Console.WriteLine("Enter the number = "); n=int.Parse(Console.ReadLine());

if(n%2==0) Console.WriteLine(n +" is an even number"); else Console.WriteLine(n +" is not an even number"); } }

Output:-

Q5. Write a program to add marks of student in five subjects and display their his/her details in the following format : Name : Class : Total Marks : Percentage : PROGRAM :using System; class abc4 { public static void Main() { string nm,cls; int []marks = new int[5]; int tm=0; double per; Console.WriteLine("Enter your name = "); nm=Console.ReadLine(); Console.WriteLine("Enter your Class = "); cls=Console.ReadLine(); for(int i=0; i<5; i++) { Console.WriteLine("Enter the marks of "+(i+1)+" subject = "); marks[i]=int.Parse(Console.ReadLine()); tm=tm+marks[i];

} per=tm/5; Console.WriteLine("Your details are = "); Console.WriteLine("Name : "+nm); Console.WriteLine("Class : "+cls); Console.WriteLine("Total Marks : "+tm); Console.WriteLine("Percentage : "+per); } }

Output:-

Q6. Write a program for the following in an interactive manner 1. Prime Number 2. Factorial 3. Table Generation 4. Exit PROGRAM :using System; class abc { public void primeno() { int n,r,s=0; Console.WriteLine("Enter the number = "); n=int.Parse(Console.ReadLine()); if(n==2) Console.WriteLine(n+" is a prime number."); else { for(int i=2; i<n; i++) { r=n%i; if(r==0) s++; }

if(s==0) Console.WriteLine(n+" is a prime number."); else Console.WriteLine(n+" is not a prime number."); } } public void facto() { int n,fact=1; Console.WriteLine("Enter the number = "); n=int.Parse(Console.ReadLine()); for(int i=1; i<=n; i++) { fact=fact*i; } Console.WriteLine("Factorial = "+fact); } public void tabgen() { int n; Console.WriteLine("Enter the number = "); n=int.Parse(Console.ReadLine()); Console.WriteLine("The table of "+n+ " ="); for(int i=1; i<=10; i++)

{ Console.WriteLine(n+" * "+i+" = "+(n*i)); } } } class inter { public static void Main() { abc obj= new abc(); Console.WriteLine("Enter your choice = "); Console.WriteLine("1. Prime Number."); Console.WriteLine("2. Factorial."); Console.WriteLine("3. Table Generation."); Console.WriteLine("4. Exit."); int ch=int.Parse(Console.ReadLine()); switch(ch) { case 1: obj.primeno(); break; case 2: obj.facto(); break; case 3: obj.tabgen(); break; case 4: return; default: Console.WriteLine("Wrong Choice.");

break; } } }

Output:-

Q7. Write a program to print following output 1 22 333 4444 55555 PROGRAM :using System; class abc { public static void Main() {

for(int i=1; i<=5; i++) { for(int j=1; j<=i; j++) { Console.Write(i); } Console.WriteLine(); } } }

Q8. Write a program to find whether a given number is Armstrong or not. PROGRAM :using System; class abc { public static void Main() { int n,k,r,d,sum=0;

Console.WriteLine("Enter the number = "); n=int.Parse(Console.ReadLine()); k=n; while(n!=0) { r=n%10; d=r*r*r; sum=sum+d; n=n/10; } if(sum==k) Console.WriteLine(k+" is an armstrong number."); else Console.WriteLine(k+" is not an armstrong number."); } }

Output:-

Q9. Write a program to input salary of 2 employees and find the enhanced salary of employees with 15% raise in their salaries. PROGRAM :using System; class sonu { public static void Main() { int []empid = new int[10]; double []empsal = new double[10]; for(int i=0; i<2; i++) { Console.WriteLine("Enter the employee id of "+(i+1)+" employee"); empid[i]=int.Parse(Console.ReadLine()); Console.WriteLine("Enter the Salary of "+(i+1)+" employee"); empsal[i]=int.Parse(Console.ReadLine()); } Console.WriteLine("The employee Id, Salary and Increased Salary are:- "); Console.WriteLine("(Empid)\t(salary)(Salary(after increased))"); for(int i=0; i<2; i++) { Console.WriteLine(empid[i] + "\t" +empsal[i] + "\t" +(empsal[i]+(empsal[i]*0.15))); } } }

Output:-

Q-10- Define a class library which includes of the following data members. Title_book Author B_code Date_issued Date_returned Member Functions: Read_data Compute_data Show_data

If the library account holder did not returned the book within 7 days from the date the book issued, then compute fine @ Rs. 20 per day and show the record. PROGRAM :using System; class Library

{String Book_Title; String Author; int B_Code,diff,fine; int[] d_i=new int[3]; int[] d_r=new int[3]; public void Read_Data() {Console.WriteLine("enter the title of the book "); Book_Title=Console.ReadLine(); Console.WriteLine("enter the author of the book "); Author=Console.ReadLine(); Console.WriteLine("enter the code of the book "); B_Code=int.Parse(Console.ReadLine()); Console.WriteLine("enter the issue date:"); Console.WriteLine("enter day"); d_i[0]=int.Parse(Console.ReadLine()); Console.WriteLine("enter month"); d_i[1]=int.Parse(Console.ReadLine()); Console.WriteLine("enter year"); d_i[2]=int.Parse(Console.ReadLine()); Console.WriteLine("enter the Return date:"); Console.WriteLine("enter day"); d_r[0]=int.Parse(Console.ReadLine()); Console.WriteLine("enter month"); d_r[1]=int.Parse(Console.ReadLine());

Console.WriteLine("enter year"); d_r[2]=int.Parse(Console.ReadLine()); } public void Compute_Data() { if(d_i[0]>d_r[0]) diff=d_i[0]-d_r[0]; else diff=d_i[0]-d_r[0]; if(diff<0) diff=diff*-1; if(diff>7) {fine=diff*20; } } public void Show_Data() {Console.WriteLine(); Console.WriteLine(); Console.WriteLine("The Details are:"); Console.WriteLine("Book Title "+Book_Title); Console.WriteLine("Author "+Author); Console.WriteLine("Book Code "+B_Code); Console.WriteLine("Book Issued in dd-mm-yyyy format:"+d_i[0]+"-"+d_i[1]+"-"+d_i[2]); Console.WriteLine("Book Returned in dd-mm-yyyy format:"+d_r[0]+"-"+d_r[1]+"-"+d_r[2]);

Console.WriteLine("Fine Generated "+fine); } } class LibraryDemo { static void Main() { Library l=new Library(); l.Read_Data(); l.Compute_Data(); l.Show_Data(); } }

Output:-

Q11.Mars is approx 34 million miles away from earth. Assume there is someone on mars you want to talk with, what is the delay between the time

a radio signal leaves earth and the time it arrives on mars. Suppose that the light travel approx 18600 miles per second. Now compute the delay, you will need to divide the distance by the speed of light. Display the delay in terms of second and minutes? Program: using System; class distance { public static void Main() { double delay; double d; delay=34/18600; Console.WriteLine(delay); d=delay/(60*60); Console.WriteLine("the delay in terms of second & minutes:"+d); } }

Output:-

Q12. Write a program that display the truth table for C#s logical operator (i.e. AND,OR,XOR,NOT) PROGRAM :// Truth Table using System; class sonu { public void AND() { Console.WriteLine("The truth table of \"AND\" is"); Console.WriteLine(); Console.WriteLine("A \t B \t Output"); Console.WriteLine(); Console.WriteLine("0 \t 0 \t 0"); Console.WriteLine("0 \t 1 \t 0"); Console.WriteLine("1 \t 0 \t 0"); Console.WriteLine("1 \t 1 \t 1"); } public void OR() { Console.WriteLine("The truth table of \"OR\" is"); Console.WriteLine(); Console.WriteLine("A \t B \t Output"); Console.WriteLine(); Console.WriteLine("0 \t 0 \t 0");

Console.WriteLine("0 \t 1 \t 1"); Console.WriteLine("1 \t 0 \t 1"); Console.WriteLine("1 \t 1 \t 1"); } public void XOR() { Console.WriteLine("The truth table of \"XOR\" is"); Console.WriteLine(); Console.WriteLine("A \t B \t Output"); Console.WriteLine(); Console.WriteLine("0 \t 0 \t 0"); Console.WriteLine("0 \t 1 \t 1"); Console.WriteLine("1 \t 0 \t 1"); Console.WriteLine("1 \t 1 \t 0"); } public void NOT() { Console.WriteLine("The truth table of \"NOT\" is"); Console.WriteLine(); Console.WriteLine("A \t Output"); Console.WriteLine(); Console.WriteLine("0 \t 1"); Console.WriteLine("1 \t 0"); } }

class truth { public static void Main() { int ch; sonu obj = new sonu(); Console.WriteLine("MAIN MENU: "); Console.WriteLine("1.AND truth table"); Console.WriteLine("2.OR truth table"); Console.WriteLine("3.XOR truth table"); Console.WriteLine("4.NOT truth table"); Console.WriteLine("5.EXIT"); Console.WriteLine("Enetr your choice(1-5)"); ch=int.Parse(Console.ReadLine()); switch(ch) { case 1: obj.AND(); break; case 2: obj.OR(); break; case 3: obj.XOR(); break; case 4: obj.NOT(); break; case 5: return; default: Console.WriteLine("Invalid Choice."); break; }}}

Output:-

Q13. Input the principal, the length of time, number of payments per year and the interest rate from user. Compute the regular payments on a loan by using following formula:

Payment= IntRate*(Principal/pay per year) ____________________________ 1-((IntRate/PayperYear)+1)Payperyear*Numyears PROGRAM :using System; class sonu { public static void Main() { int p,t,py; double r,a,b,c,c1,d; Console.WriteLine("Enter the Principal : ");

p=int.Parse(Console.ReadLine()); Console.WriteLine("Enter the Time Interval : "); t=int.Parse(Console.ReadLine()); Console.WriteLine("Enter the no. of Payments/year : "); py=int.Parse(Console.ReadLine()); Console.WriteLine("Enter the Rate : "); r=int.Parse(Console.ReadLine()); r=r/100; a=r*p/py; b=(r/py)+1; c=py*t; c1=Math.Pow(b,c); d=1-(a/c1); Console.WriteLine("The regular payments on a loan is : "+d); } }

Output:-

Q14. W.A.P of Bubble Sort. PROGRAM :// Bubble Sort using System; class sonu { public static void Main() { int i, n; int [] a=new int [20]; int j; int k=1; int temp; bool exchange_flag=true; Console.WriteLine("input size of array:"); n=int.Parse(Console.ReadLine( )); Console.WriteLine("enter {0} elements of array",n); for(i=0;i<n;i++) a[i]=int.Parse(Console.ReadLine( )); while( (k<n)) { while(exchange_flag==true) { exchange_flag=false;

for(j=0;j<n-k;j++) { if(a[j]>a[j+1]) { exchange_flag=true; temp=a[j]; a[j]=a[j+1]; a[j+1]=temp; } k++; } Console.WriteLine("sorted list of elements are:"); for(i=0;i<n;i++) { Console.Write(a[i]); Console.Write("\n"); } } } } }

Output:-

Q15.WRITE A PROGRAM that reads charcter from the keyboard. Convert lower case letter to uppercase and all uppercase characters to lowercase, display the result make no changes to any other character. The program should stop when the user presses the period key. PROGRAM :using System; class sonu { public static void Main() { string str; string str1=""; Console.WriteLine("Enter some characters."); str = Console.ReadLine(); Console.WriteLine("You entered: " + str); foreach(char ch in str) { if(Char.IsLower(ch)) str1+=Char.ToUpper(ch); else str1+=Char.ToLower(ch); } Console.WriteLine("You formatted new string is: " + str1); } }

Output:-

Q16. WRITE A PROGRAM that uses an array to find the average of ten double values. Use any double values. PROGRAM :// Find average any 10 double value using System; class sonu { public static void Main() { double []val = new double[10]; double sum=0.0,avg; Console.WriteLine("Enter any 10 double values : "); for(int i=0; i<10; i++) { val[i]=Double.Parse(Console.ReadLine()); sum=sum+val[i]; }

avg=sum/10; Console.WriteLine("Average = "+avg); } }

Output:-

Q17. Write a recursive method that display the content of a string backwards. PROGRAM :using System; class sonu { public void Reverse(String str) { if(str.Length>0) Reverse(str.Substring(1,str.Length-1)); else return; Console.Write(str[0]);

} } class Recursive { static void Main() {string s; sonu r=new sonu(); Console.WriteLine("enter a string "); s=Console.ReadLine(); Console.WriteLine(); Console.WriteLine("String before recursion "); Console.WriteLine(s); Console.WriteLine(); Console.WriteLine("String after recursion "); r.Reverse(s); } }

Output:-

You might also like