Number Series Programs

 WAP to print Armstrong number from 100 to 500
class Test {
public static void main(String[] a)
{
int temp,r,sum;
for(int i=100; i<=500; i++) {
temp = i;
sum = 0;
while(temp!=0) {
r = temp%10;
sum = sum+(r*r*r);
temp = temp/10;
}
if(sum ==i) {
System.out.print(i+"  ");
}
}
}
}
OUTPUT :
153  370  371  407
 WAP to print the following series 1 2 3 4 ... n
import java.util.Scanner;
class Test {
public static void main(String[] a)
{
Scanner s=new Scanner(System.in);
System.out.print("Enter N: ");
int n = s.nextInt();
for(int i = 1; i<=n; i++) {
System.out.print(i+"  ");
}
}
}
OUTPUT :
Enter N: 10
1  2  3  4  5  6  7  8  9  10
 WAP to print the following series. 2 4 6 8... n
import java.util.Scanner;
class Test {
public static void main(String[] a)
{
Scanner s=new Scanner(System.in);
System.out.print("Enter N: ");
int n = s.nextInt();
for(int i = 2; i<=n; i=i+2) {
System.out.print(i+"  ");
}
}
}
OUTPUT :
Enter N: 15
2  4  6  8  10  12  14
 WAP to print the following series. 1 3 5 7... n
import java.util.Scanner;
class Test {
public static void main(String[] a)
{
Scanner s=new Scanner(System.in);
System.out.print("Enter N: ");
int n = s.nextInt();
for(int i = 1; i<=n; i=i+2) {
System.out.print(i+"  ");
}
}
}
OUTPUT :
Enter N: 16
1  3  5  7  9  11  13  15
 WAP to print the following series. 3 6 9 12... n
import java.util.Scanner;
class Test {
public static void main(String[] a)
{
Scanner s=new Scanner(System.in);
System.out.print("Enter N: ");
int n = s.nextInt();
for(int i = 1; i<=n; i++) {
System.out.print((3*i)+"  ");
}
}
}
OUTPUT :
Enter N: 6
3  6  9  12  15  18
 WAP to print the following series. 1 3 7 15 31... n
import java.util.Scanner;
class Test {
public static void main(String[] a)
{
Scanner s=new Scanner(System.in);
System.out.print("Enter N: ");
int n = s.nextInt();
int x = 0;
for(int i = 0; i< n; i++) {
x = x + (int) Math.pow(2,i);
System.out.print(x+"  ");
}
}
}
OUTPUT :
Enter N: 5
1  3  7  15  31

 WAP to print all prime numbers till 20
class Test {
public static void main(String[] a)
{
int factors;
for(int i = 1; i<=20; i++) {
factors = 0;
for(int j = 1; j<=i; j++) {
if(i%j ==0)
factors++;
}
if(factors ==2) {
System.out.print(i+"  ");
}
}
}
}
OUTPUT :
2  3  5  7  11  13  17  19
 WAP to print the following febonnaci series. 0 1 1 2 3 5 8 13 21 34 upto n times
import java.util.Scanner;
class Test {
public static void main(String[] a)
{
int x = -1, y = 1, z;
Scanner s=new Scanner(System.in);
System.out.print("Enter N: ");
int n = s.nextInt();
for(int i = 1; i<=n; i++) {
z = x+y;
System.out.print(z+"  ");
x = y;
y = z;
}
}
}
OUTPUT :
Enter N: 10
0  1  1  2  3  5  8  13  21  34
 WAP to print the following series.1 2 2 3 3 3 4 4 4 4 5 5 5 5 5
class Test {
public static void main(String[] a)
{
for(int i = 1; i<=5; i++) {
for(int j = 1; j<=i; j++) {
System.out.print(i+"  ");
}
}
}
}
OUTPUT :
1  2  2  3  3  3  4  4  4  4  5  5  5  5  5
 WAP to print the following series. 0 1 3 6 10 15 21... upto n terms
import java.util.Scanner;
class Test {
public static void main(String[] a)
{
Scanner s=new Scanner(System.in);
System.out.print("Enter N: ");
int n = s.nextInt();
int x = 0;
for(int i = 1; i<=n; i++) {
System.out.print(x+"  ");
x = x+i;
}
}
}
OUTPUT :
Enter N: 10
0  1  3  6  10  15  21  28  36  45

#### CONTACT DETAILS

info@prowessapps.in
(8AM to 10PM):

+91-8527238801 , +91-9451396824