You are on page 1of 21

Q1) Write a program to print

"Hello Java" in Java.

CODE:
class Simple{
public static void main(String args[]){
System.out.println("Hello Java");
}
}

Output:
Hello Java
Q2) Write a program to print 7
table in Java.

CODE:
import java.util.Scanner;
public class TableExample
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter number: ");
//reading a number whose table is to be print
int num=sc.nextInt();
/*loop start execution form and execute until the condition i
<=10 becomes false*/
for(int i=1; i <= 10; i++)
{
//prints table of the entered number
System.out.println(num+" * "+i+" = "+num*i);
}
}
}

Output:
Enter number : 7
7*1=7
7 * 2 = 14
7 * 3 = 21
7 * 4 = 28
7 * 5 = 35
7 * 6 = 42
7 * 7 = 49
7 * 8 = 56
7 * 9 = 63
7 * 10 = 70
Q3) Write a program to add two
numbers in Java.

CODE:

public class SumOfNumbers1


{
public static void main(String args[]) {
int n1 = 225, n2 = 115, sum;
sum = n1 + n2;
System.out.println("The sum of numbers is: "+sum);
}
}

Output:
The sum of numbers is: 340
Q4) Write a program to print the
smallest number in Java.

CODE:
public class SmallestElement_array {
public static void main(String[] args) {

//Initialize array
int [] arr = new int [] {25, 11, 7, 75, 56};
//Initialize min with first element of array.
int min = arr[0];
//Loop through the array
for (int i = 0; i < arr.length; i++) {
//Compare elements of array with min
if(arr[i] <min)
min = arr[i];
}
System.out.println("Smallest element present in given arr
ay: " + min);
}
}

Output:
Smallest element present in given
array:7
Q5) Write a program to print 1 to 10
numbers in Java.

CODE:
public class Exercise10 {

public static void main(String[] args)


{
int i;
System.out.println ("The first 10 natural numbers
are:\n");
for (i=1;i<=10;i++)
{
System.out.println (i);
}
System.out.println ("\n");
}
}

Output:
The first 10 natural numbers are :
1
2
3
4
5
6
7
8
9
10
Q6) Write a program to print
weekdays using switch case
statement in java.
CODE:
//Java - Switch Case Statement Example Code - Print Weekdays.
import java.util.Scanner;

public class SwitchCaseExample{


public static void main(String args[]){
int day;
Scanner SC=new Scanner(System.in);

System.out.print("Enter a weekday number (0 to 6): ");


day=SC.nextInt();

if(day<0 || day>6){
System.out.println("Invalid weekday number.");
System.exit(0);
}

//print weekday name according to give value


switch(day){
case 0:
System.out.println("Sunday");
break;
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
case 3:
System.out.println("Wednesday");
break;
case 4:
System.out.println("Thursday");
break;
case 5:
System.out.println("Friday");
break;
case 6:
System.out.println("Saturday");
break;
default:
System.out.println("Invalid");
break;
}

}
}

Output:

First Run:
Enter a weekday number (0 to 6): 8
Invalid weekday number.

Second Run:
Enter a weekday number (0 to 6): 5
Friday
Q7) Write a program to print
fibonacci series in Java.

CODE:
class FibonacciExample1{
public static void main(String args[]) {
int n1=0,n2=1,n3,i,count=10;
System.out.print(n1+" "+n2);//printing 0 and 1
for(i=2;i<count;++i)//loop starts from 2 because 0 and 1
are already printed {
n3=n1+n2;
System.out.print(" "+n3);
n1=n2;
n2=n3;
}

Output:
0 1 1 2 3 5 8 13 21 34
Q8) Write a program to print
factorial in Java.

CODE:
class FactorialExample{
public static void main(String args[]){
int i,fact=1;
int number=5;//It is the number to calculate factorial
for(i=1;i<=number;i++){
fact=fact*i;
}
System.out.println("Factorial of "+number+" is: "+fact
);
}
}

Output:
Factorial of 5 is: 120
Q9) Write a program to print
Arithmetic Operator in Java.

