Module 3

Looping Constructs

Home > Courses > CP Lab > Loops

Programs with line-by-line explanations

Prepared by: Dr. Tojo Mathew

Warning! You are warned against blindly copying the code statements. Read the explanations given for the code statements and understand the implementation. Then, try to do it yourself. Blindly re-typing the code statements is a waste of time & effort.

Table of contents

Note: Descriptions in brown font enclosed within  /*  and  */  or starting with // are comments for human readers  to understand the program better.  No need to type in comments in the actual program. Compilers ignore comments while generating executable code of the program. 

3.1 Factorial

Write a program to find the factorial of a given number(Using While loop).

Input Format:

Input consists of a single integer, n.

Output Format:

The output consists of a single integer which corresponds to n!

Sample Input :

4

Sample Output :

24


Solution:

Solution idea: Using a loop to multiply 1 to n. Accumulate the product in same variable that gives the factorial at the end of loop, i.e do repeatedly fact = fact*temp  where temp varies from 1 to n.  Manually work out an example with n as 3 or 4 using the loop code below. 

3.2 GCD and LCM

Write a C program to find the GCD and LCM of two integers and output the results along with the given integers. Use Euclid’s algorithm. (Using looping constructs)

Input Format:

The first and the second input are two integers.

Output Format:

Output consists of GCD and LCM of the 2 input numbers.

Refer sample input and output for formatting specifications.

[All text in bold corresponds to input and the rest corresponds to output]


Sample Input and Output:

Enter two numbers

50

25

GCD = 25 and LCM = 50 of 50 and 25


Solution:

General description of program: GCD & LCM based on Euclidean algorithm https://en.wikipedia.org/wiki/Euclidean_algorithm 

3.3 Number Palindrome

Write a C program to reverse a given integer number and check whether it is a palindrome or not. Output the given number with a suitable message. (Using While statement)


Input Format:

The first input consists of an integer.

Output Format:

Output consists of a statement in which we print whether the input is palindrome or not.

Refer sample input and output for formatting specifications.

[All text in bold corresponds to input and the rest corresponds to output]


Sample Input and Output 1:

Enter an integer :

15651

15651 is a palindrome.


Sample Input and Output 2:

Enter an integer :

100

100 is not a palindrome.


Solution:

General description of program: Given an integer as the input, check if it reads the same from both directions.

E.g.

Input: 123 - Not palindrome

Input: 343 - Is a palindrome

Solution idea: Reverse the number inputted. Compare input number & its reverse. If both are equal the input number is a palindrome. Otherwise not.

3.4 Fibonacci Numbers

Write a C program to generate and print first ‘N’ Fibonacci numbers. (Using do-While statement)

Input Format:

The first input consists of an integer.

Output Format:

Output consists of a series of n fibonacci numbers.

Refer sample input and output for formatting specifications.

[All text in bold corresponds to input and the rest corresponds to output]


Sample Input and Output 1:

Enter an integer :

7

0 1 1 2 3 5 8


Sample Input and Output 2:

Enter an integer :

5

0 1 1 2 3


Solution:


General description of program: The Fibonacci series is an infinite series, starting from '0' and '1', in which every number in the series is the sum of two numbers preceding it in the series. ( 0 1 1 2 3 5 8 13......)


Solution idea: Preceding two numbers are stored in variables prev & cur. Next value in the series is computed by adding prev & cur, and then printed. prev, cur, & next are updated in a moving manner. next is printed n times to obtain n Fibonacci numbers in the series. Initialization of variables pre & cur are given special values to generate the starting sequence values 0, 1 correctly using the loop used. Work out the problem manually for n = 4.

3.5 Prime

Write a program to find whether a given number is prime or not.

Input Format:

Input consists of a single integer.

Output Format:

Output should display whether the input is “Prime” or “Not prime”


Refer sample input and output for formatting specifications.

[All text in bold corresponds to input and the rest corresponds to output]

Sample Input and Output 1:

13

Prime

Sample Input and Output 2:

33

Not prime

Solution:

General description of program: Check if the input number is prime or not.


Solution idea:  A prime number is a number that is divisible only by 1 & that number itself. Divide the given number n continuously starting from 2 to n/2. If any of these divisions results in remainder 0, then n is not prime. Otherwise n is prime.