0
Someone can please answer why it gives different result when I write System.out.println outside the body in case of for loops.
8 Antworten
0
can u give example
0
In case of writing table of any number if we write System.out.println outside the body like in this way
for{
tableOf=tableOf*i
}
System.out.println(tableOf);
}
0
You could share your code with us to be easy bring you an answer.
0
public class Program
{
public static void main(String[] args) {
int tableOf=10;
for(int i=1;i<=10;i++){
tableOf*=i;
}
System.out.println(tableOf );
}
}
0
Because you are printing the statement out from the loop. You are only printing the end of the loop but no show the increment of the for.
0
But in case Fibonacci numbers we have to actually write it outside from the loop
0
Here there is a example of a Fibonacci numbers, the outprint is just into the loop for.
public class Fibonacci {
public static void main(String[] args) {
int n = 10, t1 = 0, t2 = 1;
System.out.print("First " + n + " terms: ");
for (int i = 1; i <= n; ++i)
{
System.out.print(t1 + " + ");
int sum = t1 + t2;
t1 = t2;
t2 = sum;
}
}
}
0
Thanks for clearing that