top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Print numbers from 1 to n without using loop or recursion?

+2 votes
736 views

How will we print numbers from 1 to N without using loop or recursion?

posted Jul 9, 2014 by anonymous

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

2 Answers

+4 votes
#include <stdio.h>

int main()
{
  int n, c = 1;

  scanf("%d", &n);   // It is assumed that n >= 1

  print:  // label

  printf("%d\n", c);
  c++;

  if (c <= n)
    goto print;

  return 0;
}
answer Jul 9, 2014 by Venkateswarlu Swarna
0 votes

As you have tagged question as C++ also so providing c++ code (basically using the constructor of C++)

#include <stdio.h>

struct X {
    static int count;

    X() { ++count; printf("%d\n", count); }
};

int X::count = 0;

int main() {
    X arr[10];
    return 0;
}
answer Jul 9, 2014 by Salil Agrawal
Similar Questions
+4 votes

Requirements:

1.No input should be processed, and the output should be in the form of 2 3 5 7 11 13 ... etc.
2. No reserved words in the language are used at all
3.The language should at least allow structured programming, and have reserved words (otherwise point 2 would be moot).

+3 votes

A number is called as a Jumping Number if all adjacent digits in it differ by 1. The difference between ‘9’ and ‘0’ is not considered as 1.
All single digit numbers are considered as Jumping Numbers. For example 7, 8987 and 4343456 are Jumping numbers but 796 and 89098 are not.

Given a positive number x, print all Jumping Numbers smaller than or equal to x. The numbers can be printed in any order.

Example:

Input: x = 20
Output: 0 1 2 3 4 5 6 7 8 9 10 12

Input: x = 105
Output: 0 1 2 3 4 5 6 7 8 9 10 12
21 23 32 34 43 45 54 56 65
67 76 78 87 89 98 101

Note: Order of output doesn't matter,
i,e., numbers can be printed in any order

...