Open In App
Related Articles

C Program to print numbers from 1 to N without using semicolon?

Improve Article
Improve
Save Article
Save
Like Article
Like

How to print numbers from 1 to N without using any semicolon in C.
 

C




#include<stdio.h>
#define N 100
 
// Add your code here to print numbers from 1
// to N without using any semicolon


What code to add in above snippet such that it doesn’t contain semicolon and prints numbers from 1 to N?
We strongly recommend you to minimize your browser and try this yourself first
Method 1 (Recursive)
 

C




// A recursive C program to print all numbers from 1
// to N without semicolon
#include<stdio.h>
#define N 10
 
int main(int num)
{
    if (num <= N && printf("%d ", num) && main(num + 1))
    {
    }    
}


Output: 

1 2 3 4 5 6 7 8 9 10 

See this for complete run. Thanks to Utkarsh Trivedi for suggesting this solution.
Method 2 (Iterative)
 

C




// An iterative C program to print all numbers from 1
// to N without semicolon
#include<stdio.h>
#define N 10
 
int main(int num, char *argv[])
{
while (num <= N && printf("%d ", num) && num++)
{
}
}


Output: 

1 2 3 4 5 6 7 8 9 10 

See this for complete run. Thanks to Rahul Huria for suggesting this solution.
How do these solutions work? 
main() function can receive arguments. The first argument is argument count whose value is 1 if no argument is passed to it. The first argument is always program name. 
 

C




#include<stdio.h>
 
int main(int num, char *argv[])
{
   printf("num = %d\n", num);
   printf("argv[0] = %s ", argv[0]);
}


Output: 

num = 1 
argv[0] = <file_name>

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
 


Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 03 Mar, 2022
Like Article
Save Article
Previous
Next
Similar Reads