Java Program To Print Alphabets
Java Program To Print Alphabets
} 4. class Multiplication{ public static void main(String a[]){ int n=6; int i; for(i=1;i<=20;i++) { System.out.println(i+"*"+n+"="+(i*n)); } } }
integer and entered integer is also printed and finally a float and it is also printed ion the screen. Scanner class is present in java.util package so we import this package in our program. We first create an object of Scanner class and then we use the methods of Scanner class. Consider the statement
Scanner a = new Scanner(System.in);
here Scanner is the class name, a is the name of object, new keyword is used to allocate the memory and System.in is the input stream. Following methods of Scanner class are used in the program below :1) nextInt to input an integer 2) nextFloat to input a float 3) nextLine to input a string
import java.util.Scanner; class GetInputFromUser { public static void main(String args[]) { int a; float b; String s; Scanner in = new Scanner(System.in); System.out.println("Enter a string"); s = in.nextLine(); System.out.println("You entered string "+s); System.out.println("Enter an integer"); a = in.nextInt(); System.out.println("You entered integer "+a); System.out.println("Enter a float"); b = in.nextFloat(); System.out.println("You entered float "+b); } }
} }
11. java program print prime numbers This java program prints prime numbers, number of prime numbers required is asked from the user. Remember that smallest prime number is 2.
status = 1; num++; } } }
12. Java program to check armstrong number This java program checks if a number is armstrong or not.
} } }
} }
} } }
for( c = 0 ; c < length ; c++ ) for( i = 1 ; i <= length - c ; i++ ) { sub = string.substring(c, c+i); System.out.println(sub); }
18. java program to display date and time, print date and time using java program
Java date and time program :- Java code to print or display current system date and time. This program prints current date and time. We are using GregorianCalendar class in our program. Java code to print date and time is given below :import java.util.*; class GetCurrentDateAndTime { public static void main(String args[]) { int day, month, year; int second, minute, hour; GregorianCalendar date = new GregorianCalendar(); day = date.get(Calendar.DAY_OF_MONTH); month = date.get(Calendar.MONTH); year = date.get(Calendar.YEAR); second = date.get(Calendar.SECOND); minute = date.get(Calendar.MINUTE); hour = date.get(Calendar.HOUR); System.out.println("Current date is "+day+"/"+(month+1)+"/"+year); System.out.println("Current time is "+hour+" : "+minute+" : "+second); } }
System.out.println("Free memory in jvm (Java Virtual Machine) after Garbage Collection = "+rs.freeMemory()); } }
10
for ( c = 0 ; c < m ; c++ ) for ( d = 0 ; d < n ; d++ ) second[c][d] = in.nextInt(); for ( c = 0 ; c < m ; c++ ) for ( d = 0 ; d < n ; d++ ) sum[c][d] = first[c][d]+ second[c][d]; System.out.println("Sum of entered matrices:-"); for ( c = 0 ; c < m ; c++ ) { for ( d = 0 ; d < n ; d++ ) System.out.print(sum[c][d]+"\t"); System.out.println(); } } }
11
12
} } }
13