50 Most Important Programs
50 Most Important Programs
Knowledge
50 Most
Important
programs
ICSE 9 & 10
Code is Easy
Clarify
Knowledge
Conditional
Constructs
import java.util.Scanner;
public class NumberAnalysis
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter first number: ");
int a = in.nextInt();
System.out.print("Enter second number: ");
int b = in.nextInt();
System.out.print("Enter third number: ");
int c = in.nextInt();
int greatestNumber = a;
if ((a == b) && (b == c)) {
System.out.println("Entered numbers are equal.");
}
else {
if (b > greatestNumber) {
greatestNumber = b;
}
if (c > greatestNumber) {
greatestNumber = c;
}
System.out.println("The greatest number is " + greatestNumber);
if ((a >= 0) && (b >= 0) && (c >= 0)) {
System.out.println("Entered numbers are all positive numbers.");
}
else if((a < 0) && (b < 0) && (c < 0)) {
System.out.println("Entered numbers are all negative numbers.");
}
else {
System.out.println("Entered numbers are mixed numbers.");
}
}
}
}
Clarify
Knowledge
Conditional
Constructs
Conditional
Constructs
Conditional
Constructs
Conditional
Constructs
import java.util.Scanner;
public class CourierCompany
{
public static void main(String args[]) {
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(0);
double charge = 0;
if (wt <= 0)
charge = 0;
else if (wt <= 100 && type == 'O')
charge = 80;
else if (wt <= 100 && type == 'E')
charge = 100;
else if (wt <= 500 && type == 'O')
charge = 150;
else if (wt <= 500 && type == 'E')
charge = 200;
else if (wt <= 1000 && type == 'O')
charge = 210;
else if (wt <= 1000 && type == 'E')
charge = 250;
else if (wt > 1000 && type == 'O')
charge = 250;
else if (wt > 1000 && type == 'E')
charge = 300;
System.out.println("Parcel charges = " + charge);
}
}
Clarify
Knowledge
Iterative
constructs
}
Clarify
Knowledge
Iterative
constructs
Iterative
constructs
import java.util.Scanner;
public class Series
{
public void computeSeriesSum() {
Scanner in = new Scanner(System.in);
System.out.print("Enter n: ");
int n = in.nextInt();
int a = 1, b = 1;
int sum = a + b;
for (int i = 3; i <= n; i++) {
int term = a + b;
sum += term;
a = b;
b = term;
}
System.out.println("Sum=" + sum);
}
}
Clarify
Knowledge
Iterative
constructs
import java.util.Scanner;
public 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 (int i = 1; i <= n; i++) {
term *= i;
sum += term;
}
System.out.println("Sum=" + sum);
}
}
Clarify
Knowledge
Iterative
constructs
Iterative
constructs
Iterative
constructs
Iterative
constructs
Iterative
constructs
Iterative
constructs
case 2:
System.out.print("Enter number: ");
int num = in.nextInt();
int sum = 0;
while (num != 0) {
sum += num % 10;
num /= 10;
}
System.out.println("Sum of Digits " + " = " + sum);
break;
default:
System.out.println("Incorrect choice");
break;
}
}
}
Clarify
Knowledge
iterative
constructs
import java.util.Scanner;
public class SeriesSum
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.println("1. Sum of x^1 - x^2 + .... - x^20");
System.out.println("2. Sum of 1/a^2 + 1/a^4 + .... to n terms");
System.out.print("Enter your choice: ");
int ch = in.nextInt();
switch (ch) {
case 1:
System.out.print("Enter the value of x: ");
int x = in.nextInt();
long sum1 = 0;
for (int i = 1; i <= 20; i++) {
int term = (int)Math.pow(x, i);
if (i % 2 == 0)
sum1 -= term;
else
sum1 += term;
}
System.out.println("Sum = " + sum1);
break;
Clarify
Knowledge
case 2:
System.out.print("Enter the number of terms: ");
int n = in.nextInt();
System.out.print("Enter the value of a: ");
int a = in.nextInt();
int c = 2;
double sum2 = 0;
for(int i = 1; i <= n; i++) {
sum2 += (1/Math.pow(a, c));
c += 2;
}
System.out.println("Sum = " + sum2);
break;
default:
System.out.println("Incorrect choice");
break;
}
}
}
Clarify
Knowledge
Nested
loops
Nested
loops
Nested
loops
Nested
loops
Nested
loops
Nested
loops
Nested
loops
Nested
loops
Nested
loops
case 2:
String s = "ICSE";
for (int i = 0; i < s.length(); i++) {
for (int j = 0; j <= i; j++) {
System.out.print(s.charAt(j) + " ");
}
System.out.println();
}
break;
default:
System.out.println("Incorrect Choice");
}
}
}
Clarify
Knowledge
Character
Manipulation
Write a program in Java to accept an integer number N
such that 0<N<27. Display the corresponding letter of
the alphabet (i.e. the letter at position N).
[Hint: If N =1 then display A]
import java.util.Scanner;
public class Integer2Letter
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter integer: ");
int n = in.nextInt();
if (n > 0 && n < 27) {
char ch = (char)(n + 64);
System.out.println("Corresponding letter = " + ch);
}
else {
System.out.println("Please enter a number in 1 to 26
range");
}
}
}
Clarify
Knowledge
Character
Manipulation
Character
Manipulation
Character
Manipulation
Character
Manipulation
Arrays
Arrays
Arrays
Arrays
Arrays
Arrays
Arrays
Strings
import java.util.Scanner;
public class WordsNLetters
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.println("Enter a sentence:");
String str = in.nextLine();
int wCount = 0, lCount = 0;
int len = str.length();
for (int i = 0; i < len; i++) {
char ch = str.charAt(i);
if (ch == ' ')
wCount++;
elselCount++;
}
System.out.println("No. of words = " + wCount);
System.out.println("No. of letters = " + lCount);
}
}
Clarify
Knowledge
Strings
import java.util.Scanner;
public class VowelRemoval
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.println("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));
if (ch == 'A' ||
ch == 'E' ||
ch == 'I' ||
ch == 'O' ||
ch == 'U') {
continue;
}
newStr = newStr + ch;
}
System.out.println("String with vowels removed");
System.out.println(newStr);
}
}
Clarify
Knowledge
Strings
import java.util.Scanner;
public class NameInitials
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.println("Enter a name of 3 or more
words:");
String str = in.nextLine();
int len = str.length();
System.out.print(str.charAt(0) + " ");
for (int i = 1; i < len; i++) {
char ch = str.charAt(i);
if (ch == ' ') {
char ch2 = str.charAt(i + 1);
System.out.print(ch2 + " ");
}
}
}
}
Clarify
Knowledge
Strings
Strings
Strings
Strings
Strings
Strings
Strings
Strings
Strings