CODE:
public class OperatorExample{
public static void main(String args[]){
int a=10;
int b=5;
System.out.println(a+b);//15
System.out.println(a-b);//5
System.out.println(a*b);//50
System.out.println(a/b);//2
System.out.println(a%b);//0
}
}

Output:
15
5
50
2
0
Q10) Write a program to print
Pyramid Program.

CODE:
import java.util.Scanner;
public class FirstPattern
{
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
System.out.println("Please provide number of rows to print... ");
int myrows = scanner.nextInt();
System.out.println("\nThe star pattern is... ");
for (int m = 1; m <= myrows; m++)
{
for (int n = 1; n <= m; n++)
{
System.out.print("*");
}
System.out.println();
}
}
}

Output:
The star pattern is …
*
**
***
****
*****
Q11) Write a program to print
armstrong numbers from 1 to 100
in Java.
CODE:
public class Armstrong
{
public static void main(String[] args)
{
int n, count = 0, a, b, c, sum = 0;
System.out.print("Armstrong numbers from 1 to 1000:");
for(int i = 1; i <= 1000; i++)
{
n = i;
while(n > 0)
{
b = n % 10;
sum = sum + (b * b * b);
n = n / 10;
}
if(sum == i)
{
System.out.print(i+" ");
}
sum = 0;
}
}
}

Output:
$ javac Armstrong.java
$ java Armstrong

Armstrong numbers from 1 to 1000:1 153 370 371 407


Q12) Write a Java program to
Calculate Percentage.
CODE:
// Java Program to illustrate Total
// marks and Percentage Calculation

import java.io.*;

class GFG {

public static void main(String[] args)

int N = 5, total_marks = 0;

float percentage;

// create 1-D array to store marks

int marks[] = { 89, 75, 82, 60, 95 };

// calculate total marks

for (int i = 0; i < N; i++) {


total_marks += marks[i];
}

System.out.println("Total Marks : " + total_marks);

// calculate percentage

percentage = (total_marks / (float)N);


System.out.println(

"Total Percentage : " + percentage + "%");

}
}

import java.util.Scanner;
public class Percentage {
public static void main(String args[]){
float percentage;
float total_marks;
float scored;
Scanner sc = new Scanner(System.in);
System.out.println("Enter your marks ::");
scored = sc.nextFloat();

System.out.println("Enter total marks ::");


total_marks = sc.nextFloat();

percentage = (float)((scored / total_marks) * 100);


System.out.println("Percentage ::"+ percentage);
}
}

Output:
Enter your marks ::
500
Enter total marks ::
600
Percentage ::83.33333
Q13) Write a program to print Java
Program to calculate simple interest

CODE:
import java.util.Scanner;
public class JavaExample
{
public static void main(String args[])
{
float p, r, t, sinterest;
Scanner scan = new Scanner(System.in);
System.out.print("Enter the Principal : ");
p = scan.nextFloat();
System.out.print("Enter the Rate of interest : ");
r = scan.nextFloat();
System.out.print("Enter the Time period : ");
t = scan.nextFloat();
scan.close();
sinterest = (p * r * t) / 100;
System.out.print("Simple Interest is: " +sinterest);
}
}

Output:
Enter the Principal : 2000
Enter the Rate of interest : 6
Enter the Time period : 3
Simple Interest is: 360.0
Q14) Write a program to print Java
Program to find Reverse of the
string.

CODE:
// java program to reverse a word

import java.io.*;

import java.util.Scanner;

class GFG {

public static void main (String[] args) {

String str= "Geeks", nstr="";

char ch;

System.out.print("Original word: ");

System.out.println("Geeks"); //Example word

for (int i=0; i<str.length(); i++)

ch= str.charAt(i); //extracts each character

nstr= ch+nstr; //adds each character in front of the existing string

}
System.out.println("Reversed word: "+ nstr);

}
}

