Q4. Write a program to print numbers from 1 to 100 without using loops.

There is nothing tough to write this program, only trick should click in your mind at time of program. I mean presence of mind. See below C program.

Method 1:

#include<stdio.h>

/* Logic to print numbers from 1 to 100 without using loops using recursion */

void print_num(int start_num, int end_num);

{

                if( start_num > end_num)

                return;

                printf(“%d\t”, start_num++);

                print_num(start_num, end_num);

}

int main()

{

                int start_num, end_num;

                scanf(“%d”,&start_num);

                scanf(“%d”,&end_num);

                print_num(start_num, end_num);

                return 0;

}

Method 2:

/* Logic to print numbers from 1 to 100 without using loops using goto */

#include<stdio.h>

int main()

{

                int start_num, end_num;

                scanf(“%d”,&start_num);

                scanf(“%d”,&end_num);

                start:

                if( start_num > end_num)

                {

                                goto end:

                }

                else

                {

                                printf(“%d\t”, start_num++);

                                goto start:

               }

                end:

                                return 0;

                return 0;

}

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.