Wednesday, April 16, 2014

Sample coding logics using using Java

Problem

Write a Java program that prints the numbers from 1 to 50. But for multiples of three print "Property" instead of the number and for the multiples of five prints "Guru". For numbers which are multiples of both three and five print "PropertyGuru"

Code Snippet

package com;

public class PrintString{
public static void main(String[] args){


for(int i=1;i <=50;i++){
if (i%3==0)
if (i%5==0)
System.out.println("PropertyGuru");

if (i%3==0)
System.out.println("Property");
else if (i%5==0)
System.out.println("Guru");
else 
System.out.println(i);
}
}



}


Challenge!

Do you have better and faster way to solve the problem?

No comments:

Post a Comment