For Loop

Declare multiple variables in for loop Example

Want to learn quickly?
Try one of the many quizzes. More than Java 400 questions with detailed answers.

85 Comments

    • public class triangle {

      /**
      * @param args
      */
      public static void main(String[] args) {
      // TODO Auto-generated method stub
      int n=7;
      for(int i=1;i<n;i++) {=”” for(int=”” j=”0;j&lt;i;j++)” {=”” system.out.print(j+”=”” “);=”” }=”” system.out.println(“=”” “);=”” }=”” }=”” }=””>

    • public class piramid
      {
      public static void main(String arys[])
      {
      for(int i=0;i<5;i++)
      {
      for(int j=i;j<5;j++)

      System.out.print(‘*’);
      System.out.println();
      }
      }
      }

  • [code]
    for (int i = 5; i > 0; i -= 2) {
    for (int j = 0; j < i; j++) {
    System.out.print(‘*’);
    }
    System.out.println();
    }
    [/code]

  • [code]
    void makePyramid()
    {
    for (int i = 0; i < 5; i++)
    {
    for (int j = i; i < 5; j++)
    System.out.print(‘*’);
    System.out.println();
    }
    }
    [/code]

    • for(i=1;i<=n;i++)
      {
      for(j=1;j<=i;j++)
      System.out.print(“*”);
      System.out.print(“
      “);
      }
      for(i=1;i<n;i++) {=”” for(j=”i;j&lt;n;j++)” system.out.print(“*”);=”” system.out.print(“=”” “);=”” }=””>

  • [code]
    for(int i=5; i>0; i–){
    for(int x=0; x<i; x++{=”” system.out.print(“*”);=”” }=”” if(i!=”1)” system.out.println();=”” avoid=”” printing=”” line=”” if=”” final=”” output=”” }=”” [=”” code]=””>

    • S=3+4+6+9+…+Tn
      S= 3+4+6+…+T(n-1)+Tn
      ————————-(subtracting)
      0=3+1+2+3+….+(n-1)-Tn
      which means
      Tn=3+(n*(n-1))/2

      for(int i=1;i<=6;i++)
      {
      int x=3+(i*(i-1))/2;
      System.out.println(x);
      }

    • import java.util.*;
      import java.text.*;
      class pr
      {

      public static void main(String args[])
      {
      Scanner input=new Scanner(System.in);
      int k=1;
      for(int i=1;i<=7;i++)
      {
      int j=2;
      System.out.print(j +k);
      k=k+i;
      System.out.println();
      }

      }
      }

  • how can you make this? please i need to know.. thanks…
    *********
    **** ****
    *** ***
    ** **
    * *
    ** **
    *** ***
    **** ****
    *********

    • [code]
      public class PrintSequenceAddition {

      public void createSeq(){
      for(int i=0; i<=9; i++){
      System.out.println(i +”+” +(i*2) +”=” +(i + (i*2)));
      }
      }

      public static void main(String[] args) {
      PrintSequenceAddition psa = new PrintSequenceAddition();
      psa.createSeq();
      }
      }
      [/code]

    • class New1

      {

      public static void main(String[] args)

      {

      for(int a = 1;a <= 4;a++){for(int b = 1;b <= a;b++)

      {System.out.print(a);} System.out.println();}

      for(int c = 3;c >= 1;c–){for(int d = 1;d <= c;d++)

      {System.out.print(c);}System.out.println();}

      }

      }

    • [code]
      public class JavaPyramid2 {

      public static void main(String[] args) {

      for(int i=5; i>0 ;i–){

      for(int j=0; j < i; j++){
      System.out.print(“#”);
      }

      //generate a new line
      System.out.println(“”);
      }
      }
      }
      [/code]

    • for(int i=1;i<4;i++)
      {
      for(int j=0;j<i;j++) {=”” system.out.print(‘j’);=”” }=”” system.out.println();=”” }=””>

  • how to produce this output?
    Enter a string: abba
    The reverse of the entered string is abba.
    The entered strig is a palindrome

    • import java.util.*;
      public class palindrome {

      static Scanner w = new Scanner(System.in);
      public static void main(String args[]){

      for(int i = 1; i > 0; i++){

      String strOriginalA;
      String strOriginalB;

      System.out.print(“Enter your word : “);
      strOriginalA = w.next();

      strOriginalB = new StringBuffer(strOriginalA).reverse().toString();

      System.out.println(“Palindrome : ” + strOriginalB);

      if(strOriginalA.equalsIgnoreCase(strOriginalB))
      System.out.println(“TRUE”);
      else
      System.out.println(“False”);

      }
      }
      }

  • I have to loop a program containing if statements, 3 times. a double has to be input by the user and the output has to show either POSITIVE, NEGATIVE or ZERO>

    Please help

    • public class diamond
      {
      public static void main(String args[])
      {
      int i , j;
      for (i=0;i<=5;i++)
      {
      for (j=0;j<i;j++) {=”” system.out.print(“*”);=”” }=”” system.out.println(“”);=”” }=”” }=”” }=”” }=””>

      • class New1

        {

        public static void main(String[] args)

        {

        for(int a = 1;a <= 7;a+=2){for(int b = 1;b <= a;b++)

        {System.out.print(“*”);} System.out.println();}

        for(int c = 5;c >= 1;c-=2){for(int d = 1;d <= c;d++)

        {System.out.print(“*”);}System.out.println();}

        }

        }

    • class Star

      {

      public static void main(String a[])

      {

      Character x=0;

      Character y=0;

      while (x<5)

      {

      x++;

      y=0;

      while(y<x) {=”” y++;=”” system.out.print(“*”);=”” }=”” system.out.print(“\n”);=”” }=”” {=”” character=”” n=”5;” character=”” m=”0;” while=”” (0<n)=”” {=”” n–;=”” m=”0;” while(m<n)=”” {=”” m++;=”” system.out.print(“*”);=”” }=”” system.out.print(“\n”);=”” }=”” }=”” }=”” }=””>

      • This is wrong, it would give an output something like
        1
        1
        1
        1
        1
        2
        2
        2
        2
        2
        ….5
        The correct answer should be
        for(int a=1; a<=5;a++){
        for(int b=1;b<=a;b++){
        System.out,printf(a);
        }
        system.out.println(“”);
        }

    • import java.util.*;
      class NumberPeramid
      {
      public static void main(String args[])
      {
      Scanner s = new Scanner(System.in);
      System.out.println(“Enter the number”);
      int x=s.nextInt();

      for(int i=1; i<=x; i++)
      {
      for(int j=1; j<=i; j++)
      {
      System.out.print(i);
      }
      System.out.println (” \n “);
      }
      }
      }

    • for(int z = 9 ; z >=1 ; z –)

      {

      for(int x = 9 ; x >=z ; x –)

      {

      System.out.print(“*”);

      }

      System.out.println(“”);

      }

      output :

      *
      **
      ***
      ****
      *****
      ******
      *******
      ********
      *********

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

      for(int i=1;i<10;i++)
      {
      for(int j=0;j<i;j++) {=”” system.out.print(“*”);=”” }=”” system.out.println();=”” }=”” }=”” }=””>

    • for(int z =1 ; z <=5 ; z++)
      {
      for(int x = 1 ; x <=5-z ; x++)
      {
      System.out.print(“@”);
      }
      for(int x = 1 ; x <=z ; x++)
      {
      System.out.print(“*”);
      }
      System.out.println();
      }

  • create a java programm that will accept an integer number and will display the FF:
    Example:
    Enter Starting number: 1
    Enter Ending Number : 20
    enter step number : 2
    output must be

    1
    2

    3
    4

    5
    6

    7
    8

    9
    10

    11
    12

    13
    14

    15
    16

    17
    18

    19
    20

    thx for those who help me…

  • code with this output///

    0
    1, 0
    2, 1, 0
    3, 2, 1, 0
    4, 3, 2, 1, 0
    5, 4, 3, 2, 1, 0
    6, 5, 4, 3, 2, 1, 0
    7, 6, 5, 4, 3, 2, 1, 0
    8, 7, 6, 5, 4, 3, 2, 1, 0
    9, 8, 7, 6, 5, 4, 3, 2, 1, 0
    10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0

    • for (int i = 10; i >= 0; i–) {
      for (int j = (10 – i); j >= 0; j– ) {
      System.out.print(j);
      if (j != 0) System.out.print(“, “);
      }
      System.out.println();
      }

      • how can i make an out put like this…that exchanges the values in two variables.

        LIKE THAT THE OUTPUT :

        Before—a: 7, b:100

        After – a: 100, b:7

  • Write an application that calculates the air temperature given the number of cricket chirps per minute. A user supplies the number of chirps per minute.

    Output:

    Enter the number of chirps/minute: 99
    The temperature is 19.0C.

    plssssss..help me

  • how can i make an out put like this…that exchanges the values in two variables.

    LIKE THAT THE OUTPUT :

    Before—a: 7, b:100
    After – a: 100, b:7

    help me plsssss

  • Write a program that prompts an instructor for.
    1. the number of students in his/her class, and
    2. the number of grades assigned to each student,

    and determines the average grade for each student. Grades are whole numbers, but an average may be a decimal number.

    Sample Run:

    Number of Students: 2
    Number of grades per student: 2
    Grades for student 1
    1: 90
    2: 88
    Average: 89
    Grades for student 2
    1: 76
    2: 78
    Average: 77

    how can i do this, plssss i need your help guiz.

Sponsors

Facebook Fans