Open In App

Write a program to print 1 to 100 without using any numerical value

Write a program to print numbers from 1 to 100 without using any numerical value.

Output Format:

1 2 3 4 5 6 7 8 9 10 11 ....... 97 98 99 100

Approach:

Use ASCII characters to make count of numbers i.e. 'A' character has ASCII value equal to 65 and 'B' has ASCII value 66, so we can subtract 'A' from 'B' to get the starting value 1. Now, we can run a loop till we do not reach 'd' as d has ASCII value equal to 100.

Step-by-step algorithm:

Below is the implementation of the approach:

C++
#include <iostream>
using namespace std;
void printSequence()
{
    // loop starting with null character

    for (int i = 'B' - 'A'; i <= 'd'; i++) {
        // printing values
        cout << i << " ";
    }
}
int main()
{
    printSequence();

    return 0;
}
Java
/*package whatever //do not write package name here */

import java.io.*;

class GFG {
    public static void main(String[] args)
    {
        printSequence();
    }

    public static void printSequence()
    {
        // loop starting with null character
        for (int i = 'B' - 'A'; i <= 'd'; i++) {
            // printing values
            System.out.print(i + " ");
        }
    }
}
C#
using System;

class MainClass
{
    static void PrintSequence()
    {
        // Loop starting with null character
        for (int i = 'B' - 'A'; i <= 'd'; i++)
        {
            // Printing values
            Console.Write(i + " ");
        }
    }

    public static void Main(string[] args)
    {
        PrintSequence();

        
    }
}
Javascript
function printSequence() {
    // Array to store the values
    let sequence = [];

    // Loop starting from the difference of ASCII values of 'B' and 'A'
    for (let i = 'B'.charCodeAt(0) - 'A'.charCodeAt(0); i <= 'd'.charCodeAt(0); i++) {
        // Pushing values to the array
        sequence.push(i);
    }

    // Joining the values with a space and printing the result
    console.log(sequence.join(" "));
}

printSequence();
Python3
def print_sequence():
    # loop starting with null character
    for i in range(ord('B') - ord('A'), ord('d') + 1):
        # printing values
        print(i, end=" ")


print_sequence()

Output
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70...






Time Complexity: O(1)
Auxiliary Space: O(1)

Article Tags :