Professional Documents
Culture Documents
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:
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 {
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;
if(day<0 || day>6){
System.out.println("Invalid weekday number.");
System.exit(0);
}
}
}
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
import java.io.*;
class GFG {
int N = 5, total_marks = 0;
float percentage;
// calculate 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();
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 {
char ch;
}
System.out.println("Reversed word: "+ nstr);
}
}
//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);
}
Output:
CODE:
LoginFormDemo.java
//import required classes and packages
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.lang.Exception;
//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
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
Output: