Skip to main content

Write a program to print prime numbers between 10 and 99.

Write a program to print prime numbers between 10 and 99.




import java.util.Scanner;

public class SumofDigits {


public static void main(String[] args) 

{

int n,rem,sum;

Scanner s=new Scanner(System.in);

System.out.println("Enter the number:");

n=s.nextInt();


for(sum=0;n!=0;n/=10) {

rem=n%10;

sum=sum+rem;

}

  System.out.println(sum);

}


}




Write a program to print the sum of the elements of an array following the given below condition.


If the array has 6 and 7 in succeeding orders, ignore the numbers between 6 and 7 and consider the other numbers for calculation of sum.


Eg1) Array Elements - 10,3,6,1,2,7,9

O/P: 22   

[i.e 10+3+9]


Eg2) Array Elements - 7,1,2,3,6

O/P:19


Eg3) Array Elements - 1,6,4,7,9

O/P:10



//Use One Dimensional Array. 

>> If you have any alternate methods then please comment below.



import java.io.*;

public class ConditionalSum {
static void conditionalSum(int l[], int a, int b) {

int sum = 0;
boolean add = true;
for (int i=0; i<l.length; i++) {

if (l[i] != a && add == true)

sum = sum + l[i];
else if (l[i] == a)
add = false;

else if (l[i] == b)
add = true;

}

System.out.println(sum);

}

public static void main(String[] args) {

int arr[] = {10,3,6,1,2,7,9};
int a = 6;
int b = 7;
conditionalSum(arr,a,b);

        }

}




Write an interface called Playable, with a method void play(); Let this interface be placed in a package called music.  Write a class called Veena which implements Playable interface. Let this class be placed in a package music.string  Write a class called Saxophone which implements Playable interface. Let this class be placed in a package music.wind  Write another class Test in a package called live. Then, a. Create an instance of Veena and call play() method b. Create an instance of Saxophone and call play() method c. Place the above instances in a variable of type Playable and then call play()  Interfaces


//if you have any alternate method comment down below.


import music.Playable;
import music.string.Veena;
import music.wind.Saxophone;

public class Test {

public static void main(String[] args) {

Veena v = new Veena();
v.play();
Playable p = new Veena();
p.play();
Saxophone s = new Saxophone();
s.play();
Playable ps = new Saxophone();
ps.play();

}

}

package music;

public interface Playable {

void play();

}


package music.string;

import music.Playable;

public class Veena implements Playable {

public void play() {

System.out.println("Veena is Playing");

}

}

package music.wind;

import music.Playable;

public class Saxophone implements Playable {

public void play() {

System.out.println("Saxophone is Playing");

}

}



Write a program that takes as input the size of the array and the elements in the array. The program then asks the user to enter a particular index and prints the element at that index. Index  starts from zero.   This program may generate Array Index Out Of Bounds Exception  or NumberFormatException .  Use exception handling mechanisms to handle this exception.   Sample Input and Output 1: Enter the number of elements in the array 2 Enter the elements in the array 50 80 Enter the index of the array element you want to access 1 The array element at index 1 = 80 The array element successfully accessed    Sample Input and Output 2: Enter the number of elements in the array 2 Enter the elements in the array 50 80 Enter the index of the array element you want to access 9 java.lang.ArrayIndexOutOfBoundsException    Sample Input and Output 3: Enter the number of elements in the array 2 Enter the elements in the array 30 j java.lang.NumberFormatException


//If you have any alternate method then comment below.


import java.util.InputMismatchException;
import java.util.Scanner;

public class ExceptionHandling {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of elements in the arrays");
int n = sc.nextInt();
int[] arr = new int[n];
System.out.println("Enter the elements in the array ");
try {

for (int i = 0; i<n; i++)
arr[i] = sc.nextInt();
System.out.println("Enter the index of the array element you want to access");
int index = sc.nextInt();
System.out.println("The array element at index "+ index + " = "+ arr[index]);
System.out.println("The array element successfully accessed");
} catch (ArrayIndexOutOfBoundsException e) {

System.out.println("java.lang.ArrayIndexOutOfBoundsException");

}catch (InputMismatchException e) {

System.out.println("java.util.InputMismatchException");

}
sc.close();

}

}



Write a Program to take care of Number Format Exception if user enters values other than integer for calculating average marks of 2 students. The name of the students and marks in 3 subjects are taken from the user while executing the program. In the same Program write your own Exception classes to take care of Negative values and values out of range (i.e. other than in the range of 0-100)


//If you have any alternate method then comment below.


