You are on page 1of 6

National University of Sciences and Technology (NUST)

SMME

LAB REPORT

COURSE : Fundamentals of Programming

SUBMITTED BY : Altaf Ahmad (250282)

SUBMITTED TO: Ahmad Subhani

SECTION : ME-10 A

DATE : December 06,2018


Program#1:
Take an integer number from the user and print the sum of its digits:
Code:

Description:
The program basically just separates the number of digits involved in the entered
number and adds them to form the sum and prints it.
Explanation:
After loading the preprocessor directives and the standard libraries, three integer
variables named num, a and sum are declared, the initial value of sum is assigned as 0. Then the
program asks the user to enter a number which is stored in num. A while loop is conditioned to
run as long as the value of num > 0. Inside this while loop, the digit a is assigned the remainder
value of num/10. When a number is divided by 10, the remainder is actually the lasts digit of
that number. Then the number is divided by 10 to eliminate the last digit. The value of a is now
added to the initial value of sum. In this the num has now one less digit than the former value
of num. This program runs as long as num is greater than 0. And this process continues as long
as the loop runs. Finally, the program prints the sum of the digits of the entered number.
Output:

Explanation:
In the output, the program at first asks the user to enter a number. The program
separates the digits of the entered number and prints their sum.
Program#2:
Take a 5 digit number from the user and check if it’s a palindrome:
Code:

Description:
The program checks the number if it is a palindrome (a number which remains the same
if it is written in reverse) or not?
Explanation:
After loading the preprocessor directives and the standard libraries, an integer variable
“num” and an array “A” having 5 integers are declared. Then the program asks the user to enter
a number whose value is stored in num. A for loop is declared in which an integer x with an
initial value of 0 is defined and is conditioned to run as long as the num > 0. The integer x is
incremented by 1 every time the loop runs. The Array elements from first to fifth are assigned
the remainder values of the entered number by eliminating the last digit in the number and
saving it in the elements of the array “A” in sequence. Then, if else statements are used to
compare the digits to check if they exist for a palindrome or not. In the first if statement, the
program checks for a five digit number if the first digit and the fifth digit of the number are
same AND second and fourth digits are same, then the number is a palindrome. Another else if
statement is used to check for a three digit number if the first and the third digits are the same,
then the number is a palindrome. Another else if statement is used to check for a 4 digit
number if the first and fourth AND second and third number are the same, then it is a
palindrome. In case of a palindrome, the program prints, the number is a palindrome. Another
else statement checks if the program does not meet these conditions, then it is not a
palindrome and so it prints that the number is not a palindrome.
Output:

Explanation:
In the output, a 4 digit number is used to check the program. A palindrome (2112) is
entered and the program recognizes it as a palindrome. In the second example, a non-
palindrome 4 digit number (2122) is entered. And the program recognizes it as a non-
palindrome number. This program is tested in the examples for 4 digit numbers but it also
works for 3 and 5 digit numbers.
Program#3:
Take an integer number from the user and print the sum of its digits:
Code:

Description:
The program takes 10 numbers from the user and arranges them in ascending order.
Explanation:
After loading the preprocessor directives and the standard libraries, three integer
variables named x, y and a are declared. An array “A” is declared with 10 integer variables. The
program then prompts the user to enter 10 numbers one by one. A for loop is used where the
initial value of x is put as 0. And is conditioned to run as long as x is less than 10. And the value
of x is incremented by 1 each time the loop runs. Inside the for loop the user entered numbers
are stored in different elements of the array in sequence. Then, a nested for loop is used in
which in the first for loop, the initial value of x is initialized as 0 and is conditioned to run as long
as x is less than 10. And the value of x is incremented by 1 each time the loop runs. Inside this
for loop another for loop is defined in which the initial value of y is put as x+1 (which is the next
number in the sequence of entered numbers) and is conditioned to run as long as y is less than
10. And is incremented by 1 each time the loop runs. Inside this for loop, an if statement is used
which compares the next and previous numbers of the sequence. If the previous number is
greater than the next one, than it is moved to the right in sequence. After doing this for every
number, the program prints the number in ascending order. For this another for loop is defined,
in which the initial value of x is initialized as 0 and is conditioned to run as long as x is less than
10. And the value of x is incremented by 1 each time the loop runs. Inside the program the
program is set to print the values of the numbers which are now sorted in ascending order one
by one.
Output:

Explanation:
In the output, the program at first asks the user to enter 10 numbers. The program then
arranges them in ascending order and shows their output in the ascending form as shown in the
example above.

You might also like