Posts

Showing posts with the label Basic Programs

Program to display Digits in 7-Segment Style Using Java

Image
Display Digits in 7-Segment Style Using Java Ever wondered how digital clocks or calculators display numbers? They often use seven-segment displays —a simple arrangement of 7 LEDs to represent numbers from 0 to 9. In this post, we'll create a Java program that simulates the same effect on your console screen! 💡 What is a 7-Segment Display? A 7-segment display is made up of seven LEDs (labeled a through g ) that can be turned on or off to create numerical digits. Here's a visual representation: Each digit is formed by turning on a combination of these segments.

Pascal triangle 5

Image
This program will display the pascal triangle or print star pattern as shown below: * *A* *A*A* *A*A*A* *A*A*A*A*

Program for Bubble Sort

An  array  is a group of contiguous or related data items that share a common name. An array stores multiple data items of the same data type, in a contiguous block of memory, divided into a number of slots.Arrays of any type can be created and may have one or more dimensions. To create an array, you first must create an array variable of the desired type. The general form of a one-dimensional array declaration is type  var-name[ ]; Here  type  is the data type of an array which is to hold. By specifying declaring array no array actually exists. To create actual array you must allocate one using  new  and assign it to the array-variable. Its general form is given below: array-var  =  new type[size]; Here  size  specifies the number of elements in the array. In this program, we read the 10 elements from keyboard and store them in array and after that we sort these elements using  Bubble sort  ...

Java's for-each loop demo

The  for-each  loop introduced in Java5 (also called the  "enhanced for loop" ). It is mainly used to traverse array or collection elements. The advantage of for-each loop is that it eliminates the possibility of bugs and makes the code more readable. The syntax of for-each loop is given below: for(data_type variable : array | collection){ } For example, consider the code int arr[ ]={1,2,3,4}; for(int i : arr) { System.out.println(i); } The output of this will be 1 2 3 4 PROGRAM class ForEachDemo { public static void main(String[] args) { String str[ ] = {"Physics", "Chemistry", "Maths"}; for(String a : str) { System.out.println(a); } } } OUTPUT C:\>javac ForEachDemo.java C:\>java PForEachDemo Physics Chemistry Maths

Pascal triangle 4

