Monday, 31 August 2020

Sum Of First N Natural Numbers(for loop)

Write a Java Program to find the Sum Of First N Natural Numbers

Input: 7
output: 28

Code in Java:

import java.util.Scanner;
public class SumOfFirstNNaturalNumers 
{
	public static void main(String[] args) 
	{
		Scanner scan = new java.util.Scanner(System.in);
		System.out.println("Enter the N: ");
		int N=scan.nextInt();
		int sum=0;
		for(int i=1; i<=N; i++)
		{
			sum=sum+i;
		}
		System.out.println("Sum of First "+ N +" Natural Numbers: ");
		System.out.println("="+sum);
	}
}
  
Output:
Enter the N: 
10
Sum of First 10 Natural Numbers: 
=55



Sum Of N Natural Numbers From Given Range

Write a Java Program to find the Sum Of N Natural Numbers From Given Range

Input:  5 to 15
output: 110

Code in Java:

import java.util.Scanner;
public class SumOfNaturalNumbersFromGivenRange 
{
	public static void main(String[] args) 
	{
		Scanner scan = new java.util.Scanner(System.in);
		System.out.println("Enter the starting number: ");
		int n1=scan.nextInt();
		System.out.println("Enter the end number: ");
		int n2=scan.nextInt();
		int sum=0;
		for(int i=n1; i<=n2; i++)
		{
			sum=sum+i;
		}
		System.out.println(sum);
	}
}
  
Output:
Enter the starting number: 
11
Enter the end number: 
20
155



Write a JAVA program to Find the Sum Of First N Natural Numbers

Sum Of Even Numbers from Given range

Write a Java Program to find the Sum Of Even Numbers from Given range

Input: 10 to 20
output: 90

Code in Java:

import java.util.Scanner;
public class SumOfEvenNumbers 
{
	public static void main(String[] args) 
	{
		Scanner scan = new java.util.Scanner(System.in);
		System.out.println("Enter the starting number: ");
		int n1=scan.nextInt();
		System.out.println("Enter the end number: ");
		int n2=scan.nextInt();
		int sum=0;
			for(int i=n1; i<=n2; i++)
			{
				if(i%2==0)
				{
					System.out.println(i);
					sum=sum+i;
				}
			}
		System.out.println("Sum of Even Numbers from given range: ");
		System.out.println("= "+sum);
	}
}
Output:
Enter the starting number: 
1
Enter the end number: 
10
2
4
6
8
10
Sum of Even Numbers from given range: 
= 30


Write a JAVA program to Find the Sum Of First N Natural Numbers

Sum Of Digits Of Number

Write a Java Program to find the Sum Of Digits Of Number

Input: 2461
output: 13

Code in Java:

  
import java.util.Scanner;
public class SumOfDigitsOfNumber 
{
	public static void main(String[] args) 
	{
		Scanner scan = new java.util.Scanner(System.in);
		System.out.println("Enter the N: ");
		int N=scan.nextInt();
		int reminder,sum=0;
		 while(N>0)
		 {
			 reminder=N%10;
			 sum=sum+reminder;
			 N=N/10;
		 }
		 System.out.println("Sum of Digits: "+sum);	
	}
}
  
Output:
Enter the N: 
456789
Sum of Digits: 39



Write a JAVA program to Find the Sum Of First N Natural Numbers

Sum Of Prime Numbers From Given Range

Write a Java Program to find the Sum Of Prime Numbers From Given Range 

Input:
Enter the starting number: 
5
Enter the end number: 
20
Output:
Prime nos from Given Range :
5
7
11
13
17
19
Sum of Prime Numbers From Given Range= 72


Code in Java:

import java.util.Scanner;
public class SumOfPrimrNumbersFronGivenRange 
{
	public static void main(String[] args) 
	{
		Scanner scan = new java.util.Scanner(System.in);
		System.out.println("Enter the starting number: ");
		int n1=scan.nextInt();
		System.out.println("Enter the end number: ");
		int n2=scan.nextInt();
		int count,sum=0;
		System.out.println("Prime nos from Given Range :");
		//condition used to identify number is odd or even
			for(int i=n1; i<=n2; i++)
			{
				count=0;
				for(int j=1; j<=i; j++)
				{
					if(i%j==0)
					{
						count++;
					}
				}
				if(count==2)
				{ 
					System.out.println(i);	
					sum=sum+i;
				}
			}
	System.out.println("Sum of Prime Numbers From Given Range= "+sum);
	}
}
  

