Write a Java program to print numbers between 1 to 100 which are divisible by 3, 5 and by both.

 /* 

Write a Java program to print numbers between 1 to 100 which are divisible by 3, 5 and by both.

Sample Output:

Divided by 3:

3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36, 39, 42, 45, 48, 51, 54, 57

, 60, 63, 66, 69, 72, 75, 78, 81, 84, 87, 90, 93, 96, 99,

Divided by 5:

5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90,

95,

Divided by 3 & 5:

15, 30, 45, 60, 75, 90, */


class Eleven

{

public static void main(String arg[])

{

int  a=0, b=0,c, d,e=0;

int arr1[] = new int[50];

int arr2[] = new int[50];


for(int i=1 ; i<100 ; i++)

{

c = i % 3;

d = i % 5;

if(c == 0)

{

arr1[a] = i;

a++;

}

    if(d == 0)

    {

arr2[b] = i;

b++;

    }    

}


System.out.print("\n Divisible by 3 :");


for(int j=0 ; j<a ; j++)

{

System.out.print(""+ arr1[j]+"," );

}


System.out.print("\n\n Divisible by 5 : ");


for(int j=0 ; j<b; j++)

{

  System.out.print(""+ arr2[j]+", " );

     }


     System.out.print("\n\n Divisible by both :");


for(int x=0 ; x<=a ; x++)

{

for(int y=0 ; y<b ; y++)

{

if(arr1[x]==arr2[y])

{

System.out.print("" + arr1[x]+ ",");

}

}

}


System.out.print("\n");

}

}

More Information on our channel Programmer Community

please like , share and subscribe to get notifications of new videos....

To understand this program video will coming soon on our you tube channel



Comments

Popular posts from this blog

Design and Develop a java program to read marks of a student and print the total and average of marks using scanner class.

Write a Java program to convert seconds to hour, minute and seconds.

Write a Java program to print the ascii value of a given character.