import java.util.Scanner;

public class ExceptionHandling3 {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);
for (int i=0; i<2; i++) {

String StudentName = "";
int sub1 = 0;
int sub2 = 0;
int sub3 = 0;
try {

StudentName = sc.nextLine();
if (sc.hasNextInt())
sub1 = sc.nextInt();

else
throw new NumberFormatException();
if (sc.hasNextInt())
sub2 = sc.nextInt();

else
throw new NumberFormatException();
if (sc.hasNextInt())

sub3 = sc.nextInt();

else
throw new NumberFormatException();
if (sub1 < 0) throw new NegativeValuesException();
if (sub1 > 100) throw new ValuesOutOfRangeException();
if (sub2 < 0) throw new NegativeValuesException();
if (sub2 > 100) throw new ValuesOutOfRangeException();
if (sub3 < 0) throw new NegativeValuesException();
if (sub3 > 100) throw new ValuesOutOfRangeException();
} catch (ArithmeticException e) {

System.out.println(e.getMessage());

} catch (NegativeValuesException e) {

System.out.println(e.getMessage());

} catch (ValuesOutOfRangeException e) {

System.out.println(e.getMessage());

}
System.out.println("Name: "+StudentName);
System.out.println("Marks of subject 1: "+sub1);
System.out.println("Marks of subject 2: "+sub2);
System.out.println("Marks of subject 3: "+sub3);
}
sc.close();
}

}




Comments

Popular posts from this blog

Classification of Elements & Periodicity in Properties Handwritten Notes | Chemistry Class 11 Chapter 3 | Classification of Elements & Periodicity in Properties Notes for Board Exams | STAR tube Notes

Classification of Elements & Periodicity in Properties Handwritten Notes | Chemistry Class 11 Chapter 3 | Classification of Elements & Periodicity in Properties Notes for Board Exams | STAR tube Notes Chapter 3 Classification of Elements & Periodicity in Properties Classification of Elements & Periodicity in Properties Notes Higher Secondary is the most crucial stage of school education because at this juncture specialized discipline based, content  ‐oriented courses are introduced. Students reach this stage after 10 years of general education and opt for Chemistry with a purpose of pursuing their career in basic sciences or professional courses like medicine, engineering, technology and study courses in applied areas of science and technology at tertiary level. Therefore, there is a need to provide learners with sufficient conceptual background of Chemistry, which will make them competent to meet the challenges of academic and professional courses after the senior seco

NCERT All Chapters Handwritten Notes | Physics Chemistry Best Handwritten Notes | STAR tube Notes

NCERT All Chapters Handwritten Notes | Physics Chemistry Best Handwritten Notes | STAR tube Notes Are you looking for handwritten notes which are easy and simple to understand.Then you are on the right place because here you would get handwritten notes which are very easy to understand. It's not possible to cover whole syllabus and revise it during exam time because you have to revise lots of subjects in very less time. In this case,notes are one of the best option to cover whole syllabus in very short period of time. STAR tube will provide you the Best Handwritten Notes of Class 11 , Class 12 , Btech/Bsc Electrical and Electronics. Follow and Subscribe the STAR tube on  YouTube . Chemistry Handwritten Notes | Class 11 Unit I : Some Basic Concepts of Chemistry   -    PDF Unit II : Structure of Atom   -    PDF Unit III : Classification of Elements and Periodicity in Properties   -    PDF Unit IV : Chemical Bonding and Molecular Structure   -    PDF Unit V : States of Matter: Gas

Some Basic Concepts of Chemistry Handwritten Notes | Chemistry Class 11 Chapter 1 | Some Basic Concepts of Chemistry Notes for Board Exams | STAR tube Notes

Some Basic Concepts of Chemistry Handwritten Notes | Chemistry Class 11 Chapter 1 | Some Basic Concepts of Chemistry Notes for Board Exams | STAR tube Notes Chapter 1 Some Basics Concepts of Chemistry Some Basic Concepts of Chemistry Best Handwritten Notes Higher Secondary is the most crucial stage of school education because at this juncture specialized discipline based, content  ‐oriented courses are introduced. Students reach this stage after 10 years of general education and opt for Chemistry with a purpose of pursuing their career in basic sciences or professional courses like medicine, engineering, technology and study courses in applied areas of science and technology at tertiary level. Therefore, there is a need to provide learners with sufficient conceptual background of Chemistry, which will make them competent to meet the challenges of academic and professional courses after the senior secondary stage. STAR tube will provide you the Best Handwritten Notes of Class 11 ,