0% found this document useful (0 votes)
47 views

Lab Session 9 PDF

The document contains 4 tasks to write programs using while loops: 1) Find the sum of a series. 2) Repeat a factorial program from the previous lab using a while loop. 3) Modify the factorial program to repeatedly calculate new numbers. 4) Write a program to check if a number is a palindrome by reversing it and comparing.

Uploaded by

Ahmed Sajid
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views

Lab Session 9 PDF

The document contains 4 tasks to write programs using while loops: 1) Find the sum of a series. 2) Repeat a factorial program from the previous lab using a while loop. 3) Modify the factorial program to repeatedly calculate new numbers. 4) Write a program to check if a number is a palindrome by reversing it and comparing.

Uploaded by

Ahmed Sajid
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Computer Programming – I (MCT-143)

Lab Session 9
Task 1:
Write a program to find the sum of the following series:

It can also be written as:

Take value of n from user.

Concept of while LOOP

Task 2:
Repeat the Task 1 of the last Lab Session 7 i.e. finding the factorial of the input number, using while
statement.

Task 3:
Modify above task such that after calculating the factorial, it asks again from user if he/she wants to
calculate the factorial again for some new integer.

Sample output is:


Enter a number: 4
4!=24

You want to calculate again (press Y): y


Enter a number:3
3!=6

You want to calculate again (press Y): n

Task 4: (Difficult!)
A number is known as to be a palindrome if it same in reverse order e.g. 1331, 4536354, 787 are
palindromes and 1330 and 1778 are not. Write a program using while loop to that will take a number
from user as input and will display if the number is palindrome or not. Define number as unsigned int
to have wide range.

Sample output is:


Enter a number: 1331
Palindrome number
Another Sample output is:
Enter a number: 1330
Not a Palindrome number

How to do it:
Input the number. Create a new number with reverse order. It can be done by writing a while loop, get unit
digit of the number and start producing a new number from remainder. For example if number is 5623,
you have to create a new number with reverse order. It can be done by dividing 5623 by 10, you will get
562 and remainder 3 which is the first digit of reverse number. Now again divide 562 by 10, you will get 56
and 2. Combine this remainder with the previous one and you will get 32. Repeat this process until the
quotient becomes 0 and you will get the reverse number 3265. Compare it with the input to decide
whether it is a palindrome or not.

You might also like