Here i am posting a very interesting java program.
This code will print numbers from 1 to 100 without using loop, conditional statement, and relational operator.
In this code i am using recursion.
Lets understand the logic.
## printing value of variable i
## variable i is incremented and variable j is decremented
## divide value of variable i with value of variable j
## if value of variable j is zero an run-time error (exception) will be generated and it will be handled inside catch block
public class Print1To100WithoutLoop
{
static int i = 1;
static int j = 100;
public static void main(String[] args)
{
if(i==1)
{
System.out.println("Numbers from 1 to 100 =>");
}
try
{
System.out.println("\t\t\t"+i);
i++;
j--;
int k = i/j;
/*
calling main method imside main, so here we are using recursion
*/
main(args);
}
catch (Exception e)
{
System.out.println("\t\t\tThanks");
}
}
}
This code will print numbers from 1 to 100 without using loop, conditional statement, and relational operator.
In this code i am using recursion.
Lets understand the logic.
## printing value of variable i
## variable i is incremented and variable j is decremented
## divide value of variable i with value of variable j
## if value of variable j is zero an run-time error (exception) will be generated and it will be handled inside catch block
public class Print1To100WithoutLoop
{
static int i = 1;
static int j = 100;
public static void main(String[] args)
{
if(i==1)
{
System.out.println("Numbers from 1 to 100 =>");
}
try
{
System.out.println("\t\t\t"+i);
i++;
j--;
int k = i/j;
/*
calling main method imside main, so here we are using recursion
*/
main(args);
}
catch (Exception e)
{
System.out.println("\t\t\tThanks");
}
}
}
No comments:
Post a Comment