The Wayback Machine - https://web.archive.org/web/20241126220800/https://www.geeksforgeeks.org/cpp-program-to-print-number-without-reassigning/
Open In App

C++ Program To Print Number Without Reassigning

Last Updated : 25 Aug, 2022
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Here, we will build a C++ program to print the number pattern without Reassigning using 2 approaches i.e.

  1. Using for loop
  2. Using while loop

1. Using for loop

Input: 

n = 5

Output:

1 
2 3 
4 5 6 
7 8 9 10 
11 12 13 14 15  

The first for loop is used to iterate the number of rows and the second for loop is used to repeat the number of columns. Then print the number and increment the number to print the next number. 

C++




// C++ program to print number pattern
// without re assigning using for loop
#include <iostream>
using namespace std;
 
int main()
{
 
    int rows, columns, number = 1, n = 5;
   
    // first for loop is used to identify number of rows
    for (rows = 0; rows <= n; rows++) {
       
        // second for loop is used to identify number of
        // columns and here the values will be changed
        // according to the first for loop
        for (columns = 0; columns < rows; columns++) {
           
            // printing number pattern based on the number
            // of columns
            cout << number << " ";
           
            // incrementing number at each column to print
            // the next number
            number++;
        }
       
        // print the next line for each row
        cout << "\n";
    }
    return 0;
}


Output

1 
2 3 
4 5 6 
7 8 9 10 
11 12 13 14 15 

2. Using while loop

Input: 

n = 5

Output:

1 
2 3 
4 5 6 
7 8 9 10 
11 12 13 14 15  

The while loops check the condition until the condition is false. If the condition is true then it enters into the loop and executes the statements. 

C++




// C++ program to print number without
// reassigning patterns using while loop
#include <iostream>
using namespace std;
 
int main()
{
    int rows = 1, columns = 0, n = 5;
   
    // 1 value is assigned to the number
    // helpful to print the number pattern
    int number = 1;
   
    // while loops check the condition and repeat
    // the loop until the condition is false
    while (rows <= n) {
       
        while (columns <= rows - 1) {
           
            // printing number to get required pattern
            cout << number << " ";
           
            // incrementing columns value
            columns++;
           
            // incrementing number value to print the next
            // number
            number++;
        }
        columns = 0;
       
        // incrementing rows value
        rows++;
        cout << endl;
    }
    return 0;
}


Output

1 
2 3 
4 5 6 
7 8 9 10 
11 12 13 14 15 


Previous Article
Next Article

Similar Reads

