Showing posts with label Programs in Java. Show all posts
Showing posts with label Programs in Java. Show all posts

Thursday, 11 February 2016

Print Star Pattern in Java

Print Star Pattern in Java

In java programming you can easily print star pattern in java. For this code you need to follow same concept which is used in java only change syntax of C language into Java.

Print Star Pattern in Java

Pattern

*
**
***
****
*****
******
*******
********
*********
**********

Example To Print Triangle of Star in Java

public class StarDemo
{
public static void main(String[] args)
{
for(int i = 0; i < 10; i++)
{
for(int j=i+1;j>0;j--)
{
System.out.print("*");
}
System.out.println();
}
}
}

Pattern

********
*******
******
*****
****
***
**
public class StarDemo
{
public static void main(String[] args)
{
for(int i = 0; i < 10; i++)
{
for(int j=i+1;j>0;j--)
{
System.out.print("*");
}
System.out.println();
}
}
}

Sunday, 7 February 2016

Palindrome Number Program in Java

Palindrome Number Program in Java

Hello, friends, Hitesh is here; Here we will write the complete code of Palindrome Number Program in Java. This type of number is the same when you reverse it, here some tips and concepts are applied which is used in C programming only change the syntax.

Palindrome Number Program in Java

Code of Java Program to Check Number is Palindrome or Not

import java.util.*;
public class PalindromeExample
{
 public static void main(String s[])
 {
 int temp=0,d=0,rem=0;
 temp=t;
System.ot.println("Enter any number: ");
 Scanner c= new Scanner(System.in);
 int t=c.nextInt();
 while(t>0)
 {
d=t%10;
 rem=rem*10+d; t=t/10;
 }
 System.out.println("Reverse of Number is "+rem);
 if(r==temp)
 System.out.println("Number is Palindrome");
 else
 System.out.println("Number is not Paliundrome");
 }
}

Output:

Enter any number:  121
Number is Palindrome

Prime number Program in Java

Prime number Program in Java

Hello friends; Hitesh is here, Today we will learn about how to create Prime number Program in Java. For this program you need to follow normal concept of C language which is already you learn only need to change syntax of C language into Java Syntax. Prime number is only divisible by 1 and itself number.
Prime number Program in Java


class PrimeExample
{
public static void main(String args[])
{
int i,m=0,flag=0;
int n=21;  // Check 21 is prime number or not
m=n/2;
for(i=2;i<=m;i++)
{
if(n%i==0)
{
System.out.println("Number is not prime");
flag=1;
break;
}
}
if(flag==0)
System.out.println("Number is prime");
}
}

Output:

Number is not prime number

I hope this code is helpful for you, but this is normal code for more detail you need to visit our website www.tutorial4us.com. 

Thursday, 4 February 2016

Armstrong program in Java

Armstrong program in Java

Hello friends; Hitesh is here today we write on simple code for Armstrong program in Java. Here the main things are to check the given number is Armstrong or not by using Java programming. For this program no need new concept just follow the same algorithm like C language only changes the syntax of C to Java Language.

Armstrong program in Java

Java Program to Check Number is Armstrong or Not

class ArmstrongNumber
{
public static void main(String[] args)
 {
int c=0,a,temp;
int n=153; // check armstrong for 153
temp=n;
while(n>0)
{
a=n%10;
n=n/10;
c=c+(a*a*a);
}
if(temp==c)
System.out.println("This is Armstrong number");
else
System.out.println("Not Armstrong number");
}
}

Output:

This is Armstrong number
I hope this code is helpful for you; for more detail and other same code visit our website.

Tuesday, 2 February 2016

Star Pattern in Java | Print Triangle of star in Java

Print Star Pattern in Java

Hello, today we write for for print Star Pattern in Java. This is very simple you need to follow C language concept to print triangle of star only change syntax of C programming to Java syntax programming.

Print Star Pattern in Java

Code to print Star pattern Program in Java:

class StarsPattern
{
 public static void main(String[] args)
{
 int row, numberOfStars;
 for (row = 1; row <=5; row++)
{
 for(numberOfStars = 1; numberOfStars <= row; numberOfStars++)
 {
 System.out.print("*");
 }
 System.out.println(); // Used for Go to next line
 }
 }
 }

Output:

*
**
***
****
*****

Factorial Program in Java

Factorial Program in Java

Hello, friends, hitesh is here today we will write Factorial Program in Java; Here same concept is applied like C language, The main point to notice is; change the syntax of C with Java. Use System.out.print(); in place of printf();

Factorial Program in Java

Find Factorial of Any Number Program in Java Code

public class FactorialPro
{
 public static void main(String args[])
{
 for (int counter = 0; counter <=10; counter++)
{
 System.out.printf("%d! = %d\n", counter, factorial(counter));
 }
 } public static long factorial(long number)
{ if (number <= 1) return 1;
 else return number * factorial(number - 1);
 }
}

Output:

0! = 1
1! = 1
2! = 2
3! = 6
4! = 24
5! = 120
6! = 720
7! = 5040
8! = 40320
9! = 362880
10! = 3628800