Count the number of divisible numbers in a given range

 Count the number of divisible numbers in a given range

Range: 1-100
Number : 2

Output: 50

import java.util.Scanner;


public class Demo {


public static void main(String[] args) {

int x=1,n,count=0;

Scanner s=new Scanner(System.in);

System.out.println("Enter n value");

n=s.nextInt();

do

{

if(x%n==0)

{

count=count+1;

}

x++;

}while(x<=100);

System.out.println(count);

}



}



Output:

Enter n value

2

50


more programs visit http://sateeshm.com/

Sum Of Even Or Odd Numbers Based On The N value DoWhile Loop

 

  • Sum Of Even Or Odd Numbers Based On The N value DoWhile Loop


import java.util.Scanner;


public class Demo {


public static void main(String[] args) {

int x=1,n,sum=0;

Scanner s=new Scanner(System.in);

System.out.println("Enter n value");

n=s.nextInt();

do

{

if(x%2==0 && n%2==0)

{

sum=sum+x;

}

else if(x%2!=0 && n%2!=0)

{

sum=sum+x;

}

x++;

}while(x<=n);

System.out.println("sum is " +sum);

}



}



output:


Enter n value 5

sum is 9


Enter n value 6

sum is 12

Sum Of Even Numbers DoWhile Loop

  Sum Of Even Numbers DoWhile Loop 


import java.util.Scanner;


public class Demo {


public static void main(String[] args) {

int x=1,n,sum=0;

Scanner s=new Scanner(System.in);

System.out.println("Enter n value");

n=s.nextInt();

do

{

if(x%2==0)

{

sum=sum+x;

}

x++;

}while(x<=n);

System.out.println(sum);

}



}


Output:

Enter n value

5

6


Sum Of Natural Numbers DoWhile Loop

 

  • Sum Of Natural Numbers DoWhile Loop

import java.util.Scanner;


public class Demo {


public static void main(String[] args) {

int x=1,n,sum=0;

Scanner s=new Scanner(System.in);

System.out.println("Enter n value");

n=s.nextInt();

do

{

sum=sum+x;

x++;

}while(x<=n);

System.out.println(sum);

}



}



output:

Enter n value

5

15

Factorial Number Using DoWhile Loop -up to n Numbers

  

  • Factorial Number Using DoWhile Loop(up to n Numbers)

import java.util.Scanner;


public class Demo {


public static void main(String[] args) {

int x=1,n,fact=1;

Scanner s=new Scanner(System.in);

System.out.println("Enter n value");

n=s.nextInt();

do

{

fact=fact*x;

System.out.println(x+" Factorial =" +fact);

x++;

}while(x<=n);

}



}


Output:

Enter n value

5

1 Factorial =1

2 Factorial =2

3 Factorial =6

4 Factorial =24

5 Factorial =120

More Programs visit : http://sateeshm.com/

Factorial Number Using DoWhile Loop

 

  • Factorial Number Using DoWhile Loop

import java.util.Scanner;


public class Demo {


public static void main(String[] args) {

int x=1,n,fact=1;

Scanner s=new Scanner(System.in);

System.out.println("Enter n value");

n=s.nextInt();

do

{

fact=fact*x;

x++;

}while(x<=n);

System.out.println(fact);

}



}


Output:

Enter n value

5

120

More Programs visit : http://sateeshm.com/

Display Even Number or Odd Number Based On The N Value Using DoWhile Loop using Single Loop

  

  • Display Even Number or Odd Number Based On The N Value Using DoWhile Loop using Single Loop


import java.util.Scanner;


public class Demo {


public static void main(String[] args) {

int x=1,n;

Scanner s=new Scanner(System.in);

System.out.println("Enter n value");

n=s.nextInt();

do

{

if(x%2==0 && n%2==0)

{

System.out.println(x);

}

else if(x%2==0 && n%2==0)

{

System.out.println(x);

}

x++;

}while(x<=n);

}



}




output:

Enter n value

10

2

4

6

8

10


Enter n value

11

1

3

5

7

9

11


More information visit:http://sateeshm.com/

Display Even Number or Odd Number Based On The N Value Using DoWhile Loop

 

  • Display Even Number or Odd Number Based On The N Value Using DoWhile Loop


import java.util.Scanner;


public class Demo {


public static void main(String[] args) {

int x=1,n;

Scanner s=new Scanner(System.in);

System.out.println("Enter n value");

n=s.nextInt();

if(n%2==0)

{

do

{

if(x%2==0)

{

System.out.println(x);

}

x++;

}while(x<=n);

}

else

{

do

{

if(x%2!=0)

{

System.out.println(x);

}

x++;

}while(x<=n);

}



}


}


output:

Enter n value

10

2

4

6

8

10


Enter n value

11

1

3

5

7

9

11


More information visit:http://sateeshm.com/