C Program To Print Numbers From 1 To 100
Hi OrAnGeWorX what I could make out of your question is that user enters N numbers and you want the perfect numbers. You also want the product of non perfect numbers.I have tried something I hope you will go through it and reply me.Ashutosh_shukla, thanks for replying. i should rephrase myself as far as the problem and coding this in C shouldn't be problematic (i guess)
The code is :
The user enters N for number of integers he'd like to check if or not they are perfect.
stage 1: how many numbers do u want to check?
user enters up to 10 and hits enter
stage 2: program now waits for user to input his n numbers that will be saved in a table.
stage 3: program goes through table[0] to table[n-1] and calculates if integer in each position is perfect or not, display a message accordingly, table[n] = integer is / is not a perfect number and in that same loop calculate the product (multiplication) of the non perfect numbers
finally to display that last number.
Thanks in advance.
Marc
i'll try convertin this to C and see if that works.. having some problems with dev-c++, compiling is fine but when i execute, program is crashing with windows send report window.
Write a C program to find Armstrong numbers between 1 to n. Logic to print Armstrong numbers in given range in C programming. Learn C programming, Data Structures tutorials, exercises, examples, programs, hacks, tips and tricks online.
Probably we all know how to write C program to print 1 to 100 using loop. We can have a quick look of the code snippet.
2 4 6 8 10 12 14 | printf('1'); printf('3'); printf('5'); . printf('100'); } |
Print 1 to 100 Using Recursion
Another option to do similar job multiple times other than loop is recursion. Yes recursion is an option to do repetitive work. here is the C program to print 1 to 100 using recursion.
2 4 6 8 10 12 14 | if(n<=100){ printnum(n+1); } intmain(){ printnum(num); } |
In this example, we have a function, printnum(), to print a number of the number is less than or equal to 100. Additionally it calls itself with the next number. The function, printnum(), is called from main with 1. The function, printnum(), will print 1 first and then will call itself by 2. This process will continue until the parameter becomes more than 100, I.e. 101. In the case the function will neither print the number nor call the function itself. The function will eventually terminates. So the function will print all numbers from 1 to 100.
This program has a time complexity O(n). It has higher space requirement also as recursion uses stack internally to remember the context which is required to comeback to the previous function call context.
Mame 0.214 roms. We can do the recursion without using any other function. We can call the main() recursively. In that case we have to use global or static variable. The code below uses global variable.
2 4 6 8 | intmain(){ printf('%d ',i++); } |
Print 1 to 100Using goto Statement
Another way to do repetitive work in C programming is to use goto statement. Here is the program using goto statement.
2 4 6 8 10 | inti=1; next: if(i<=100)gotonext; return0; |
This solution is simple. We have a variable i which is initialized as 1. We have a label next to return the execution here using goto statement. After printing the value of i we are checking whether its value is less than or equal to 100. If it is, then we are sending the execution to label next. That way we’ll print i 100 times from 1 to 100. Its time complexity is also O(n) but it does not require any extra space like the previous solution.
Using seq Command on Linux
We can have Linux specific solution. There is a command called seq that can print a range of numbers. We can set the range, starting number and end number, as argument of the command. We can use system() function to execute a Linux command from C program. Here is the program.
2 4 6 | system('seq 1 100'); } |