0% found this document useful (0 votes)
147 views

50 most important programs for computer class 10 Icse

k

Uploaded by

esportsg231
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)
147 views

50 most important programs for computer class 10 Icse

k

Uploaded by

esportsg231
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/ 54
Sa IMPORTANT PROGRAMS | i] im Bt a |) Conditional Constructs A special two-digit number is such that when the sum of its digit is added to the product of its digits, the result is equal to the original two- digit number. Example: Consider the number 59. sum of 5+9=14 Product igits =5*9=45 nots of the sum of digits and product of digits = 14 + 45 = Write a es chgits to accept a two-digit number. Add the sum of its to the product of its digits. If the value is = I to the paarinee input, display the message" “Special 2 jigit number" otherwise, display the message “Not a special two-digit number”. import java.util.Scanner: ublic class SpeciaiNumber public static void main(String args{]) { Scanner in = new Scanner(System.in): System.out print("Enter a 2 digit number: ~); int orgNum = in.nextint(); if (orgNum < 10 |] orgNum > 99) { ‘System.out_printin(“Invalid input. Entered number is not a 2 digit num i Sytiem exit(0}; int num = orgNum; + digit2; te digit2:; int grandSum = digitsum + digitProduct: randsum == OfgNUM) | oie r ystem out print nt Special number”) elseSystern-out printin@Not a special 2-digit number"): Conditional Constructs Write a program that accepts three numbers from the user and displays them either in "Increasing Order" or in “Decreasing Order" as per the user's choice. Choice 1: Ascending order Choice 2: Descending order Sample Inputs: 394, 475, 296 Choice 2 Sample Output First number : 475 Second number: 394 Third number : 296 The numbers are in decreasing order. import java.util Scanner; ublic class BusFare public static vold main(String args[]) { ‘Scanner in = new Scanner(System.in); System.out print("Enter distance travelled: "); Int dist = in.nextint(); ist -10)° else if (dist <= 30) fare = 80 + 60 + (dist - 20) "5; else if (dist > 30) fare = 80 + 60 + 50 + (dist - 30) * 4; System.out.printin("Fare =" + fare); a Conditional Constructs Acourier company charges differently for ‘Ordinary’ booking and ‘Express’ booking based on the weight of the parcel as per the tariff given below: Weight of parcel Ordinary booking. Express booking Up to 100 gm z80 z100 101 to 500 qm Z150 2200 501 gm to 1000 gm 2210 €250 Morethan1000gm_ #2250 2300 Write a program to input weight of a parcel and of booking (o" for ordingry and for rece): caleiate and print the charges accordingly. import java.util. Scanner; public class CourierCompany public static void main(String argsi]) { Scanner in = new Scanner(System in): System.out print("Enter weight of parcel: *); double wt = in.nextDouble(); System.out print( Enter type of booking: char type = in.next().charAt(O): double charge = 0: 100 && type =='0') charge = else if (Wt > 1000 && type =: charge = 300; System.out printin(“Parcel charges = * + charge): } erative constructs Write the programs in Java to find the sum of the following series: (a) S=14+14+24+3+5+.......tonterms import java.util.Scanner; jublic class Series public void computeSeriesSum() { Scanner in = new Tere in); System.out.print(“Enter n: int n = in.nextint(): inta=1,b=1; int sum =a+b; for (int i = 3; i <=; i++) { int term =a+b: sum += term; a=b; b=term: System.out.printin("“Sum=" + sum); Conditional Constructs Writea ne to input three numbers (positive or negative). if they are Lanse then en capi the (oe number otherwise, pave The also displays whether the num! ors entered by hee cord are'All a itive’, ‘All negative’ or ‘Mixed numbers’. import java.util.Scanner; public class NumberAnalysis £ public static void main(String argsi) { Scanner in = new Scanner(System.in); Systomout prntEnter fst number": int a= in.nextint(); System out print("Enter second number:") int b= Innextint System out print(Enter third number:”) inte=in-nextint Int greatestNumber = a: b) 2a ( m.out pri { "Entered numbers are equal”): if (© > greatestNumber) { ‘greatestNumber = b: if (¢> greatestNumber) ( ‘reatestNumber = ¢: ‘System.out printin("The greatest number is " + greatestNumber): #Go3= 0) 88 (o>= 0) 88 (c>=0))( System.out printin(“Entered numbers are all positive numbers."); O18 tem out printing Pntersd nemmbers are all negative numbers." » else S Stem out printin(‘Entered numbers are mixed numbers." y My a Conditional Constructs Atriangle is said to be an ‘Equable Triangle’, if the area of the triangle is equal to its perimeter. Write a program to enter three sides of a triangle. Check and print whether the triangle is equable or not. For example, a right angled triangle with sides 5, 12 and 13 has its area and perimeter both equal to 30. limport java.util. Scanner: public class EquableTriangle { public static vold main(String argsf) { ‘Scanner in = new Scanner(System.in); ‘System.out.printin("Please enter the 3 sides of the triangle."); ‘System.out.print(“Enter the first side: *); double a = in.nextDouble(); ‘System.out.print("Enter the second side: "); double b = in.nextDouble(); ‘System.out.print(“Enter the third side: "): double ¢ = in.nextDouble(); double p=a+b+c; double § = p / 2: double area = Math.sqrt(s * (s - a) * (s - b) * (s - ¢)); if (area == Pp) ystem.out.printin(“Entered triangle is equable."): , elseSystem.out.printin("Entered triangle is not equable.”); r lkerative constructs Wite) the programs in Java to find the sum of the following series: (b)S=2-44+6-8+ import java.util.Scanner; ton ublic class Series public void computeSeriesSum() { Scanner in = new Scanner(System.in); System.out.print("Enter n: "); int n = in.nextint(); int sum = 0; for (inti if (1% 2 sum j=2;i<=n;i++,j =j+2){ 0) { System.out.printin("Sum=" + sum); lterative constructs wires the programs in Java to find the sum of the following rat ce =1+ (142) + (14243) +... + (142434... +n) import java.util.Scanner; ublic class Series public void computeSeriesSum() { Scanner in = new Scanner(System.in); System.out.print("Enter n:"); int n = in.nextint(); inta=1,b=1; intsum=a+b; for (int i= 3; i <=; i++) { int term=a+b; sum += term; a=b; b=term; System.out.printin("Sum=" + sum); Iterative constructs evaite. athe programs in Java to find the sum of the following (d) S=1+ (192) + (192°3) +....... + (1°2*3° ....... 1) import java.util.Scanner; Ublic class Series public void computeSeriesSum() { Scanner in = new Scanner(System.in); System.out.print("Enter n: "); int n = in.nextint(); long sum = 0, term = 1; for (inti i<=n; i++) { term sum += term; System.out.printin("Sum=" + sum); lkerative constructs Write a program to enter two numbers and check whether they are co-prime or not. ave} ee are said to be co-prime, if their HCF is1 one). Sample Input: 14, 15 Sample Output: They are co-prime. import java.util.Scanner; ublic class Coprime public void checkCoprime() { Scanner in = new Scanner(System.in); System.out.print("Enter a: "); int a = in.nextint(); System.out.print("Enter b: "); int b =in.nextint(); int hef = 1; for (int i if Ie % if (nef == 1) System.out.printin(a + ""and"+b+"are co-prime"); elseSystem.out.printin(a + "and "+b +" are not co-prime"); } kerative constructs Write a program to input a number and check and print whether it is a Pronic number or not. [Pronic number is the number which is the product of two consecutive integers.] Examples: import java.util.Scanner; ublic class PronicNumber public void pronicCheck() { Scanner in = new Scanner(System.in); System.out.print("Enter the number to check: "); int num = in.nextint(); boolean isPronic = false; for (int i=1; i <= num -1; i++) { if (i* (i+1 num. isPronic = true; } break; } if (isPronic) System.out.printin(num + " is a pronic number"); elseSystem.out.printin(num + "is not a pronic number"); } : Ff kerative constructs Write a program to input a number and check whether itis a prime number or not. if it is not a prime number then display the next number that is prime. Sample Input: 14 Sample Output: 17 import java.util.Scanner; ublic class PrimeCheck public void primeCheck() { Scanner in = new Scanner(System.in); System.out.print("Enter number: "); int num = in.nextint(); boolean isPrime = true; _ for (int i = 2; num / 2; i++) { if (num % i isPrime = false; break; h } if (isPrime) { . 7 system.out.printin(num +" is a prime number"); ise { for (int newNum = num + 1, newNum <= Integer.MAX_VALUE; newNum++) { isPrime = tru eer / 2; i++) { for (int i = 2; i<= if (newNum % i == isPrime = false; break; } } (serine) { . . ystem.out.printin("Next prime number =" + newNum); ) break; c~ lterative constructs Writea progeny to input a number and check whether itis aHarshad Number or not. [A number is said to be Harshad number, if it is divisible by the sum of its digits. The Pogues displays the message according} ‘or example; Sample lepal it: 132 Sum of digits 6 and 132 is divisible by 6. Output: Itis a Harshad Number. Sample Input: 353 Output: It is not a Harshad Number. import java.util.*; public class HarshadNumber public static void main(String args{]) Scanner in = new Scanner(System .in): System.out.print("Enter a number: °): int num = in.nextint(); int x = num, rem = 0, sum = 0; sao > 2, um + rem: cs x/10: int r= num % sum; if(.== 0) System.out printin(num + "is a Harshad Number."); elseSystem.out printin(num + " is not a Harshad Number."); Ikerative constructs Using a switch statement, write a menu driven program to; (a) generate and display the first 10 terms of the Fibonacci series 0,1,1,2,3,5 The first two Fibonacci numbers are 0 and I, and each ups luent number is the sum of the previous two. (b find the sum of the digits of an integer that is input y the user. Sample Input: 15390 a Sample Output: Sum of the digits = 18 For an incorrect choice, an appropriate error message should be displayed. import java.util.Scdnner; Ublic class FibonacciNDigitsum public static void main(String args[]) { Scanner in = new Scanner(System.in); Syeternouebrinunt2 Fibonacci Series"); System.out.printin("2. Sum of digi System.out.pri med nter your choi int ch = in.nextint(): switch (co) case |: inta=0,b=1, System, out.print(a + "" +b); * iis starting from 3 below * instead of | because we have * already printed 2 terms of * the series. The for loop will * print the series from third * term onwards. for (int i = 3; i <= 10; i++) { int term = a + b; SystemiooL BANK "+ term); a=b; b=term; ea aa ZT iterative constructs Create a menu-driven Program using a switch statement for the tasks below: : () Calculate and show the sum of the series: = XA1- XA2 + XA3 - XA4 + XA5 - ...- XA20 (e) Calculate and display the sum of the series: =1/aA2 + 1/aA4 + 1/aA6 +1/a48 +... up ton terms If an incorrect option is selected, display an appropriate error message. import java.util.Scanner; ublic class SeriesSsum public static void main(String argsf]) { Scanner in = new Scanner(system.In System.out.printin("l. Sum of x“ - x2 + ....- x ; System.out printin("2. Sum of 1/a*2 + 1/a“4 + .... ton terms"); oe inter your choice: "); int ch = in.nextint(); switch (ch) { case I: System.out.print("Enter the value of x: "); intx= Int(); =20;i+4+){ int term = (int)Math.pow(x, i); system.out.printin("Sum =" + sum1); break, zz Iterative constructs Taare import java.util.Scanner; ublic class Series public void computeSeriesSum() { Scanner in = new ‘Scanner(System. in): System.out.print("Enter a: *): int a = in.nextint(); double sum = 0; for (int i = 2;i <= 20;i=i+3){ sum += a / (double)i; System.out.printin("“Sum=" + sum); Nested loops Write a program in Java to enter a number containing three digits or more. Arrange the digits of the entered number in ascending order and display the result. Sample Input: Enter a number 4972 Sample Output: 2, 4, 7,9 import java.util.Scanner; public class DigitSort { public void sortDigits() { Scanner in = new Scanner(System.in); System.out.print("Enter a number having 3 or more digits: "); int OrgNum = in.nextint(); for (int i = O; i <= 9; i++) { int num = OrgNum; int c =O; while (num != 0) { if (num % 10 == i) Crt: for (int j = 1; j <= 0; j++) { , System.out.print(i +", "); } System.out.printin(); vr Nested loops Anumber is said to be Multiple Harshad number, when divided BY the sum of its digits, produces another 'Harshad Number’. rite a program to input a number and check whether it is a jultiple Harshad Number or not. ee When a number is divisible by the sum of its digit, it is called “‘Harshad Number). Sample Input: 6804 Hint: 6804 > 6+8+0+4 378 => 3+7+8= 18 > 378/1 21> 241=3= 21/3=7 Sample Output: Multiple Harshad Number import java.util.Scanner; => 6804/18 = 378 a public class MultipleHarshad public void checkMultipleHarshad() { Scanner in = new Scanner(System.in): System.out print("Enter number to check: "); int num = in.nextint0; int dividend = num: int divisor: int count = 0; while (dividend > 1) { divisor=0: int t = dividend; while (t > 0) { int d= t % 10; divisor += d ) t/=10; if (dividend % divisor == O && divisor != 1) { dividend = dividend / divisor; count++; } else { break; 3 } If (dividend == 1&& count > 1) System.out printin(num + “is Multiple Harshad Number"): else ‘System.out printin(num + "is not Multiple Harshad Number"); on Nested loops Anumberis said to be Multiple Harshad number, when divided By the sum ofits digits, , produces another" Harshad Number". rite a program to input a numba and check whether itis a ther Harshad Number or not When a number is divisible by the sum of its digit, itis called ‘Harshad Number’). Sample input: 6804 Hint: 6804 > 6+8+0+4 = 18 => 6804/18 = 378 378 > 347+8= a eee 2 21> 2+1=3> 21/3=7 Sample Output: Multiple Harshad Number import java.util.Scanner; public class MultipleHarshad Public void checkMultipleHarshad() { Scanner in = new Scanner(System.i in) System.out.print("Enter number to Be oF a nea =i next ne anne Kanes >t mee acer: ee >0){ int d = t % 10; divisor += d; if (dividend % divisor == O && divisor != 1) { dividend = dividend / divisor; count++; } else { break; ) if (dividend == 1 && count > 1) System.out.printin(num +" is Multiple Harshad Number"); else System.out.printin(num +" is not Multiple Harshad Number Nested loops bea the programs to display the following patterns: a 1 31 531 7531 97531 ublic class Pattern public void displayPattern() { for (int i=1; 0; i--) { for (int j = 1; j <= i; j++) { System.out.print(a++ + "\t"); } System.out.printin(); Nested loops Write the programs to display the following patterns: (a 10 101 1010 10101 Ublic class Pattern public void So { inta=1,b=0; for (int i for or 2 == 0) ystem.out.print(b +" "); elseSystem.out.print(a + “"); system.out.printin(); Nested loops ee the programs to display the following patterns: . =H +e Ht “ge ublic class Pattern public void een. { for (int i= = for (int j = 1;j <= i; j++) { if ie a) ystem.out.print("# "); else System.out.print("* "); } System.out.printin(); Nested loops Using the switch statement, write a menu driven program for the following: (a) To print the Floyd's triangle: “Se z a 3 2 Zz ¢ 2 i 3 SE For an incorrect option, an appropriate error message should be displayed. import java.util.Scanner; ublic class Pattern public void choosePattern() Scanner in = new Scanner(System.in); System out printin( Type 1 for Floyd's triangle"): System.out.printin("Type 2 for an ICSE pattern"); System.out.print("Enter your choice: "); int ch = in.nextint(); switch (ch) { case 1: inta for (int for (int System.out.print! ++) +) { ) (a++ + "\t"); System.out.printin(); break; —_ case 2: String s = "ICSE"; for (inti=O;iO8&n< 2 char ch = (char)(n + 64); System.out.printin("Corresponding letter =" + ch); else { System.out.printin("Please enter a number in 1 to 26 range’): a) } =o J. Pl Character Manipulation write a program to input a letter. Find its ASCII code. Reverse the ASCII code and display the equivalent character. Sample input: Y Sample Output: ASCII Code = 89 Reverse the code = 98 Equivalent character: b import java.util.Scanner; public class ASCllReverse public static void main(String argsf[]) { Scanner in = new Si Scanner (System. in); System.out.print("Enter a letter: "); char | = in.next().charAt(0); int a = (int); System.out. ‘printin("ASCll Code ="+a); int r= 0; while (a > 0) { int digit = a % 10; r=r*10 + digit: } a/=10; System.out. printin(Beversed Code ="+ 1); System.out.printin("Equivalent character =" + (char)r); Nested loops tise the programs to display the following patterns: 14131211 10987 54 2 ublic class Pattern public void displayPattern() { int a = 15; for (int i = 5; i > 0; i--) { for (int j =1; : j++) { System.out.print(a-- + "\t"); } System.out.printin(); =a rile Character Manipulation Write a menu driven program to display i) first five upper case letters il) last five lower case letters as per the user's choice. Enter 'l' to display upper case letters and enter '2' to display lower case letters. import java.util.Scanner; public class MenuUpLowCase { public static void main(Strin: orgs{l) { Scanner in = new Senneraye fem.in); me Systemout printing Enter 'T to display upper case jetters"); System.out.printin("Enter '2' to display lower case letters"); System.out.print("Enter your choice: "); int ch = in.nextint(); switch (ch) { case for (int i = 65; i <= 69; i++) System.out.printin((char)i); break; case 2: for (int i = 118; i <= 122; i++) System.out.printin((char)i); break; default: break; t™ Arrays Write a program in Java to store 20 numbers (even and odd numbers) in a Single Dimensional Array DA). Calculate and display the sum of all even cee and all odd numbers separately. import java.util.Scanner; public class SDAOddEvenSum public static void main(String args[]) { Scanner in = new Scanner(System.in); int arr[ ] = new int[20]; System.out.printin("Enter 20 numbers"); for (int i = 0; i < arr.length: i++) { ' arr[i] = in.nextint(); int oddSum = O, evenSum = 0; for (inti=O;i< arr, length; i++) { if (arr[i] % 2 == 0) evenSum rr(il; elseoddSum += arr]: System.out.printin("Sum of Odd numbers = oddsum); System.out.printin("Sum of Even numbers =" + evenSum): } = —__—_——_ Arrays Writea prosran in Java to store 20 temperatures in °F in a Single Dimensional Array (SDA) and display all the temperatures after converting them into °c. Hint: (c/5) = (f- 32) /9 import java.util.Scanner; ublic class SDAF2C public static void main(String args[]) { Scanner in = new Scannerfsystem.in) double arr[] = new double[20]; System.out.printin("Enter 20 temperatures in degree Fahrenheit") for (in 0; i < arr.length; i++) { arr[i] = in.nextDouble(); System.out.printin("Temperatures in degree Celsius"); iy : for (int i = 0; i < arr.length; id double tc = 5 * ((arr[i] - 32) / 9); System.out.printin(tc); Arrays Write a program in Java to store 20 numbers in a Single Dimensional Array (SDA). Display the numbers whic! are prime. import java.util.Scanner; public class PrimeNumbers public static void main(String args[]) { Scanner in = new Scanner(System.in): int arr] = new int[20]: System.out.printin("Enter 20 numbers"); for (int i = O; i < arr.length; i++) { arr[i] .nextint(); System.out.printin("Prime Numbers:"); for (int i = 0; i < arr.length; i++) { fanned oe lee arti ies) Sean: if(arrli] %i==0){ ; c+: } if (c == 2) System.out.print(arr[i] +", "); Character Manipulation write a program in Java to display the following tterns: ty A ab ABC abcd ABCDE public class Pattern public static void main(String args[]) { 5; i < 70; i++) i j4){ i ee ) system.out.print((char)(j+32)); elseSystem.out.print((char)j); System.out.printin(); Character Manipulation write a program in Java to display the following pyorns: ZYXwu ZYXw zYx zy z public class Pattern Ublic static void main(String args| a 86; 1< gun(string ost for (int j = 90; j >= i; j--) { System.out,| Senile ar)j); System.out.printin(); i Arrays Write a program in Java using arrays: (a) To store the Roll No., Name and marks in six ubjects for 100 students. (b) Calculate the percentage of marks obtained by each candidate. maximum marks in each subject are 100. (c) Calculate the Grade as per the given criteria: Percentage MarksGrade From 80 to 100 A From 60 to 79. B From 40 to 59 c Less than 40. D import java.util.Scanner; public class SDAGrades public static void main(String args[]) { final int TOTAL_STUDENTS = 100; Scanner in = new Scanner(System.in); int rollNo[ ]= new int{TOTAL_STUDENTS]: String namef[ ]= new Stringf TOTAL_STUDENTS]; int si[ ]= new int{TOTAL_STUDENTS] int s! new int] TOTAL_STUDENTS]: int s3[ |= new int[TOTAL_STUDENTS]: int s4[ ]= new int( TOTAL STUDENTS]; int sS[ ]= new int{TOTAL_STUDENTS]; int s6[ J= new int{TOTAL_STUDENTS]: double p[ ] = new double[TOTAL_STUDENTS]: char ai new char[TOTAL_STUDENTS]; for (int i = 0; i < TOTAL_STUDENTS: i++) { _ fr System.out.printin("Enter student " + (i+1) + " details:"); PeiNotH ein nextintO: No: "); rollNofi] = in.next in.nextLine(); System.out. Foes: a): name[i] = in.nextLine(Q; chen out. it ‘Subject 1 Marks: "); i] = in.nes sien .out. prinsubject 2 Marks: "); n.nextint oF em.out.prin ("Subject 3 Marks: "); ie in.nextint(): oye em. Rouen “Subject 4 Marks: " In int(); su em.out. print("Subject S Marks: "); ul n.nextint(); System.out. prin ‘Subject 6 Marks: "); [i] = in.nextint( 1] = (((sifi] + s2[i] + s3fi] + s4{i Pll = ail eset as = 3), if (el i] < 40) es + (el < 60) gl a tf (elit < 80) Bisa } System.out.printin(); for (int i = 0; i < TOTAL_STUDENTS; i++) { System. out, printin(rollNofi] +"\t" + namefi] + “stb; —————l$[- Arrays Write a progam: to accept a list of 20 integers. Sort the first 10 numbers in ascending order and next the 10 numbers in descending order by using ‘Bubble Sort’ technique. Finally, print the complete list of integers. import java.util.Scanner; ublic class SDABubbleSort public static void main(String args[]) { Scanner in = new Scanner(System.in); int arr] = new in20y System.out.printin("Enter 20 numbers:"); a eon first half in ascending orderfor (int i = 0; i < arr.length / - 1 its) for (int j = O; j < arr.length / 2 - i - 1; j++ iar > ab +) ie at intt = +1; orl +1] =arrfj]; ) arrjj=t: ) } //Sort second half in descending orderfor (int i = 0; i < arr.length / 2 - 1; i++) { for (int j = arrlength / 2: j < arrlength - i - 1; j++) { if (arf) < arrfj + 1]) { int t= arrfj + 1]; arr[jl: A Hane the final sorted arraySystem.out.printin("\nSorted rray:"); ‘or (int i = O; i < arr.length; i++) { . System out print(ar{i] +"); y ry Strings Write a program to input a sentence. Find and display the following: i) Number of words present in the sentence iil) Number of letters present in the sentence ssume that the sentence has neither include any digit nor a special character. import java.util.Scanner; public class WordsNLetters public static void main(String args[]) { Scanner in = new SESE OF System.out.printin("Enter a sentence:"); String str = in.nextLine(); int wCount = O, ICount = O; int len = str.length(); for (int i = O; i < len; i++) { char c str.charAt(i); if (ch =="') wCount++; elselCount++; System.out.printin("No. of words =" + wCount); System.out.printin("No. of letters =" + Count); —/ ~ain Strings Writea programm in Java to accept a word/a String and display the new string after removing all the vowels resent in it. ample Input: COMPUTER APPLICATIONS Sample Output: CMPTR PPLCTNS import java.util.Scanner; public class VowelRemoval public static void main(String args[]) { Scanner in = new Scannerteys em.in); oi ous rintin("Enter a word or sentence:”); String str = in.nextLine(): int len = str.length(): String newStr =""; for (int i = 0: i < len: i++) { char ch = Character.toUpperCase(str.charAt(i)); { continue; newStr = newStr + ch; System.out.printin("String with vowels removed"); System.out.printin(newStr); — J tititi‘“(i‘CstsCsCC Strings Write a program in Java to accept a name(Containing three words) and display only the initials (i.e., first letter of each word). Sample Input: LAL KRISHNA ADVANI Sample Output: LK A import java.util.Scanner; public class Namelinitials public static void main(String argsf[]) { Scanner in = new Scanner(System.in); System.out.printin("Enter a name of 3 or more words:"); String str = in.nextLine(); int len = str.lengthQ: System.out.print(str.charAt(O) +" "); for (inti= RD char if (ch =="') char ch2 = str.charAt(i + 1): System.out.print(ch2 + ""); Strings Write a program in Java to accept a name containing three words and display the surname first, followed by the first and middie names. Sample Input: MOHANDAS KARAMCHAND GANDHI Sample Output: GANDHI MOHANDAS KARAMCHAND import java.util.Scanner; ublic class SurnameFirst ublic static void main(String args| Scanner in =new Sonnet Syste) SyStern aE panne tes a name of 5 words:”); String name = in.nextLine(); Int lastSpaceldx = name.lastindexOf(’ '); String surname = name.substring(lastSpaceldx + 1); String initialName = name.substring(0O, lastSpaceldx); System.out.printin(surname + "" + initialName); oo Arrays write eprom in Java to accept 20 numbers ina single dimensional array arr[20]. Transfer and store all the even numbers in an earay en| | and all the odd numbers in another array aT |. Finally, print the elements of both the arrays. import java.util.Scanner; ublic class SDAEvenOdd public static void main(String args[]) { final int NUM_COUNT = 20; Scanner in = new Scanner(System.in); inti=0; int arr[] = new intr URL COUNT: int even{] = new int! COU! int odd[J= = new int! uM aa System.out, Ea ‘Enter 20 numbers:"); |UM_COUNT; i+#) { -nextint(); if (arrf[i. = even) eldix++] = arr]; elseodd[oldx+#] = arr[i]; aren out.printin(“Even Numbers: for (i = 0; i < eldx; i++) { System.out,| print(even{i] eo): eens out. Bun vow Numbers:"); for (i = O; i < oldx; ial } System.out, print( id[i] +""); 2 = Strings Write a program in Java to accept a String in uy case and replace all the vowels present in the String with Asterisk (*) sign. Sample Input: “TATA STEEL IS IN JAMSHEDPUR™ Sample output: T*T* ST**L *s *N J*MSH*DP*R import java.util.Scanner; ublic class VowelReplace public static void aera args[]) { : % i Scanner in = new Scanner(System.in); System.out.printin("Enter a string in in.nextLine(); String newStr =""; int len = str.length(); for (int i = O; i < len; i++) { str.charAt(i); newStr = newStr + “"; else newStr = newStr + ch; } System.out.printin(newStr); f= Strings Write a program in Java to enter a sentence. Frame a word by joining all the first characters of each word of the sentence. Display the word. Sample Input: Vital Information Resource Under Seize Sample Output: VIRUS import java.util.Scanner; ublic class FrameWord public static void SIE SUIDG args[]) { Scanner in = new Scanner(System.in); System.out.printin("Enter a sentence:"); String str = in.nextLine(); String word = "" + str.charAt(0); int len = str.length(); for (int i = 0; i < len; i++) { char tr.charAt(i); if (ch word erat +1); System.out.printIn(word); = a Arrays A double dimensional array is defined as N[4][4] to store numbers. Wiied @ program to find the sum of all even numbers and product of all odd numbers of the elements stored in Double Dimensional Array (BDAY. import java.util.Scanner; Ublic class DDAEvenOdd public static void main(String args[]) { Scanner in = new Scanner(System.in); int NO = new intaes 114]: long evenSum. dd Prod = 1; System.out.printl for (int i++) { for (i < 4: j++) { NI in.-nextint(); if U == Si aren um += N{i[i): OddProd *= N{i][j]; } } System.out.printin("Sum of all even numbers = "+ eyensum); a Se out. printin("Product of all odd ee = oO } ms Strings Write a program to input a sentence. Create a new sentence by lacing each consonant with the previous letter. If the previous letter is a vowel then replace it with the next letter ., if the letter is B then replace it with C as the previous letter of Bis A). Other characters must remain the same. Display the new sentence. Sample input |: ICC WORLD C! Sample Output :IBB VOQKC BUQ import java.util.Scanner; public class ReplaceLetters public static void main(String args[]) { Scanner in = new Scanner(System.in); System.out printin("Enter a sentence: "); String str = in.nextLine(); int len = str-length(); String newStr ="; for (int i=0;i< len; Ge) 1); char ch = str.charAtl Character.toUpperCase(ch); newStr + ch; } else { char prevChar = (char)(ch - 1); char prevCharUp = Character.toUpperCase(prevChar); if (prevCharUp prevCharUp =='E' prevCharU| prevCharU| prevCharUp == 'U') { newStr = newStr + (char)(ch +1); } else { newStr = newStr + prevChar; } } System.out.printin(newStr); Strings Write a program to generate a triangle or an inverted triangle based upon User's choice. Example Input: Type 1 for a triangle and Type 2 for an inverted triangle Enter your choice 1 Enter a word : BLUEJ Sample Output: import java.util. Scanner: public class TriangleMenu public static void main(String args[]) { Scanner in = new Scanner erate System.out.printin("Type I for a triangle and"); System.out.printin("Type 2 for an inverted triangle’ System.out print("Enter your choice: "); int choice = in.nextint(); In.nextLine(); System.out.print("Enter a word: "); String word = in.nextLine(); int len = word.length(); switch (choice) { for(int | = 0; for(int = 0; j < +) { System.cut. ei charAt(i)); system.out.printin(); reak; i len; hae cored tls for(int +) { Syotdm.cut prlitiward.charAt()) ystem.out.printin(): reak; default: System.out printin("Incorrect choice"); break; ?’ - Strings writea promam in Java to enter a String/Sentence and display the longest word and the length of the longest word present in the String. Sample Input: “TATA FOOTBALL ACADEMY WILL PLAY AGAINST MOHAN BAGAN” Sample Output: The longest word: FOOTBALL: The length of the word: 8 import java.util.Scanner: ublic class LongestWord public static void main(String args[]}) { Scanner in = new Scanner(System.in); eysious out.printin(“Enter a word or sentence"): String str = in.nextLine(); str += Word = int len = str.length(); i < len; i++) Succnaray i); "; //Add space at end of stringString word = "", for (inti char cl if (ch if (word.length() > IWord.length(Q)) lord = word, word else { word += ch: } } pyaar e longest word: " + [Word + } " The length of the word: " + Word.length()): Strings Write a program in Java to accept a word and display the ASCII code of each character of the word. Sample Input: BLUEJ Sample Output: ASCil of B= 66 ASCIl of L=76 ASCIl of U=85 ASCIl of E=69 ASCII of J=74 import java.util.Scanner; ublic class ASCIICode public static void main(String args[]) { Scanner in = new Scanner(System.in); System.out.printin("Enter a word:"); String word = in.nextLine(); int len = word.length(); for (int i = 0; i < len; i++) { char ch = word.charAt(i); System.out.printin("ASCIl of "+ ch +"="+ (int)ch); Strings Write a program in Java to enter a sentence. Display the words which are ony (poenrcne: Sample Input: MOM AND DAD ARE NOT AT HOME Sample Output: MOM DAD import java.util. Scanner; ublic class PalinWords public static void panlaning args[]) { Scanner in = new Scanner(System.in); System.out.printin("Enter a sentence:”); String str str=str+ String word =""; int len = str.length(): for (inti= int wordLen = word.length(); boolean isPalin = true; for (int j = 0; j < wordLen / 2 j++) { if (word.charAt(j) !=word.charAt(wordLen - 1 - j)) { isPalin = false; break; } } if (isPalin ‘System.out.printin(word); word =""; Maal word += ch; ——_“3=>- Strings Write a prog to accept a word and convert it into lower case, if itis in upper case, Display the new word by replacing only the vowels with the letter following it. Sample Input: computer Sample Output: cpmpvtfr import java.util.Scanner: Ublic class VowelReplace ic rosf]) { Scanner in = new Scanner| em.in): System.out.print("Enter a word: "); String str = in.nextLine(): str = str.toLowerCase(); String newStr = int len = strlength(); public static void Pots Gg y' for (int i= O; i < len: i++ char ch = str.charAt(i); if (str.charAt str.charAt(i str.charAt(i str.charAt(i str.charAt(i, u') char nextChar = (char)(ch + 1); newStr = newStr + nextChar, ( ll } else { newStr = newStr + ch; } } System.out.printin(newStr):

You might also like