public class Reverse


{
public static void main(String[] args) {
String string = "Dream big"; //Stores the reverse of giv
en string
String reversedStr = "";

//Iterate through the string from last and add each character t
o variable reversedStr
for(int i = string.length()-1; i >= 0; i--){
reversedStr = reversedStr + string.charAt(i);
}

System.out.println("Original string: " + string);


//Displays the reverse of given string
System.out.println("Reverse of given string: " + reversedStr)
;
}
}

Output:

Original string: Dream big


Reverse of given string: gib maerD
Q15) Write a program to print login
form in Java

CODE:
LoginFormDemo.java
//import required classes and packages
import javax.swing.*;

import java.awt.*;
import java.awt.event.*;
import java.lang.Exception;

//create CreateLoginForm class to create login form


//class extends JFrame to create a window where our component
add
//class implements ActionListener to perform an action on button
click
class CreateLoginForm extends JFrame implements ActionListe
ner
{
//initialize button, panel, label, and text field
JButton b1;
JPanel newPanel;
JLabel userLabel, passLabel;
final JTextField textField1, textField2;

//calling constructor
CreateLoginForm()
{
//create label for username userLabel = new JLabel(
); userLabel.setText("Username"); //set label value for
textField1 //create text field to get username from the use
r
textField1 = new JTextField(15); //set length of the text
//create label for password
passLabel = new JLabel();
passLabel.setText("Password"); //set label value for textFiel
d2
//create text field to get password from the user
textField2 = new JPasswordField(15); //set length for the p
asswor
//create submit button
b1 = new JButton("SUBMIT"); //set label to button
//create panel to put form elements newPanel = new JPan
el(new GridLayout(3, 1));
newPanel.add(userLabel); //set username label to panel
newPanel.add(textField1); //set text field to panel
newPanel.add(passLabel); //set password label to panel
newPanel.add(textField2); //set text field to panel
newPanel.add(b1); //set button to panel

//set border to panel


● add(newPanel, BorderLayout.CENTER) //perform acti
on on button click
b1.addActionListener(this); //add action listener to button

setTitle("LOGIN FORM"); //set title to the login for


m
}

//define abstract method actionPerformed() which will be calle


d on button click
public void actionPerformed(ActionEvent ae) //pass action list
ener as a parameter
{
String userValue = textField1.getText();
//get user entered username from the textField1
String passValue = textField2.getText();
//get user entered pasword from the textField2

//check whether the credentials are authentic or not


if (userValue.equals("test1@gmail.com") && passValue.eq
uals("test")) { //if authentic, navigate user to a new page

//create instance of the NewPage

NewPage page = new NewPage();

//make page visible to the user


page.setVisible(true);

//create a welcome label and set it to the new page


JLabel wel_label = new JLabel("Welcome: "+userValue);

page.getContentPane().add(wel_label);
}
else{
//show error message
System.out.println("Please enter valid username and pass
word");
}
}
}
//create the main class
class LoginFormDemo
{
//main() method start
public static void main(String arg[])
{
try
{
//create instance of the CreateLoginForm
CreateLoginForm form = new CreateLoginForm();
form.setSize(300,100); //set size of the frame
form.setVisible(true); //make form visible to the user
}
catch(Exception e)
{
//handle exception
JOptionPane.showMessageDialog(null, e.getMessage());
}
}
}

NewPage.java

//import required classes and packages


import javax.swing.*;
import java.awt.*;
//create NewPage class to create a new page on which user will n
avigate
class NewPage extends JFrame
{
//constructor
NewPage()
{
setDefaultCloseOperation(javax.swing.
WindowConstants.DISPOSE_ON_CLOSE);
setTitle("Welcome");
setSize(400, 200);
}
}

Output:

You might also like