0% found this document useful (0 votes)
24 views10 pages

XII JAVA Practicals

The document provides a series of Java programming examples covering various concepts such as printing messages, using conditional statements, loops, arrays, user-defined methods, and mathematical calculations. Each example includes a brief description of the aim and the corresponding Java code. The programs demonstrate fundamental programming techniques suitable for beginners.

Uploaded by

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

XII JAVA Practicals

The document provides a series of Java programming examples covering various concepts such as printing messages, using conditional statements, loops, arrays, user-defined methods, and mathematical calculations. Each example includes a brief description of the aim and the corresponding Java code. The programs demonstrate fundamental programming techniques suitable for beginners.

Uploaded by

alkullu.1989
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

SAMPLE

1. Write a simple Java program that prints a Welcome Message.

Program:

public static void main (String[] args) {


System.out.println("Hello World”);
}

2. Write a java program to store text data.

Program:

public class Greetings {


public static void main(String[]args){
String firstname = "yourfirstname";
String lastname = "yourlastname";
System.out.println("Hello " +firstname+ " " +lastname);
}
SAMPLE
3. Write a java program in NetBeans to execute ifElse statement.

AIM: If the percentage secured by the student is greater than or equal to 40%, we will declare the
student as passed.

Program:
import java.util.Scanner;
public class PercentageCalculator {
public static void main (String[] args) {
// create an object of Scanner class
Scanner user_input=new Scanner(System.in);
System.out.print("Enter the marks obtained by the student: ");
double marksobtained = user_input.nextDouble();
System.out.print("Enter the total marks: ");
int totalmarks = user_input.nextInt();
double percentage;
percentage = (marksobtained/totalmarks)*100;
System.out.println("Student's Percentage = "+percentage);
if (percentage >= 40) {
System.out.println ("PASSED");
}
else {
System.out.println("FAILED");
}}}
SAMPLE
4. Write a java program in NetBeans to execute Switch statement.

AIM: To Make a Simple Calculator Using switch.

Program:
import java.util.Scanner;
public class Switch {
public static void main (String[]args){
char operator;
double number1, number2, result;
Scanner input = new Scanner(System.in);
System.out.print("Choose an operator: + , - , * or / :");
operator = input.next().charAt(0);
System.out.print("Enter the first number: ");
number1 = input.nextDouble();
System.out.print("Enter the second number: ");
number2 = input.nextDouble();

switch (operator) {
// performs addition between numbers
case '+':
result = number1 + number2;
System.out.println(number1+ " + " +number2+ " = " +result);
break;
// performs subtraction between numbers
case '-':
result = number1 - number2;
System.out.println(number1+ " - " +number2+ " = " +result);
break;
// performs multiplication between numbers
case '*':
result = number1 * number2;
System.out.println(number1+ " * " +number2+ " = " +result);
break;
// performs division between numbers
case '/':
result = number1 - number2;
System.out.println(number1+ " / " +number2+ " = " +result);
break;
default:
System.out.println("Invalid operator!");
break;
}
input.close();
}}
SAMPLE

5. Write a java program in NetBeans to execute While statement.

AIM: A program to print the squares of numbers from 1 to 10.

Program:

import java.util.Scanner;
public class While {
public static void main (String[ ] args) {
int number;
Scanner input = new Scanner(System.in);
System.out.print(“Enter any number between 1-10: ”);
number = input.nextInt();
while (number <= 10) {
System.out.print ("Square of " + number);
System.out.println (" = " + number*number);
++number;
} }
SAMPLE
6. Write a java program in NetBeans to execute DoWhile statement.

AIM: A program to print the squares of numbers from 1 to 10.

Program:
import java.util.Scanner;
public class DoWhile {
public static void main (String[ ] args) {
int number;
Scanner input = new Scanner(System.in);
System.out.print(“Enter any number between 1-10: ”);
number = input.nextInt();
do{
System.out.print ("Square of " + number);
System.out.println (" = " + number*number);
++number;
} while (number <= 10) } }

7. Write a java program in NetBeans to execute for statement.

AIM: A program to print the squares of numbers from 1 to 5.

Program:
public class ForLoop {
public static void main (String[ ] args) {
for (int number = 1; number<= 5; ++number) {
System.out.print("Square of "+ number);
System.out.println(" = "+ number*number);
}}}
SAMPLE
8. Write a java program to show the use of an array to print the class report card for
the five students.

Program:
public class Array {
public static void main (String[ ] args) {
int[] age = {8, 12, 6, 4, 1};
System.out.println("Accessing elements of array:");
for(int i=0; i < age.length; i++) {
System.out.println(age[i]); } } }

9. Write a java program to create a User defined method.

Program:
public class UserDefinedMethod {
static double rectangle_area (double length, double breadth) {
return (length * breadth); }
public static void main(String[]args){
double a;
a = rectangle_area(34.5, 45);
System.out.println("Area of the rectangle = " +a); } }
SAMPLE
10. Write a java program to calculate the area and perimeter of a circle.

Program:
import java.util.Scanner;
public class circle {
public static void main(String[]args){
Scanner input=new Scanner(System.in);
System.out.print("Enter the radius of a circle: ");
float radius=input.nextFloat();
double area=3.14*radius*radius;
double perimeter=2*3.14*radius;
System.out.println("Area of the circle is "+area);
System.out.println("Perimeter of the circle is "+perimeter); } }

11. Write a java program to identify if the number is even or odd.

Program:
import java.util.Scanner;
public class evenodd {
public static void main(String[]args){
Scanner input=new Scanner(System.in);
System.out.print(“Enter any number: ”);
double num=input.nextDouble();
double result=num%2;
if (result==0) {
System.out.println(“The entered number is even”); }
else {
System.out.println(“The entered number is odd”); }
SAMPLE
12. Write a java program in NetBeans to calculate percentage of the marks given by the
student.

Program:
import java.util.Scanner;
public class PercentageCalculator {
public static void main (String[] args) {
Scanner user_input=new Scanner(System.in);
System.out.print("Enter the marks obtained by the student: ");
double marksobtained = user_input.nextDouble();
System.out.print("Enter the total marks: ");
int totalmarks = user_input.nextInt();
double percentage;
percentage = (marksobtained/totalmarks)*100;
System.out.println("Student's Percentage = "+percentage);
}

13. Write a JAVA program to print the sum of first 10 natural numbers 1 to 10.

Program:
public class SumNatural {
public static void main(String[] args) {
int i, sum = 0;
for(i = 1; i <= 10; ++i) {
sum = sum + i;
}
System.out.println("The Sum of first 10 Natural numbers is = " + sum);
}}
SAMPLE
14. Write a Java program to print the FIBONACCI SERIES.

Program:
import java.util.Scanner;
public class Fibonacci {
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
System.out.println("Enter number of terms: ");
int n=s.nextInt();
int num1=-1, num2=1,num3=0;
System.out.println("Fibonacci series is ");
for(int i=0;i<n;i++) {
num3=num1+num2;
num1=num2;
num2=num3;
System.out.print(num3);
System.out.print("\t");
}
}
SAMPLE
15. Write a Java program to print the FACTORIAL of a given number.

Program:
import java.util.Scanner;
public class Factorial {
public static void main(String arg[]) {
int n, fact=1;
Scanner s=new Scanner(System.in);
System.out.println("Enter a number:");
n=s.nextInt();
for(int i=1;i<=n;i++) {
fact=fact*i;
}
System.out.println("Factorial of the given number "+n +" is " +fact);
}
}

You might also like