Program for Print number from 1 to 10 using while loop

"while" loop is common loop which is existing in most of programming language. This is an basic ask question, how to print number from 1 to N or 1 to 10 using of this loop.

Find understand, how to works of this "while" loop.

while is simplest loop that is dependent upon expression which are provide by program. Execution is condition which is allow to execute loop, When expression result are not false then loop are execute otherwise stoped they loop execution proecess. See the flowchart of while loop.

While loop flowchart

Inside the while loop we define statement. Which is execute when loop are work. Here given code implementation process of different programming languages

// Java program for
// print number from 1 to 10 using while loop
class Example
{
    public static void main(String[] args)
    {
        // Counter variables 
        // start with 1
        int start = 1;
        // last with 10
        int last = 10;
        // Executing loop until, 
        // The value of start number is less than or equal to 10.
        while (start <= last)
        {
            // Print number
            System.out.println(start);
            // Increment the number value by 1
            start++;
        }
    }
}

Output

1
2
3
4
5
6
7
8
9
10
// Include header file
#include <iostream>

using namespace std;
// C++ program for
// print number from 1 to 10 using while loop

int main()
{
    // Counter variables 
    // start with 1
    int start = 1;
    // last with 10
    int last = 10;
    // Executing loop until, 
    // The value of start number is less than or equal to 10.
    while (start <= last)
    {
        // Print number
        cout << start << endl;
        // Increment the number value by 1
        start++;
    }
    return 0;
}

Output

1
2
3
4
5
6
7
8
9
10
// Include header file
#include <stdio.h>
// C program for
// print number from 1 to 10 using while loop
int main()
{
    // Counter variables 
    // start with 1
    int start = 1;
    // last with 10
    int last = 10;
    // Executing loop until, 
    // The value of start number is less than or equal to 10.
    while (start <= last)
    {
        // Print number
        printf("%d\n", start);
        // Increment the number value by 1
        start++;
    }
    return 0;
}

Output

1
2
3
4
5
6
7
8
9
10
// Include namespace system
using System;
// Csharp program for
// print number from 1 to 10 using while loop
public class Example
{
    public static void Main(String[] args)
    {
        // Counter variables 
        // start with 1
        var start = 1;
        // last with 10
        var last = 10;
        // Executing loop until, 
        // The value of start number is less than or equal to 10.
        while (start <= last)
        {
            // Print number
            Console.WriteLine(start);
            // Increment the number value by 1
            start++;
        }
    }
}

Output

1
2
3
4
5
6
7
8
9
10
<?php
// Php program for
// print number from 1 to 10 using while loop
class Example
{
    public static
    function main($args)
    {
        // Counter variables 
        // start with 1
        $start = 1;
        // last with 10
        $last = 10;
        // Executing loop until, 
        // The value of start number is less than or equal to 10.
        while ($start <= $last)
        {
            // Print number
            printf("%d\n", $start);
            // Increment the number value by 1
            $start++;
        }
    }
}
Example::main(array());

Output

1
2
3
4
5
6
7
8
9
10
// Node JS program for
// print number from 1 to 10 using while loop

function main()
{
    // Counter variables 
    // start with 1
    var start = 1;
    // last with 10
    var last = 10;
    // Executing loop until, 
    // The value of start number is less than or equal to 10.
    while (start <= last)
    {
        // Print number
        console.log(start);
        // Increment the number value by 1
        start++;
    }
}
// Start program execution
main();

Output

1
2
3
4
5
6
7
8
9
10
#  Python 3 program for
#  print number from 1 to 10 using while loop

def main() :
    #  Counter variables 
    #  start with 1
    start = 1
    #  last with 10
    last = 10
    #  Executing loop until, 
    #  The value of start number is less than or equal to 10.
    while (start <= last) :
        #  Print number
        print(start)
        #  Increment the number value by 1
        start += 1
    

if __name__ == "__main__": main()

Output

1
2
3
4
5
6
7
8
9
10
#  Ruby program for
#  print number from 1 to 10 using while loop


def main() 
    #  Counter variables 
    #  start with 1
    start = 1
    #  last with 10
    last = 10
    #  Executing loop until, 
    #  The value of start number is less than or equal to 10.
    while (start <= last) 
        #  Print number
        print(start, "\n")
        #  Increment the number value by 1
        start += 1
    end

end

main()

Output

1
2
3
4
5
6
7
8
9
10
// Scala program for
// print number from 1 to 10 using while loop

object Main
{
    def main(args: Array[String]): Unit = {
        // Counter variables 
        // start with 1
        var start: Int = 1;
        // last with 10
        var last: Int = 10;
        // Executing loop until, 
        // The value of start number is less than or equal to 10.
        while (start <= last)
        {
            // Print number
            println(start);
            // Increment the number value by 1
            start += 1;
        }
    }
}

Output

1
2
3
4
5
6
7
8
9
10
// Swift 4 program for
// print number from 1 to 10 using while loop

func main()
{
    // Counter variables 
    // start with 1
    var start: Int = 1;
    // last with 10
    let last: Int = 10;
    // Executing loop until, 
    // The value of start number is less than or equal to 10.
    while (start <= last)
    {
        // Print number
        print(start);
        // Increment the number value by 1
        start += 1;
    }
}
main();

Output

1
2
3
4
5
6
7
8
9
10
// Kotlin program for
// print number from 1 to 10 using while loop

fun main(args: Array < String > ): Unit
{
    // Counter variables 
    // start with 1
    var start: Int = 1;
    // last with 10
    val last: Int = 10;
    // Executing loop until, 
    // The value of start number is less than or equal to 10.
    while (start <= last)
    {
        // Print number
        println(start);
        // Increment the number value by 1
        start += 1;
    }
}

Output

1
2
3
4
5
6
7
8
9
10


Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment







© 2021, kalkicode.com, All rights reserved