Computer Programming - I (MCT-143) Lab Session 7 Practice of "For" Loop
Computer Programming - I (MCT-143) Lab Session 7 Practice of "For" Loop
Computer Programming - I (MCT-143) Lab Session 7 Practice of "For" Loop
Lab Session 7
Practice of “for” Loop
How to do it:
You will take input from user and apply three conditions. If entered number is zero, display 0!=1; if entered
number is negative, display the message Factorial of negative number does not exists; and if number is
greater than zero, execute a for loop to find its factorial. You will start the loop from entered number and
keep on decrementing till one. You can do reverse as well i.e. start from one and increment till number
entered. Inside loop body, you will multiply variable to update it (which stores answer for factorial), as you
updates Sum variable task of calculating average of marks.
Task 2:
Write a program that that will take a number from user and if the number is even, it will print sum of all
number from 0 to that number and if the entered number is odd, it will print sum of all numbers from 1 to
that number. Furthermore if user enters a negative number, it should display some message e.g. negative
numbers are not allowed. (In coming lab sessions you will learn that how to display such message and
again ask for the number. At the moment you just limit it to display the message).
How to do it:
As I told you that you cannot complete the whole task in a single attempt. You should start with simple
cases and slowly move towards complexity while checking output at each step. Think of the case that user
enters an odd number and you need to show sum of all odd number from 1 to entered number. This can
be done very easily using a for loop. In for loop, variable will be initialized to 1 and final value will be the
entered number. Then you need to increment it by twice like i=i+2 inside for statement; as you need
1,3,5…..
Similarly you can do it for the even numbers. for loop variable will be initialized to 0 and incremented by
two as in case of odd numbers.
Now think how you can combine these two. If you observe carefully the only difference is the initial value
of the variable i.e. for even numbers it is 0 and for odd numbers it is 1. So you should be intelligent enough
to decide at this point that in for loop you will initialize the loop variable not by 0 or 1 but you should
initialize it by some other variable, say variable a. And before this for loop you should use if condition to
assign value to variable a i.e. if entered number is even, it should be assigned to 0 and otherwise it should
be 1.
To include the final feature that if entered number is negative, you should display some message. It can be
done by if statement just after taking the input. Use if statement in a way that if entered number is
negative, message should be displayed and otherwise it should execute the above mentioned logic.
Task 3:
Write a program that will ask user for 10 inputs; one by one using for loop and will show sum of even
numbers entered and sum of odd numbers entered.
Task 4:
Write a program that will take a positive number from user and will display whether it is a prime number
or not.
Note: Use exit(0) function to exit for loop at the iteration you find that the entered number is not a prime
number. Also use Boolean variable to decide prime or not a prime.
Task 5:
Write a program to find the sum of the following series:
Task 6:
Use nested loops to create a triangle of stars as shown below:
*****
****
***
**
*