0% found this document useful (0 votes)
6 views4 pages

Java Programs

The document contains several Java programs demonstrating basic programming concepts such as printing output, variable declaration, user input, and control structures. It includes examples of using the Scanner class for user input, calculating factorials, and checking for prime numbers. Each program is structured with a main method and showcases different functionalities of Java.

Uploaded by

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

Java Programs

The document contains several Java programs demonstrating basic programming concepts such as printing output, variable declaration, user input, and control structures. It includes examples of using the Scanner class for user input, calculating factorials, and checking for prime numbers. Each program is structured with a main method and showcases different functionalities of Java.

Uploaded by

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

1.

/*

This is a simple Java program.


Call this file "Example.java".
*/
class Example {
// Your program begins with a call to main().
public static void main(String args[]) {
System.out.println("welcome to simple Java program.");
}
}

2. public class second {


public static void main(String args []) {
int num; // this declares a variable called num
num = 100; // this assigns num the value 100
System.out.println("This is num: " + num);
num = num * 2;
System.out.print("The value of num * 2 is ");
System.out.println(num);
}
}

3. public class tc{


public static void main(String args []) {
byte b;
int i = 257;
double d = 323.142;
System.out.println("Conversion of int to byte.");
b = (byte) i;
System.out.println("i and b " + i + " " + b);
System.out.println(d*i +" " + d+i);
}
}
4. input from user
import java.util.*;
class UserInputDemo
{
public static void main(String[] args)
{
Scanner sc= new Scanner(System.in); //System.in is a standard input stream
System.out.print("Enter first number- ");
int a= sc.nextInt();
System.out.print("Enter second number- ");
int b= sc.nextInt();
System.out.print("Enter third number- ");
int c= sc.nextInt();
int d=a+b+c;
System.out.println("Total= " +d);
}
}

The java.util package contains a class named Scanner that is used to receive
input of primitive types like int, double, etc., as well as strings. If you want
an input method for situations where time is a factor, like in competitive
programming, it is the simplest way to read input in a Java application.
Scanner class
The Java.util package’s Scanner class is used to read input data from a
variety of sources, including input streams, users, files, etc.
Java User Input
In the java.util package is the Scanner class, which gathers user input. By
creating an object of the class and using it, you can use any of the several
methods given in the Scanner class documentation. The nextLine() method,
which is used to read strings, will be utilized in our example:
Input Types
Below are some of the input types we use with the Scanner class.
Method Description

nextBoolean() Reads a user-provided boolean value.

nextByte() Reads a user-supplied byte value.

nextDouble() A double value is read from the user.

nextFloat() Reads a user-supplied float value.

nextInt() Reads a user-supplied int value.

nextLine() Reads a value for a String from the user.

nextLong() A lengthy value is read from the user.

nextShort() Reads a brief value supplied by the user.


import java.util.*; OR
import java.util.Scanner;

class UserInputDemo
{
public static void main(String[] args)
{
Scanner sc1= new Scanner(System.in); //System.in is a standard input stream
System.out.print("Enter student name- ");
String f= sc1.nextLine();
System.out.print("Enter first number- ");
Double a= sc1.nextDouble();
System.out.print("Enter second number- ");
int b= sc1.nextInt();
System.out.print("Enter third number- ");
int c= sc1.nextInt();
Double d=a+b+c;
System.out.println("Total= " +d);

System.out.println("Student name is:"+f);


}
}

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);

1. public class PrimeExample{


2. public static void main(String args[]){
3. int i,m=0,flag=0;
4. int n=3;//it is the number to be checked
5. m=n/2;
6. if(n==0||n==1){
7. System.out.println(n+" is not prime number");
8. }else{
9. for(i=2;i<=m;i++){
10. if(n%i==0){
11. System.out.println(n+" is not prime number");
12. flag=1;
13. break;
14. }
15. }
16. if(flag==0) { System.out.println(n+" is prime number"); }
17. }//end of else
18. }
19. }

You might also like