Output:
Enter the starting number: 
5
Enter the end number: 
20
Prime nos from Given Range :
5
7
11
13
17
19
Sum of Prime Numbers From Given Range= 72



Write a JAVA program to Find the Sum Of First N Natural Numbers

Sum Of Odd Numbers from Given range

Write a Java Program to find the Sum Of Odd Numbers from Given range

Input: 5 to 22
output: 117

  
import java.util.Scanner;
public class SumOfOddNumbers 
{
	public static void main(String[] args) 
	{
		Scanner scan = new java.util.Scanner(System.in);
		System.out.println("Enter the starting number: ");
		int n1=scan.nextInt();
		System.out.println("Enter the end number: ");
		int n2=scan.nextInt();
		int sum=0;
			for(int i=n1; i<=n2; i++)
			{
				if(i%2!=0)
				{
					System.out.println(i);
					sum=sum+i;
				}
			}
		System.out.println("Sum of Odd Numbers from given range: ");
		System.out.println("= "+sum);
	}
}
Output:
Enter the starting number: 
20
Enter the end number: 
30
21
23
25
27
29
Sum of Odd Numbers from given range: 
= 125



Write a JAVA program to Find the Sum Of First N Natural Numbers


prime numbers from 1 to N

Write a Java program to find Number is Prime number or not from 1 To N.

Input: 
Enter the N: 20
Output:
Prime nos from Given Range :
2
3
5
7
11
13
17
19
no of prime number in list: 8

Code In java:
import java.util.Scanner;
public class Prime{
public static void main(String[] args) {
Scanner scan = new java.util.Scanner(System.in);
System.out.println("Enter the N: ");
int N=scan.nextInt();
int count,count2=0,temp;
System.out.println("Prime nos from Given Range :");
//condition used to identify number is odd or even
for(temp=1;temp<=N;temp++)
                {
count=0;
for(int j=1; j<=temp; j++)
                        {
if(temp%j==0)
                                {
count++;
}
}
if(count==2)
                        
System.out.println(temp);
count2++;
}
}
                 System.out.println("no of prime number in list: " + count2);
}
}
Output:
Enter the N: 
20
Prime nos from Given Range :
2
3
5
7
11
13
17
19
no of prime number in list: 8



Count Prime nos from a Given Range

Write a program to find Number is Prime number or not from a given range 

Input: Enter the First number: 
20
Enter the Last number: 
50
Output: Prime nos from Given Range :
23
29
31
37
41
43
47
no of prime number in list: 7

Code In Java:
import java.util.Scanner;
public class Prime
{
public static void main(String[] args) 
{
Scanner scan = new java.util.Scanner(System.in);
System.out.println("Enter the First number: ");
int first=scan.nextInt();
System.out.println("Enter the Last number: ");
int last=scan.nextInt();
int count,count2=0,temp;
System.out.println("Prime nos from Given Range :");
//condition used to identify number is odd or even
for(temp=first;temp<=last;temp++)
{
count=0;
for(int j=1; j<=temp; j++)
{
if(temp%j==0)
{
count++;
}
}
if(count==2)

System.out.println(temp);
count2++;
}
}
System.out.println();
System.out.println("no of prime number in list: " + count2);
}
}
Output:
Enter the First number: 
20
Enter the Last number: 
50
Prime nos from Given Range :
23
29
31
37
41
43
47
no of prime number in list: 7


Write a JAVA program to Find the Sum Of First N Natural Numbers





Saturday, 29 August 2020

prime number or not (using do-while loop).

Write a program to find Number is Prime number or not using do-while loop

Input:
Enter the Number  17
output:
Number is prime

Code in Java:
import java.util.Scanner;
public class Prime{
public static void main(String[] args) {
Scanner scan = new java.util.Scanner(System.in);
System.out.println("Enter the number: ");
int number=scan.nextInt();
int count=0;int i=1;
//condition used to identify number is prime or not
do
{
if(number%i==0)
{
count++;
} i++;
}while(i<=number);
if(count==2)
{
System.out.println(number + " is Prime number");
}
else
{
System.out.println(number + " is not Prime number");
}
}
}
Output:
Enter the number: 
67
67 is Prime number