This program will display the pascal triangle or print star pattern as shown in output.  System.out.print("....."):  are used for display message on screen or console but cursor not move in new line.  System.out.println("....."):  are used for display message on screen or console cursor move in new line. PROGRAM class PascalTriangle4 { public static void main(String[] args) { for(int i=0; i<5; i++) { for(int j=0; j<=i; j++) { // This loop will show star '*' System.out.print((j+1) + " "); } System.out.println(""); } } } OUTPUT 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5

Pascal triangle 3

This program will display the pascal triangle or print star pattern as shown in output.  System.out.print("....."):  are used for display message on screen or console but cursor not move in new line.  System.out.println("....."):  are used for display message on screen or console cursor move in new line. PROGRAM class PascalTriangle3 { public static void main(String[] args) { for(int i=5; i>0; i--) { for(int j=0; j<i; j++) { // This loop will show star '*' System.out.print("* "); } System.out.println(""); } } } OUTPUT C:\>javac PascalTriangle3.java C:\>java PascalTriangle3 * * * * * * * * * * * * * * *

Pascal triangle 2

This program will display the pascal triangle or print star pattern as shown in output.  System.out.print("....."):  are used for display message on screen or console but cursor not move in new line.  System.out.println("....."):  are used for display message on screen or console cursor move in new line. PROGRAM class PascalTriangle2 { public static void main(String[] args) { for(int i=0; i<5; i++) { for(int j=0; j<=i; j++) { // This loop will show star '*' System.out.print("* "); } System.out.println(""); } } } OUTPUT C:\>javac PascalTriangle2.java C:\>java PascalTriangle2 * * * * * * * * * * * * * * *

Pascal triangle 1

This program will display the pascal triangle or print star pattern as shown in output.  System.out.print("....."):  are used for display message on screen or console but cursor not move in new line.  System.out.println("....."):  are used for display message on screen or console, cursor move in new line. PROGRAM class PascalTriangle { public static void main(String args[]) { for(int i=0; i<4; i++) { int k; for(int j=i; j<=5-i; j++) { // This loop will show blank space System.out.print(" "); } for(k=0; k<=i; k++) { // This loop will show left side of triangle System.out.print("* "); } for(int l=0; l<i; l++) { // This loop will show right side of triangle System.out.print("* "); } System.out.println(""); } } } OUTPUT C:\>javac PascalTriangle.java C:\>java PascalTriangle * * * * * * * * * * * * * * * *

Matrix multiplication

This program show how to multiply two matrices A & B into a third matrix C. Here the matrix having 3 rows and 3 columns (3x3). As given below,  inputMatrix( )  and  displayMatrix( )  are two static methods which are used to read and display the matrix values respectively.  java.util.Scanner  class is used to read values from keyboard at runtime. PROGRAM import java.util.Scanner; public class MatrixMultiplication { // Method to read matrix elements public static void inputMatrix(int temp[][]) { Scanner s = new Scanner(System.in); for(int i=0;i<3; i++) { for(int j=0; j<3; j++) { temp[i][j] = s.nextInt(); } } } // Method to display matrix elements public static void displayMatrix(int temp[][]) { for(int i=0;i<3; i++) { for(int j=0; j<3; j++) { System.out.print(temp[i][j] + " "); } System.out.println(""); } } public static void main(String[] args) { int a[][] = new int[3][3]; // First ma...

Matrix addition

This program show how to add two matrices A & B into a third matrix C. Here the matrix having 3 rows and 3 columns (3x3). As given below,  inputMatrix( )  and  displayMatrix( )  are two static methods which are used to read and display the matrix values respectively.  java.util.Scanner  class is used to read values from keyboard at runtime. PROGRAM import java.util.Scanner; class MatrixAddition { public static void inputMatrix(int temp[][]) { Scanner s = new Scanner(System.in); for(int i=0;i<3; i++) { for(int j=0; j<3; j++) { temp[i][j] = s.nextInt(); } } } public static void displayMatrix(int temp[][]) { for(int i=0;i<3; i++) { for(int j=0; j<3; j++) { System.out.print(temp[i][j] + " "); } System.out.println(""); } } public static void main(String[] args) { int a[][] = new int[3][3]; // First matrix int b[][] = new int[3][3]; // Second matrix int c[][] = new int[3][3]; // Final matri...

Check whether given year is Leap year or not

A  leap year  in the Gregorian calendar has an extra day for February. A leap year has 366 days instead the 365 days of a common year. Leap year is a multiple of ‘4’. Leap year has 29 days for February. Normal year has 28 days for February Leap year:  366 days, 52 weeks + 2 odd days Non-Leap year:  365 days, 52 weeks + 1 odd day The standard logic to determine whether a year is leap year or not is: If the year is divisible by 4 and is not divisible by 100 or the year is divisible by 400 then it’s a leap year.  PROGRAM class LeapYear { public static void main(String[] args) { int year = Integer.parseInt(args[0]); boolean isLeapYear = false; if((year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0))) { System.out.println("Year "+year+" is a Leap Year"); } else { System.out.println("Year "+year+" is not a Leap Year"); } } } OUTPUT 1 C:\...

Sorting

Sorting  is nothing but arranging the elements in particular order either ascending or descending order. Here, the  Bubble sort  is used in that the two adjacent elements are compared with each other. If the first number is greater than second number then we swap these two numbers otherwise no swapping of numbers. We use two loops one for 'pass' and other to check for the exchange. In program we read 10 numbers from keyboard and store them in an array.  We have also use here  for-each  version of 'for' loop. The general form of  for-each  loop is given below: for(data_type variable : array | collection) { } PROGRAM import java.util.Scanner; class Sorting { static int arr[] = new int[10]; public static void main(String[] args) { int temp; Scanner s = new Scanner(System.in); System.out.println("Enter 10 elements..."); for(int i=0; i<10; i++) { arr[i] = s.nextInt(); } Syste...

Searching for number

Searching  is process of finding of the specific number to check whether it is present in the given list or not. Here the  linear search  is used in which the number is checked with all the numbers in the list one by one. If the number is matched then it is called as  successful search  otherwise it is called as  unsuccessful search . PROGRAM import java.util.Scanner; class Searching { public static void main(String[] args) { int arr[] = new int[10]; int key; int found=0; Scanner s = new Scanner(System.in); System.out.println("Enter 10 elements..."); for(int i=0; i<10; i++) { arr[i] = s.nextInt(); } System.out.print("Enter the element to search :: "); key = s.nextInt(); for(int i=0; i<10; i++) { // for-each loop if (arr[i] == key) { found = i; break; } } if (found != 0) { System.out.println("Element is found at " + (found+1) + " location"); } else { Syst...

Decimal to Binary number

A  Binary number  is a number expressed in the  binary numeral system  or  base-2 numeral system  which represents numeric values using two different symbols: typically 0 (zero) and 1 (one). In this program, we start with the given decimal number, and repeatedly divide it by 2 (whole number division where the result will be only the quotient, a whole number) and note down the remainder at every step till we obtain the quotient as 0. The remainders obtained in each step when concatenated together (the last remainder taken as the first digit of the binary number) give the binary form of the decimal number. For example, the binary form of decimal number 15 is 1111. PROGRAM class DecimalToBinary { public static void main(String[] args) { int num = Integer.parseInt(args[0]); String binary=""; while (num > 0) { binary = (num % 2) + binary; num = num / 2; } System.out.println("Binary number is :: " + binary); } } OUTPUT 1...

Sum of digits of a number

Below program shows how to find out sum of each digit in the given number logic. For example, if the number is 259, then the sum should be 2+5+9 = 16. PROGRAM import java.util.Scanner; class SumOfDigits { public static void main(String[] args) { int num, sum=0; Scanner input = new Scanner(System.in); System.out.print("Enter the number :: "); num = input.nextInt(); while(num > 0) { sum = sum + (num % 10); num = num / 10; } System.out.println("Sum of digits is :: " + sum); } } OUTPUT 1 C:\>javac SumOfDigits.java C:\>java SumOfDigits Enter the number :: 123 Sum of digits is :: 6 OUTPUT 2 C:\>javac SumOfDigits.java C:\>java SumOfDigits Enter the number :: 456 Sum of digits is :: 15

Find number is prime or not

Prime number  is a number that is greater than 1 and divided by 1 or itself. In other words, prime numbers can't be divided by other numbers than itself or 1. For example 2, 3, 5, 7, 11, 13, 17.... are the prime numbers. To accomplish this we have to check the number is not divided by 2 upto the half of the number. For example, if the number is 7 then we have to check for 2 and 3. Here the number 7 is not divisible by 2 and 3 therefore it is prime number. Note: 0 and 1 are not prime numbers. The 2 is the only even prime number because all the other even numbers can be divided by 2. PROGRAM class PrimeNum { public static void main(String args[]) { int n = Integer.parseInt(args[0]); boolean flag = true; // 0 and 1 are not prime numbers. // The 2 is the only even prime number. if(n < 2) flag = false; else { for(int i=2; i<=n/2; i++) { if(n % i == 0) { flag = false; break; } flag = true; } } if(flag =...

Find number is Armstrong or not

Armstrong number  is a number which is equal to the sum of cubes of its digits. For example, 0, 1, 151, 370, 371, 407, etc. Let's try to understand why 371 is an Armstrong number. 371 = (3*3*3) + (7*7*7) + (1*1*1) = 27 + 343 + 1 = 371 Here 371 = 371 , so it is an Armstrong number. To find ArmStrong number we have to first find out the remainder of number in loop and take the cube of it. After that add the result in 'res' varibale. And at last we compare the original number with 'res', if they are same then it is Armstrong number otherwiese no. PROGRAM class ArmStrongNum { public static void main(String[] args) { int num = Integer.parseInt(args[0]); int n=num,res=0; while(n>0) { res = res + (int)Math.pow(n%10,3); n = n /10; } if(num == res) { System.out.println(num+" == "+res); System.out.println("Hence the "+num+" is an armstrong no."); } else { System.out.println(num+...

Find Palindrome number or not

Palindrome number  is a number that remains the same when its digits are reversed. For example, 121 is the palindrome number because the reverse of 121 is the original number. In given program, we have first calculated the reverse of given number. Then check that number with original number. If both are same then the number is palindrome number otherwise not. PROGRAM class PalindromNum { public static void main(String[] args) { int num = Integer.parseInt(args[0]); int n = num; int rev=0; while(n > 0) { rev = rev *10 +(n%10); n = n / 10; } if(num == rev) { System.out.println(num+" == "+rev); System.out.println("Hence number is a palindrome"); } else { System.out.println(num+" != "+rev); System.out.println("Hence number is not a palindrome"); } } } OUTPUT 1 C:\>javac PalindromNum.java C:\>java PalindromNum 121 121 == 121 Hence number is a palindrome OUTPUT 2 C:\>...

Reverse of a number

Reverse number  is displayed by writing it in reverse order. For example, reverse number of 123 is given as 321. In given program, we use modulus (%) operator to obtain the digits of a number. The output of code is a number obtained by writing original number from right to left. PROGRAM class ReverseNum { int num; ReverseNum(int a) { num = a; } void showRev() { int n = num; int rev = 0; while(n>0) { rev = rev * 10 + (n%10); n = n / 10; } System.out.println("Reverse of "+num+" is "+rev); } public static void main(String args[]) { ReverseNum r = new ReverseNum(551); r.showRev(); } } OUTPUT C:\>javac ReverseNum.java C:\>java ReverseNum Reverse of 551 is 155

Find Fibonacci series upto given levels

Fibonacci series  is the series of numbers in which next number is the sum of previous two numbers. For example, 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, etc. The first two numbers of Fibonacci series are 0 and 1. In the given program, we will print the series upto the levels given by user. After giving 5 as a command line argument it will display only first five numbers in the series. PROGRAM class Fibonacci { public static void main(String[] args) { int a=0,b=1,temp,n; System.out.println("Fibonacci series upto given levels"); System.out.println(a); System.out.println(b); n = Integer.parseInt(args[0]); n = n - 2; while(n > 0) { temp = a + b; a = b; b = temp; n--; System.out.println(temp); } } } OUTPUT 1 C:\>javac Fibonacci.java C:\>java Fibonacci 5 Fibonacci series upto given levels 0 1 1 2 3 OUTPUT 2 C:\>javac Fibonacci.java C:\>java Fibonacci 10 Fibonacci series upto given levels 0 1 1 2 3 5 8 13 21 34 ...