0% found this document useful (0 votes)
15 views9 pages

Computer Science

allabout computing , hacking ai

Uploaded by

Dakshu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
15 views9 pages

Computer Science

allabout computing , hacking ai

Uploaded by

Dakshu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 9
Program to compare two numbers. Program to check whether an integer is an even number or 19 odd number. io | 11 | Program to calculate commission based on sales, ce Program to read a 4-digit year and test whether it is a leap 12 year or not. 13 __ | Program that reads a string and tests whether it begins with a vowel. Program based on if 9. Program to compare two numbers and display which of them is greater or whether they are equal. class Compare {static void main( ) if{num| > num2) {System.out printin(num! + " is greater than " + num2);} if{num! =10000) {comm=sales*0.2; System.out.printIn("Commission is "+comm);} else if(sales<10000 && sales>=5000) {comm=sales*0.1; System.out.printin("Commission is "+comm);} else System.out.println("No Commission"); } static void main( ) £ sales(10000); sales(5000); sales(4000); }} Variable Description: i | Name of the Variable Data Type Purpose/Description sales double _[ Parameter input. | comm double _ [To store commission. ‘Output: Commission is 2000.0 Commission is 500.0 No Commission Program based on nested if 12. Program to read a 4-digit year and test whether it is a leap year or not. import java.util.*; class Leapyear{ static void main( ) { Scanner sc=new Scanner(System.in); int year; System.out.printIn("Enter a 4-digit ye year=se.nextint(); if ( year % 100 = if (year % 40 ) System.out printin("Year " + year +" is a leap year."); else System.out.printin("Year " + year +" is not a leap year."); } else {if (year % 4==0) System.out.printin(" Year " + year +" is a leap year."); else System.out.printIn("Year "+ year +" is not a leap year."); } }} OE Variable Description: ea) sR Name of the Variable | Data Type Purpose/Description | year int | To store a 4-digit year. Output: Enter a 4-digit year: 2006 Year 2006 is not a leap year. Output2: Enter a 4-digit year: 1900 Year 1900 is not a leap year. Output2: Enter a 4-digit year: 2000 Year 2000 is a leap year, 10 Program on switch case ee 13. Program that reads a string and tests whether it begins with a vowel. import java.util."; class Text { static void main( ){ String line; Scanner se = new Scanner(System.in) ; System.out.printin ("Enter a string: "); line = se.nextLine ( ); char ch = line.charAt(0); switeh(ch) { case 'A': case 'E': case 'T': case 'O' case 'U' case ‘a’: +: System.out.printta(" String begins with an uppercase vowel."); break; case 'e’: case '': case 'o': case 'u'; System.out printin(" String begins with a lowercase vowel."); break; default: System.out.printin(" String begins with a consonant. "); }}} Variable Description: Name of the Variable | Data Type | Purpose/Description ch char To store a character. | line | String | To store a string. Output! : Enter a string: Umesh String begins with an uppercase vowel. Output2: Enter a string: umesh String begins with a lowercase vowel. Output3: Enter a string: Bengaluru String begins with a consonant. = T lors ie likeO 3 6. ___| Program to print fist § terms of Fibonacci se Program to accept 8 spy number or not. Ss. umber, check and display whether itis | oT _| Program to print the pattem, Program to generate 10 mul Itiples of numbers from I to the limit input by the user. 20 | Menu driven program, Program based on looping statement. 14. Program using for loop to print numbers from 1 to 5, class Test { void printNos( ) { int i=0: for(i=1; i<=5; +4i) System.out.printin(i); 7B] Variable Description: Name of the Variable | Data Type Purpose/Description | i int | for loop variable | utput: ween on Program based on printing simple series. 15. Program to print numbers between 0 and 20 with a class Numbers { static void main( ) {for(int i=0; i<20; i+=3) System.out.print(i + "\t"); }} Variable Description: gapof3ieliked 3 6,, Name of the Variable i Data Type Purpose/Description int for loop variable eg 69. AIS IBS cy 16, Program to print first 5 terms of Fibonac class Number { static void Fibonacci( ) {int f=0, s=1, t, System.out.print(f + “\t"+ s); while(n<=5) { t=fts; System.out.print(“\t"+t); fs; st; n++3}}} Variable Description: Name of the Variable Data Type Purpose/Description f To store first number s int To store second number t To store sum of 2 preceding numbers a while loop variable Output: Osea lpesgals He 2eiee 17. Program to accept a number, check and display whether it is a spy number or not. (A number is spy if the sum of its digits equals the product of its digits. Example: consider the number 1124, Sum of the digits= 1+ 1+2+4=8 Product of the digits = 1 x 1 x2x4=8 class SpyNumber { void print (int n) {int d, s=0, p=1; while (n>0) { d=n%10; s+=d; p*=d; n=n/10;} System.out printin( "It is a spy number."); else System.out printin( "It is not a spy number.");} } ‘variable Description: 13 ime of the Variable Data Type Purpose/Description n Parameter input d int To store a digit P To store product of digits s To store sum of digits Output! : (n =1124) It is a spy number. Program based on nested for loop 8, Program to print the pattem. class Nested {static void main( ) { inti, js for (i=1; i <= 5; i++) { for (JI j <= j+4) {System.out.print( i + "\¢");} System.out.println( );}}} Output2; (n =124) Itis not a spy number. Variable Description: ‘Name of the Variable [ Data Type Purpose/Description ij int__ [for loop variables Output 1 oer) FaeeeS Aged iyald eA, 5 eet 5 on Sees 19. Program to generate 10 multiples of numbers from | to the limit input by the user. import java.util.*; class Mult_Table{ static void main( ){ Scanner s=new Scanner(System.in); int num, i,j; System.out.println("Enter the limit:"); .nextint( ); 3 i<=num; i++) { for (j=1; j <=10; j++) 4 {System.out.print( i*j + "\")s) System.out.println( ):}}} Name of the Variable Data Type Purpose/Description num int Tostore the limit, | ij int for loop variables Output: Enter the V2 8 Aa eS 6 Tee Roe 10 2 ae 6B AO 12. 5 14 16 182900 3 12-215. 18: Qh 24» 4275830 Menu driven program. 20. Program to display menu for areas of different shapes and then calculate area of selected shape. import java.util. Scanner; class Menu { static void main( ) { int a, b, area=0, choice; Scanner s = new Scanner(System.in); System.out.printIn("Shapes' Area Menu"); System.out.println("1. Square"); System.out.println("2. Rectangle"); System.out.println("3. Exit"); System.out.printin("Enter your choice 1 to choice=s.nextlnt( ); switch(choice) { case I: System.out.printin("Enter side:"); a=s.nextInt( ); area =a * a; break; case 2: System.out.printIn("Enter length and breadth:"); a=s.nextInt(); b=s.nextInt( ); area =a * b; break; case 3: System.exit(0); default: System.out printIn("Valid choices are I to 3");} System.out printin("Area=" + area);} } variable Description: 1s Name of the Variable Data Type Purpose/Description a int | To store the value of side and length b int To store the breadth of rectangle area int___|To store the area of shape choice int [To store the choice Output Shapes’ Area Menu 1. Square 2. Rectangle 3. Exit Enter your choice 1 to 3: 1 Enter side: 10 Area=100 Output2: Shapes’ Area Menu 1. Square 2. Rectangle 3. Exit Enter your choice | to 3: 2 Enter length and breadth: 10 20 Area=200 Outputs: Shapes’ Area Menu 1. Square 2. Rectangle 3. Exit Enter your choice I to 3: 4 Valid choices are | to 3

You might also like