prime number or not (using while loop).

Write a program to find Number is Prime number or not using while loop

Input:
Enter the Number  17
output:
Number is prime

Code in Java:
import java.util.Scanner;
public class Prime{
public static void main(String[] args) {
Scanner scan = new java.util.Scanner(System.in);
System.out.println("Enter the number: ");
int number=scan.nextInt();
int count=0;int i=1;
//condition used to identify number is prime or not
while(i<=number)
{
if(number%i==0)
{
count++;
} i++;
}
if(count==2)
{
System.out.println(number + " is Prime number");
}
else
{
System.out.println(number + " is not Prime number");
}
}
}
Output:
Enter the number: 
96
96 is not Prime number


Write a JAVA program to Find the Sum Of First N Natural Numbers

prime number or not (using for loop).

Write a program to find Number is Prime number or not using for loop

Input:
Enter the Number  12
output:
Number is  Not prime

Code in Java:
import java.util.Scanner;
public class Prime{
public static void main(String[] args) {
Scanner scan = new java.util.Scanner(System.in);
System.out.println("Enter the number: ");
int number=scan.nextInt();
int count=0;
//condition used to identify number is prime or not
for(int i=1; i<=number; i++)
{
if(number%i==0)
{
count++;
}
}
if(count==2)
{
System.out.println(number + " is Prime number");
}
else
{
System.out.println(number + " is not Prime number");
}
}
}
Output:
Enter the number: 
51
51 is not Prime number


odd even(using do-while loop)

Write a program to find Number is a Odd or Even from 1 to N using do-while loop.
import java.util.Scanner;
public class EvenOdd {
	public static void main(String[] args) 
	{
		Scanner scan = new java.util.Scanner(System.in);
		System.out.println("Enter the N: ");
		int n=scan.nextInt();
		int i=1;
		//condition used to identify number is odd or even
		System.out.println("Even Numbers:");
		do	
		{
			if(i%2==0)
			{
				System.out.println(i);
			}
			i++;
		}while(i<=n);
		System.out.println("Odd Numbers:");
		i=1;
		do
		{
			if(i%2!=0)
			{
				System.out.println(i);
			}
			i++;
		}	while(i<=n);
    }
}


Output:
Enter the N: 
6
Even Numbers:
2
4
6
Odd Numbers:
1
3
5
 
  

odd even (using while loop)

Write a program to find Number is a Odd or Even from 1 to N using while loop.
  import java.util.Scanner;
public class EvenOdd 
{
	public static void main(String[] args) 
	{
		Scanner scan = new java.util.Scanner(System.in);
		System.out.println("Enter the N: ");
		int n=scan.nextInt();
		int i=1;
		//condition used to identify number is odd or even
		System.out.println("Even Numbers:");
		while(i<=n)	
		{
			if(i%2==0)
			{
				System.out.println(i);
			}
			i++;
		}
		System.out.println("Odd Numbers:");
		i=1;
		while(i<=n)	
		{
			if(i%2!=0)
			{
				System.out.println(i);
			}
			i++;
		}
    }
}


Output:
Enter the N: 
7
Even Numbers:
2
4
6
Odd Numbers:
1
3
5
7


  

Odd Even Numbers From Given Range

Write a program to find Number is a Odd number or Even number From Given Range.
import java.util.Scanner;
public class EvenOdd 
{
	public static void main(String[] args) 
	{
		int even_count=0,odd_count=0;
		Scanner scan = new java.util.Scanner(System.in);
		System.out.println("Enter the First Number: ");
		int n1=scan.nextInt();
		System.out.println("Enter the Second Number: ");
		int n2=scan.nextInt();
		//condition used to identify number is odd or even
		System.out.println("Even no List");
		for(int i=n1; i<=n2;i++)	
		{
			if(i%2==0)
			{
				even_count++;
				System.out.println(i);
			}
		}
		System.out.println("Odd no List");
		for(int i=n1; i<=n2;i++)	
		{
			if(i%2!=0)
			{
				odd_count++;
				System.out.println(i);
			}
		}
		System.out.println("Even count:  "+even_count);
		System.out.println("Odd count:  "+odd_count);
	}
}

