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

Pattern 31 & 32