Open In App

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

Last Updated : 11 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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:

  • Initialize an integer i with ‘B’ – ‘A’.
  • Run a loop till i <= ‘d’.
  • In each iteration, print i and increment i using i++.
  • After all the iterations, we will have all numbers from 1 to 100.

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)



Similar Reads

Write a Program to Print 2024 Calendar
Write a Program to print the 2024 Calendar. Example: 2024 Calendar: January 2024 Mo Tu We Th Fr Sa Su 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 February 2024 Mo Tu We Th Fr Sa Su 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 March 2024 Mo Tu We Th Fr Sa Su 1 2 3 4 5 6 7 8 9 1
17 min read
Write a Program to Print Holiday Calendar 2024
Write a program to print the Holiday Calendar 2024. Output Format (Holiday Calendar 2024): January 26 - Friday - Republic DayMarch 25 - Monday - HoliMarch 29 - Friday - Good FridayApril 11 - Thursday - Id-ul-FitrApril 17 - Wednesday - Ram NavmiApril 21 - Sunday - Mahavir JayantiMay 23 - Thursday - Buddha PurnimaJune 17 - Monday - Id-ul-Zuha (Bakrid
5 min read
Can we write a print statement within if parentheses?
If-Else is a decision-making statement, where the result will be either true or false. If the statement accepts boolean values – if the value is true then it will execute the block of statements below it otherwise not. If no curly braces '{' and '}' is provides after if(condition) then by default if statement will consider the immediately below sta
1 min read
Program to print ASCII Value of a character
Given a character, we need to print its ASCII value in C/C++/Java/Python. Examples : Input : a Output : 97 Input : DOutput : 68 Here are few methods in different programming languages to print ASCII value of a given character :  Python code using ord function : ord() : It converts the given string of length one, returns an integer representing the
4 min read
Write a program to convert Uppercase to LowerCase
Write a program to convert Uppercase to LowerCase Examples: Input: str= "GEEKSFORGEEKS"Output: "geeksforgeeks" Input: str= "CAT"Output: "cat" Approach: Iterate through each character in a string and add 32 in the ASCII value of each character. ASCII value of lowercase char 'a' to 'z' ranges from 97 to 122ASCII value of uppercase char 'A' to 'Z' ran
2 min read
Program to print the name of month using the month number | Menu-Driven
Your task is to design a menu-driven program that asks the user to input a month number, and the program prints the name of the month. A menu-driven program is a type of computer program that allows users to interact with it by selecting options from a menu. Instead of typing commands or code, users navigate through a series of menus that present a
7 min read
Program to Print the Trapezium Pattern
Given 'num' which indicates number of lines.The task is to print a trapezium pattern in num lines.Examples: Input : 4 Output : 1*2*3*4*17*18*19*20 5*6*7*14*15*16 8*9*12*13 10*11 Input : 2 Output : 1*2*5*6 3*4 Algorithm : step 1. To read num which indicates the number of lines. step 2.We are diving the pattern into 2 halves that is LHS part and the
6 min read
Recursive program to print triangular patterns
We have discussed iterative pattern printing in previous post. Examples: Input : 7 Output : * * * * * * * * * * * * * * * * * * * * * * * * * * * * Algorithm:- step 1:- first think for the base condition i.e. number less than 0 step 2:-do the recursive calls till number less than 0 i.e:- printPartten(n-1, k+1); step 3:-print the spaces step 4:-then
8 min read
Program to print Hut
Given a number N, the task is to print Hut of width n. [caption width="800"]Output[/caption] Algorithm: Take input for the width of the hut (n).If n is even, increment it by 1.Loop through rows from 0 to n - n/3.Loop through columns from 0 to n.Determine the character to print based on the position of the row and column: a. If the position is on th
9 min read
Program to print first 10 perfect squares
Write a program to print the first 10 perfect squares. A perfect square is an integer which is the square of some other integer, or we can say that it is a second exponent of an integer. Output format: 1 4 9 16 25 .... Approach: We know that the first 10 perfect squares will be square of 1, square of 2, square of 3... till square of 10. So, iterate
2 min read
Article Tags :