Output:
Enter the First Number: 
10
Enter the Second Number: 
20
Even no List
10
12
14
16
18
20
Odd no List
11
13
15
17
19
Even count:  6
Odd count:  5


  

Odd Even numbers from 1 to N

Write a program to find Number is a Odd number or Even number from 1 to N.
  import java.util.Scanner;
public class EvenOdd 
{
	public static void main(String[] args) 
    {
		int even_count=0;
		int odd_count=0;
		Scanner scan = new java.util.Scanner(System.in);
		System.out.println("Enter the N: ");
		int n=scan.nextInt();
		//condition used to identify number is odd or even
		for(int i=1; i<=n;i++)	
        {
			if(i%2==0)
			{
				even_count++;
				System.out.println(i + " is Even number");
			}
			else
			{
				odd_count++;
				System.out.println(i + " is odd number");
			}
		}
		System.out.println("Even count:  "+even_count);
		System.out.println("Odd count:  "+odd_count);
	}
}

Output:
Enter the N: 
10
1 is odd number
2 is Even number
3 is odd number
4 is Even number
5 is odd number
6 is Even number
7 is odd number
8 is Even number
9 is odd number
10 is Even number
Even count:  5
Odd count:  5


  

Odd Even number from given Array

Write a program to find Number is a Odd number or Even number using Array.
  import java.util.Scanner;
public class EvenOdd {
	public static void main(String[] args) {
		int even_count=0,odd_count=0;
		Scanner scan = new java.util.Scanner(System.in);
		System.out.println("Enter the Array size: ");
		int size=scan.nextInt();
		int [] number=new int[size];
		System.out.println("Enter the elements of an Array: ");
			for(int i=0;i<size;i++)
            {
				number[i]=scan.nextInt();
			}
			//condition used to identify number is odd or even
			for(int i=0; i<size;i++)	
			{
				if(number[i]%2==0)
				{
				even_count++;
				System.out.println(number[i] + " is Even number");
				}
				else
				{
				odd_count++;
				System.out.println(number[i] + " is odd number");
				}
			}
		System.out.println("Even Number Count of an array:- " + even_count);
		System.out.println("Odd Number Count of an array:- " + odd_count);
	}
}

Output:
Enter the Array size: 
6
Enter the elements of an Array: 
15
96
78
35
62
45
15 is odd number
96 is Even number
78 is Even number
35 is odd number
62 is Even number
45 is odd number
Even Number Count of an array:- 3
Odd Number Count of an array:- 3


  

Odd Even using bitwise Operator..

Identify Odd or Even Number in a way is using bit-wise Operator.
import java.util.Scanner;
public class EvenOdd 
{
	public static void main(String[] args) 
	{
		Scanner scan = new java.util.Scanner(System.in);
		System.out.println("Enter the number: ");
		int number=scan.nextInt();
		//condition used to identify number is odd or even
		if((number/2)*2==number)
		{
			System.out.println(number + " is Even number");
		}
		else
		{
			System.out.println(number + " is odd number");
		}
	}
}
Output:
Enter the number: 
64
64 is Even number


 

Odd or Even number identify in simple way

Write a program to find Number is a Odd number or Even number.
import java.util.Scanner;
public class EvenOdd 
{
	public static void main(String[] args) 
	{
		Scanner scan = new java.util.Scanner(System.in);
		System.out.println("Enter the number: ");
		int number=scan.nextInt();
		//condition used to identify number is odd or even
		if(number%2==0)
		{
			System.out.println(number + " is Even number");
		}
		else
		{
			System.out.println(number + " is odd number");
		}
	}
}

Output:
Enter the number: 
64
64 is Even number  


  

Monday, 24 August 2020

Z

 

#Program to Print alphabet 'Z' in Star Pattern

Expected Output:

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


Code in Java:
public class Z 
{
	public static void main(String[] args) 
	{
		for(int i=1; i<=5; i++)//Outer loop for number of rows
		{
			for(int j=1; j<=5;j++)//inner loop for number of columns
			{
			//if condition to print the Z pattern Stars
			if(i==1||i==5||i==2&&j==4||i==4&&j==2||i==3&&j==3)
			{
				System.out.print("* ");//printing stars
			}
			else
			{
				System.out.print("  ");//printing white spaces
			}
			}
		System.out.println();
		}
	System.out.println();//printing newline after each row
	}
}      
      

Pattern 31 & 32