Print Number series without using any loop
Problem - Givens Two number N and K, our task is to subtract a number K from N until number(N) is greater than zero, once the N becomes negative or zero then we start adding K until that number become the original number(N). Note : Not allow to use any loop.Examples : Input : N = 15 K = 5 Output : 15 10 5 0 1 5 10 15 Input : N = 20 K = 6 Output : 2
6 min read
Print a number 100 times without using loop, recursion and macro expansion in C?
It is possible to solve this problem using loop or a recursion method. And we have already seen the solution using #define directive (Macro expansion) but what if all three are not allowed? A simple solution is to write the number 100 times in cout statement. A better solution is to use concept of Concept of setjump and longjump in C. C/C++ Code //
1 min read
C++ Program to Print the Largest Possible Prime Number From a Given Number
Given an integer, the task is to find the largest prime number that can be made out of that. If we consider the integer as a string of digits, then the prime number can be a substring of any length. The examples given below will clarify the idea of the problem. Example: Input: 12691Output: 691 Approach:Create a string of the given numberCompute all
2 min read
Print "Even" or "Odd" without using conditional statement
Write a program that accepts a number from the user and prints "Even" if the entered number is even and prints "Odd" if the number is odd. You are not allowed to use any comparison (==, <,>,...etc) or conditional statements (if, else, switch, ternary operator,. Etc). Method 1 Below is a tricky code can be used to print "Even" or "Odd" accordi
4 min read
Print 1 to 100 in C++ Without Loops and Recursion
We can print 1 to 100 without using loops and recursion using three approaches discussed below: 1) Template Metaprogramming: Templates in C++ allow non-datatypes also as parameters. Non-datatype means a value, not a datatype. Example: C/C++ Code // CPP Program to print 1 to 100 // without loops and recursion #include <iostream> using namespac
3 min read
Print a character n times without using loop, recursion or goto in C++
Given a character c and a number n, print the character c, n times. We are not allowed to use loop, recursion, and goto. Examples : Input : n = 10, c = 'a'Output : aaaaaaaaaa Input : n = 6, character = '@'Output : @@@@@@ In C++, there is a way to initialize a string with a value. It can be used to print a character as many times as we want. While d
2 min read
How to print N times without using loops or recursion ?
How to print "Hello" N times (where N is user input) without using loop or recursion or goto. Input : N, that represent the number of times you want to print the statement. Output : Statement for N times First, we create a class. After that, we need to initialize the constructor of the class by writing the statement you want to print inside a cout/
1 min read
Print individual digits as words without using if or switch
Given a number, print words for individual digits. It is not allowed to use if or switch.Examples: Input: n = 123 Output: One Two Three Input: n = 350 Output: Three Five Zero We strongly recommend you to minimize your browser and try this yourself first. The idea is to use an array of strings to store digit to word mappings. Below are steps.Let the
5 min read
Print 2D matrix in different lines and without curly braces in C/C++?
Following is a general way of printing 2D matrix such that every row is printed in separate lines. C/C++ Code for (int i = 0; i &lt; m; i++) { for (int j = 0; j &lt; n; j++) { cout &lt;&lt; arr[i][j] &lt;&lt; &quot; &quot;; } // Newline for new row cout &lt;&lt; endl; } How to print without using any curly br
2 min read
Print "Hello World" in C/C++ without using any header file
Write a C/C++ program that prints Hello World without including any header file. Conceptually it's seems impractical to write a C/C++ program that print Hello World without using a header file of "stdio.h". Since the declaration of printf() function contains in the "stdio.h" header file. But we can easily achieve this by taking the advantage of C p
2 min read
Print Hello World without semicolon in C/C++
Every statement in C++ must end with a semicolon as per basics. However, unlike other languages, almost all statements in C++ can be treated as expressions. However there are few scenarios when we can write a running program without semicolon. If we place the statement inside an if/switch/while/macro statement with a blank pair of parentheses, we d
2 min read
How to print a semicolon(;) without using semicolon in C/C++?
Another interesting question is how can a semicolon be printed without using any semicolon in the program. Here are methods to print ";" : Using printf / putchar in if statement: C/C++ Code // CPP program to print // ; without using ; // using if statement #include &lt;stdio.h&gt; int main() { // ASCII value of semicolon = 59 if (printf(
1 min read
How will you print numbers from 1 to 100 without using a loop?
If we take a look at this problem carefully, we can see that the idea of "loop" is to track some counter value, e.g., "i = 0" till "i <= 100". So, if we aren't allowed to use loops, how can we track something in the C language?Well, one possibility is the use of 'recursion', provided we use the terminating condition carefully. Here is a solution
12 min read
C++ Program To Check And Print Neon Number in a Given Range
A neon number is a number where the sum of digits of the square of the number is equal to the number. The task is to check and print neon numbers in a range. Examples: Input: 9 Output: Neon Number Explanation: square is 9*9 = 81 and sum of the digits of the square is 9. Input: 12 Output: Not a Neon Number Explanation: square is 12*12 = 144 and sum
2 min read
Program to print half diamond Number-Star pattern
Given a number N which represents the number of rows. The task is to print a half diamond Number-Star pattern as shown in the below examples.Note: N is always an even number.Examples: Input: N = 4 Output: 2*2 1 1 2*2 Input: N = 6 Output: 3*3*3 2*2 1 1 2*2 3*3*3 On carefully observing the above pattern, it can be broken down into two different trian
6 min read
C++ Program To Print Multiplication Table of a Number
A multiplication table shows a list of multiples of a particular number, from 1 to 10. In this article, we will learn to generate and display the multiplication table of a number in C++ programming language. Recommended: Please try your approach on {IDE} first, before moving on to the solution.Algorithm The idea is to use loop to iterate the loop v
3 min read
C++ Program To Print Number Pattern
Here, we will see a C++ program to print the 3 different number patterns. There are 3 number patterns covered using for loop and while loop with their respective explanation.3 Different Number Patterns: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 1 2 2 3 3 3 4 4 4 4 5 5 5 5 5 1 12 123 1234 12345Pattern 1:Input: n = 5 Output: 1 2 3 4 5 6 7 8 9 10 11 12 13 1
6 min read
C++ Program for Range sum queries without updates
Given an array arr of integers of size n. We need to compute sum of elements from index i to index j. The queries consisting of i and j index values will be executed multiple times. Examples: Input : arr[] = {1, 2, 3, 4, 5} i = 1, j = 3 i = 2, j = 4 Output : 9 12 Input : arr[] = {1, 2, 3, 4, 5} i = 0, j = 4 i = 1, j = 2 Output : 15 5 C/C++ Code //
2 min read
C++ Program For Swapping Nodes In A Linked List Without Swapping Data
Given a linked list and two keys in it, swap nodes for two given keys. Nodes should be swapped by changing links. Swapping data of nodes may be expensive in many situations when data contains many fields. It may be assumed that all keys in the linked list are distinct. Examples: Input : 10->15->12->13->20->14, x = 12, y = 20 Output:
5 min read
Program to implement Separate Chaining in C++ STL without the use of pointers
Pre-requisite: Separate Chaining, STL in C++ This article implements the Separate Chaining in Hashing with help of STL in C++ without the use of pointers. Approach: Make an array of vectors to get a dynamic (resizable) array for every hash index rather than using a linked list to do the same. Now it becomes easier to work on the data-set without us
4 min read
C++ Program For Printing Reverse Of A Linked List Without Actually Reversing
Given a linked list, print reverse of it using a recursive function. For example, if the given linked list is 1->2->3->4, then output should be 4->3->2->1.Note that the question is only about printing the reverse. To reverse the list itself see this Difficulty Level: Rookie Algorithm: printReverse(head) 1. call print reverse for h
2 min read
C++ Program For In-Place Merge Two Linked Lists Without Changing Links Of First List
Given two sorted singly linked lists having n and m elements each, merge them using constant space. First, n smallest elements in both the lists should become part of the first list and the rest elements should be part of the second list. Sorted order should be maintained. We are not allowed to change pointers of the first linked list. Example: Inp
4 min read
C++ Program To Find Power Without Using Multiplication(*) And Division(/) Operators
Method 1 (Using Nested Loops):We can calculate power by using repeated addition. For example to calculate 5^6. 1) First 5 times add 5, we get 25. (5^2) 2) Then 5 times add 25, we get 125. (5^3) 3) Then 5 times add 125, we get 625 (5^4) 4) Then 5 times add 625, we get 3125 (5^5) 5) Then 5 times add 3125, we get 15625 (5^6)  C/C++ Code // C++ code fo
3 min read
How to Run a C++ Program Without Namespace?
Prerequisite: Namespace in C++ If we want to run a program without using a namespace, we have to the std keyword along with the space resolution operator (::) in every printing line and variable declaration, For example, std::cout<<"geeksforgeeks"; Example: C/C++ Code // C++ Program without the use of using namespace std; #include <iostrea
1 min read
C++ Program To Find Length Of The Longest Substring Without Repeating Characters
Given a string str, find the length of the longest substring without repeating characters. For “ABDEFGABEF”, the longest substring are “BDEFGA” and "DEFGAB", with length 6.For “BBBB” the longest substring is “B”, with length 1.For "GEEKSFORGEEKS", there are two longest substrings shown in the below diagrams, with length 7 The desired time complexit
6 min read
Increment a number without using ++ or +
The task is to Increment a number without using ++ and + operators.Examples: Input : 3 Output : 4 Input : 9 Output : 10 The idea is based on the fact that the negative numbers are stored using 2's complement form. 2's complement form is obtained by inverting bits and then adding one. So if we invert all bits of given number and apply negative sign,
6 min read
C++ Program to Print Matrix in Z form
Given a square matrix of order n*n, we need to print elements of the matrix in Z form Examples: Input : mat[][] = {1, 2, 3, 4, 5, 6, 7, 8, 9} Output : 1 2 3 5 7 8 9 Input : mat[][] = {5, 19, 8, 7, 4, 1, 14, 8, 2, 20, 1, 9, 1, 2, 55, 4} Output: 5 19 8 7 14 20 1 2 55 4 C/C++ Code // CPP program to print a square matrix in Z form #include <bits/std
2 min read
C program to print employee details using Structure
In this program, we will read and display details of n employees using Structure. Employee details like Name, age, phone number, salary will be printed. Method : Declare variables using Structure Read details of all employees like employee name, age, phone number and salary. Display all the details of employees Below is CPP program showing employee
2 min read
Program to print elements of a Matrix row-wise skipping alternate elements
Given a matrix of N*N size where N is always odd, the task is to print elements of a matrix row-wise skipping alternate elements. Examples: Input: mat[3][3] = {{1, 4, 3}, {2, 5, 6}, {7, 8, 9}} Output: 1 3 5 7 9 Input: mat[5][5] = {{1, 2, 3, 4, 9}, {5, 6, 7, 8, 44}, {19, 10, 11, 12, 33}, {13, 14, 15, 16, 55}, {77, 88, 99, 111, 444}} Output: 1 3 9 6
5 min read
Program to Print Mirrored Hollow Parallelogram
Given the length as rows and breadth as cols, the task is to print the mirrored hollow parallelogram.Examples: Input: rows = 5, cols = 8 Output: ******** * * * * * * ******** C/C++ Code // CPP program to print hollow mirrored // parallelogram star pattern series #include <iostream> using namespace std; // function for creating pattern void Pa
5 min read
Practice Tags :
three90RightbarBannerImg