- The computer is very foolish. It does not understand any other thing except a few words and symbols. Hence, while reading a programming book, use only the words and symbols taught, and do not assume any thing more.
- The computer does not know what it is doing. It is entirely up to you to write whatever statements you need and get the work done by the computer.
- We have chosen the names of variables as sum_till_now and next_number, but these could be anything of your choice.
- Empty lines or spaces in the program is just for the nice look and feel. It does not mean anything to the computer.
1. #include <stdio.h>
2. int main()
3. {
4. int sum_till_now = 0;
5. int next_number = 0;
6. int last_number;
7.
8. printf(¨What is the last number?¨);
9. scanf(¨%d¨, &last_number);
10.
11. while (next_number < last_number)
12. {
13. next_number = next_number + 1;
14. sum_till_now = sum_till_now + next_number;
15. }
16.
17. printf(¨The result is %d\n¨, sum_till_now);
18. }
Statement 9 reads a number from input and puts it in the variable last_number. If you execute the program, it will ask for the last_number, which you have to normally type from the key board.
Now that the scanf statement is in your bag, let's attempt a program for adding a few numbers to be input at the time of execution. Program should stop adding and display the result when 0 is input. The program is given below.
1. #include <stdio.h>
2. int main()
3. {
4. int next_number = 1;
5. int sum = -1;
6.
7. while (next_number != 0)
8. {
9. sum = sum + next_number;
10. printf(¨Enter the next number?¨);
11. scanf(¨%d¨, &next_number);
12. }
13.
14. printf(¨The result is %d\n¨, sum);
15. }
Conclusion
You now have got a feel of what programming is and how to tame it. Remember that this was just to show you the approach. You have a long and fascinating way to go. While studying programming, give enough time to understand all the constructs you come across, and never skip a single word from your programming book.Happy programming! Please post all your queries and feedbacks at http://groups.google.co.in/group/sanjaypatel?hl=en
Please give a little effort to recommend this article to all who need this.