Lab Report 6
Lab Report 6
Output:
Code:
Output:
Output:
Output:
Output:
• The user inputs an integer which is then stored in a variable called 'n'.
• The 'do...while' loop is then executed until the condition 'n!=0' is evaluated as false.
• During the first iteration, the value of 'n' becomes 345 and the 'count' variable is incremented to 1.
• During the second iteration, the value of 'n' becomes 34 and the 'count' variable is incremented to 2.
• During the third iteration, the value of 'n' becomes 3 and the 'count' variable is incremented to 3.
• During the fourth iteration, the value of 'n' becomes 0 and the 'count' variable is incremented to 4.
• After this, the loop terminates as the condition 'n!=0' is now false.
Code:
Output:
Output:
Task 09: Find the sum of the first and last digits.
Title: To find the sum of the first and last digits in a number using a loop in a C program.
Theory: In this C programming tutorial, we will learn how to create a program that prompts the user to enter a 4-digit
number and calculates the sum of the first and last digits. The program will also calculate the product of the middle
digits. For instance, if the user enters 8427, the program will compute the sum of the first and last digits, 8+7 or 15.
Basic Input: Here are the step-by-step instructions to find the sum of the first and last digit of a number using a loop:
1. Ask the user to enter a number and store it in a variable called 'num'.
2. To find the last digit of the number, use the modulo operator (%) to divide 'num' by 10. This will give you the value
of the last digit, which you can store in a variable called the 'last digit'.
3. To find the first digit of the number, divide 'num' by 10 repeatedly until 'num' is less than or equal to 0. The value you
get just before 'num' becomes 0 is the first digit, which you can store in a variable called 'first digit'.
4. Finally, add 'first Digit' and 'last digit' together to get the sum of the first and last digit of the original number. You
can store this value in a variable called 'sum'.
Code:
Output:
Output:
Output:
Output:
Output: