Posts Tagged ‘Java tutorial for beginners’
Using For Each in Java
Posted April 2, 2020
on:- In: Core Java Tutorials | Java Source Code | Java Tutorials
- Comments Off on Using For Each in Java
Objective:
- How to use For Each in Java?
- How to use advance for in Java?
- How to use advance for loop in Java Programming Language?
- Types of For loop in Java
Source Code:
class ForEachExample { public static void main(String args[]) { //declaring an array int a[]={1,2,3,4,5,6}; // Old way System.out.println("Old way"); for(int i=0; i<a.length;i++) { System.out.println(a[i]); } //New way: for-each loop System.out.println("For each"); for(int i:a) { System.out.println(i); } } }
Tags: Core Java Tutorial, Core Java Tutorials, For each in java, for each loop in java, Free Java Tutorials, How to use advance for in Java, How to use For Each in Java, Java Tutorial, Java tutorial for beginners, Java Tutorials, Java Tutorials for Beginners, Types of For loop in Java, using for each in java, using for each loop in java
- In: Core Java Tutorials | How to's | Java Source Code | Java Tutorials
- Comments Off on Pattern Programs in Java – Pattern 2
Pattern 2 :
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
Java Program for the above pattern:
import java.util.Scanner; public class MainClass { public static void main(String[] args) { Scanner sc = new Scanner(System.in); //Taking rows value from the user System.out.println("How many rows you want in this pattern?"); int rows = sc.nextInt(); System.out.println("Here is your pattern....!!!"); for (int i = 1; i <= rows; i++) { for (int j = 1; j <= i; j++) { System.out.print(i+" "); } System.out.println(); } //Close the resources sc.close(); } }
Tags: Aatul Palandurkar, Atul Palandurkar, Best Java Trainer in Nagpur, Best Java Training in Nagpur, Best Java Training in Pune, Best Java Tutorials, Core Java, Core Java Tutorial, Core Java Tutorials, Core Java Workshop, corporate java assignments, Corporate Trainer for Java, Corporate Training, Corporate Training for Java, Free Java Tutorials, HP Summer Internship Assignments, HP Winter Internship Assignments, HPES Internship Assignments, HPES Summer Internship Assignments, Java Assignment Questions, Java Assignments, Java Tutorial, Java tutorial for beginners, Java Tutorials, Java Tutorials for Beginners, Java User Group Nagpur, Java User Group Pune
Pattern Programs In Java – Pattern 1
Posted May 21, 2018
on:- In: Core Java Tutorials | Java Source Code | Java Tutorials
- Comments Off on Pattern Programs In Java – Pattern 1
Q. Write a Java program to print given pattern.
Pattern 1 :
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4 5 6
1 2 3 4 5 6 7
Java Program:
import java.util.Scanner; public class MainClass { public static void main(String[] args) { Scanner sc = new Scanner(System.in); //Taking rows value from the user System.out.println("How many rows you want in this pattern?"); int rows = sc.nextInt(); System.out.println("Here is your pattern....!!!"); for (int i = 1; i <= rows; i++) { for (int j = 1; j <= i; j++) { System.out.print(j+" "); } System.out.println(); } //Close the resources sc.close(); } }
Tags: Aatul Palandurkar, Best Java Trainer in Nagpur, Best Java Trainer in Pune, Best Java Trainer inn Nagpur, Best Java Training in Nagpur, Best Java Tutorials, Core Java Tutorial, Core Java Tutorials, Example for Scanner class in Java, Example of Scanner Class, Free Java Tutorials, Java Assignments, java program, Java Program for Beginners, Java Program for Scanner class, Java Programming, Java Programming Assignments, Java Training in Nagpur, Java Tutorial, Java tutorial for beginners, Number Pascal Triangle in Java, Number Pattern Program in Java, Pascal Triangle in Java, Pascal Triangle Program in Java, Pattern Program in Java, Scanner class in Java, take input using Scanner class, Taking input using Scanner class in Java, Taking input via Scanner class in Java, Using Scanner class in Java
- In: Java Source Code | Java Tutorials | NetBeans IDE Source Code
- Comments Off on Java Program to convert Decimal values to Hexadecimal values
Objectives :
- How to convert Decimal to Hexadecimal in Java?
Java Program to convert Decimal values to Hexadecimal values :
import java.util.Scanner; public class DecimalToHexadecimal { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter decimal number you like: "); int deci = input.nextInt(); System.out.println("The hexadecimal number for decimal " + deci + " is " + convert(deci)); } public static String convert(int decimal) { String hex = ""; while (decimal != 0) { int hexValue = decimal % 16; hex = toHexadecimal(hexValue) + hex; decimal = decimal / 16; } return hex; } public static char toHexadecimal(int hexValue) { if (hexValue <= 9 && hexValue >= 0) { return (char) (hexValue + '0'); } else { return (char) (hexValue - 10 + 'A'); } } }
Output :
Enter decimal number you like: 1234
The hexadecimal number for decimal 1234 is 4D2
Tags: Best Java Tutorials, Classroom Training & Workshop on Core Java, Core Java Tutorial, Core Java Tutorials, Decimal to Hexadecimal Conversion, Decimal to Hexadecimal Conversion in Java, Free Java Tutorials, Hexadecimal to Decimal Conversion, How to convert Decimal to Hexadecimal in Java?, How to convert Hexadecimal to Decimal in Java?, Java program to convert Decimal to Hexadecimal, Java Program to convert Decimal values to Hexadecimal values, Java Tutorial, Java tutorial for beginners, Java Tutorials
Objectives :
- Convert Decimal number to Roman number
- Convert Decimal numeral to Roman numeral
- How to convert a decimal number to roman number
- Write a program that converts a decimal number to Roman number.
- Write a program that converts a decimal number to Roman number. Decimal Number is accepted using Scanner class at the time of execution.
- Write a program that converts a decimal number to Roman number. Decimal Number is accepted as command line input at the time of execution.
Program :
Java program that converts a decimal number to Roman number. Decimal Number is accepted using Scanner class at the time of execution.
import java.util.Scanner; public class DecimalToRoman { private static String toRoman(int num) { String[] romanCharacters = { "M", "CM", "D", "C", "XC", "L", "X", "IX", "V", "I" }; int[] romanValues = { 1000, 900, 500, 100, 90, 50, 10, 9, 5, 1 }; String result = ""; for (int i = 0; i < romanValues.length; i++) { int numberInPlace = num / romanValues[i]; if (numberInPlace == 0) continue; result += numberInPlace == 4 && i > 0? romanCharacters[i] + romanCharacters[i - 1]: new String(new char[numberInPlace]).replace("\0",romanCharacters[i]); num = num % romanValues[i]; } return result; } public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter a number : "); int decimal = scanner.nextInt(); System.out.println(toRoman(decimal)); } }
Output :
Enter a number : 1234
MCCXXXIV
Java program that converts a decimal number to Roman number. Decimal Number is accepted as command line input at the time of execution.
public class DecimalToRoman { private static String toRoman(int num) { String[] romanCharacters = { "M", "CM", "D", "C", "XC", "L", "X", "IX", "V", "I" }; int[] romanValues = { 1000, 900, 500, 100, 90, 50, 10, 9, 5, 1 }; String result = ""; for (int i = 0; i < romanValues.length; i++) { int numberInPlace = num / romanValues[i]; if (numberInPlace == 0) continue; result += numberInPlace == 4 && i > 0? romanCharacters[i] + romanCharacters[i - 1]: new String(new char[numberInPlace]).replace("\0",romanCharacters[i]); num = num % romanValues[i]; } return result; } public static void main(String[] args) { if(args.length<1 || args.length>1){ System.out.println("Wrong input"); }else{ String number = args[0]; int decimal = Integer.parseInt(number); System.out.println(toRoman(decimal)); } } }
Tags: Best Java Trainer in Pune, Best Java Training in Pune, Best Java Tutorials, Convert Decimal number to Roman number, Convert Decimal numeral to Roman numeral, Core Java Tutorial, Core Java Tutorials, Free Java Project, Free Java Source Code, Free Java Tutorials, How to convert a decimal number to roman number, java program, Java Program for Beginners, Java program for Command Line Argument, Java Program for Scanner class, Java program that converts a decimal number to Roman number. Decimal Number is accepted as command line input at the time of execution., Java program that converts a decimal number to Roman number. Decimal Number is accepted using Scanner class at the time of execution., Java Program to Convert Decimal to Roman, java projects, Java Training in Pune, Java Tutorial, Java tutorial for beginners, Java Tutorials, Java Tutorials for Beginners, Open Source Java Project, Write a program that converts a decimal number to Roman number, Write a program that converts a decimal number to Roman number. Decimal Number is accepted as command line input at the time of execution, Write a program that converts a decimal number to Roman number. Decimal Number is accepted using Scanner class at the time of execution