You are on page 1of 8

Lab Report 6

Task 01: Print natural numbers from 1 to n.


Title: Print natural numbers from 1 to n in C program using Loop.
Theory: In this C program we will print all natural numbers from 1 to n by using the loop.
There are various ways to print n numbers. For this program, I am concentrating on for loop to print natural numbers.
Basic Input: Below is a step-by-step descriptive logic to print natural numbers from 1 to n:
1. Ask the user to input the upper limit for the natural numbers to be printed, and store it in a variable (let's call it n).
2. Use a for loop that iterates from 1 to n with an increment of 1. The loop structure should be as follows: for (i=1; i<=n;
i++). It's worth noting that this loop structure is used because we want to print natural numbers starting from 1 up to n,
with an increment of 1.
3. Inside the loop body, print the value of i. You might wonder why we print the value of i inside the loop. The reason is
that we need to print natural numbers from 1 to n, and from the loop structure, it's clear that i will iterate from 1 to n.
Therefore, to print from 1 to n, we print the value of i.
Code:

Output:

Task 02: Print alphabets from a to z.


Title: Print alphabets from a to z in C program using Loop.
Theory: This task requires printing the alphabet from a to z using loops. There are two ways to accomplish this: using a
for loop or a while loop. In this program, I will be using the for-loop method...
Basic Input:
 For loop is used to print the alphabet from a to z. A loop variable is taken to do this of type ‘char’.
 The loop variable ‘i’ is initialized with the first alphabet ‘a’ and incremented by 1 on every iteration.
 In the loop, the character ‘i’ is printed as the alphabet.
 At every loop iteration, the value of ch is printed. The loop ends when the variable ch prints ‘z’.

Code:

Output:

Task 03: Print even numbers from 1 to n.


Title: Print even numbers from 1 to n in the C program using Loop.
Theory: Various approaches exist in the given range. Here in this program, I will explain two common beginner
methods to print even numbers.
Basic Input: To generate a series of even numbers, follow these steps:
1. Prompt the user to input the upper limit of the even numbers and store it in a variable called 'N'.
2. Set up a loop that runs from 1 to N, incrementing the loop counter by 1 in each iteration. The loop structure should
look like 'for (i = 1; i <= N; i++)'.
3. Within the loop, check if the current number 'i' is even by using the modulo operator. If 'i' is divisible by 2 (i.e., 'if (i
% 2 == 0)'), then print the value of 'i'.
Make sure to follow these steps in order to generate a series of even numbers up to the upper limit specified by the user.
Code:

Output:

Task 04: Print sum of all numbers from 1 to n.


Title: Print sum of all numbers from 1 to n in a C program using loop.
Theory: To perform a sum of all numbers from 1 to n using a loop in a C program, we need first to input the upper limit
value and store it in a variable called N. Next, we need to initialize another variable called sum to store the sum of the
numbers.
After this, we can use the for-loop formula and sum formula to calculate the value of the sum. Once the calculations are
done, we will have the required sum.
Basic Input: Here is a step-by-step guide to find the sum of n natural numbers:
1. Start by entering the upper limit to find the sum of natural numbers and store it in a variable, let's call it N.
2. Next, initialize another variable to store the sum of numbers. Let's call it sum and set it to 0.
3. To find the sum, you need to iterate through all-natural numbers from 1 to n. Begin a loop starting from 1 to N, and
increment the loop counter by 1 for each iteration. The loop structure should look like this: for (i=1; i<=N; i++).
4. Inside the loop, add the previous value of the sum with i. Which is sum = sum + i.
5. Finally, after the loop ends, print the value of the sum.
Code:

Output:

Task 05: Print sum of all odd numbers from 1 to n.


Title: Print sum of all odd numbers from 1 to n in the C program using loop.
Theory: To check for odd numbers between 1 and the user-entered number, we start by assigning the
value of variable 'count' to 1. The 'for' loop keeps iterating as long as the value of 'count' is less than or
equal to the user input number. During each iteration of the 'for' loop, we increment the value of 'count'
by 1. Inside the 'for' loop, we check for the condition 'count%2 != 0'. If it's true, we add the value present
inside the 'count' variable to the previous value present in the 'sum' variable. After the control exits the
'for' loop, we display the value present in the 'sum' variable. This value is the sum of all the odd numbers
from 1 to the user-entered number.
Basic Input : Here is a step-by-step guide to finding the sum of all odd numbers between 1 and a given upper limit, N:
1. Ask the user to input the upper limit value and store it in a variable called N.
2. Create a variable called sum and set it to 0. This variable will be used to store the sum of all odd numbers.
3. To find the sum of odd numbers, we need to iterate through all odd numbers between 1 and N. To do this, we will use
a for loop that starts at 1, increments by 1, and stops when it reaches N. The loop structure should look like this: for(i=1;
i<=N; i+=2). Note the increment of 2 instead of 1 to ensure we only consider odd numbers.
4. Inside the loop, add the current value of i to the sum variable. This can be done with the following expression: sum
+= i.
5. After the loop, print the final value of the sum.
That's it! By following these steps, you should be able to find the sum of all odd numbers between 1 and N.
Code:

Output:

Task 06: Print multiplication table of n.


Title: "Write a C program that prints the multiplication table of a given number 'n' using a loop."
Theory: A multiplication table of numbers is created by multiplying a constant integer by several repetitions ranging
from 1 to 10.
Basic Input: Here are the step-by-step instructions for printing a multiplication table:
1. Begin by asking the user to input a number that they want to use as the basis of their multiplication table. Save this
number into a variable called 'num.'
2. To print a multiplication table, we need to iterate through a loop from 1 to 10. To do this, use a 'for' loop with the
following structure: for (i = 1; i <= 10; i++)
This loop will increment the value of 'i' by 1 each time it runs.
3. Inside the loop, generate the multiplication table using the formula 'num * i.' Then, print the result in the format
specified.
By following these steps, you will be able to generate a multiplication table for any number between 1 and 10.
Code:
Output:

Task 07: Find the number of digits in a number.


Title: To find the number of digits in a number using a loop in a C program.
Theory: This program obtains an integer input from the user and calculates the number of digits. For example, if the
user enters 2319, the output of the program will be 4.
Basic Input:

• 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:

Task 08: Find the first and last digits.


Title: To find the first and last digit in a number using a loop in a C program.
Theory: There are different methods to determine the first and last digits of any number, and one way to accomplish
this is by using a loop.
Basic Input:
 In this program, we have declared three int data type variables named num, First Digit, and Last Digit.
 The user is asked to enter a positive number. This number gets stored in the num-named variable.
 We find the last digit of the entered number with the help of the (%) Modulus operator. When the entered number is
divided by 10, then it returns the remainder which is the last digit of a number.
 We find the first digit of the entered number with the help of the (/) division operator. We divide the entered number
by 10 until the number is greater than 10. When the number becomes less than 10, we get the first digit.
 At the end, the first and last digit of the entered number is printed on the screen using printf() function.
 Code:

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:

Task 10: Swap the first and last digits.


Title: To Swap the first and last digits in any number using in a loop in a C program.
Theory: This program allows the user to enter any number. And then, it is going to swap the First Digit and Last Digit
of the user-entered value.
Basic Input: This program uses three mathematical functions: pow(), log10(), and round().
• The pow () function finds the power of a number.
• The log10() function finds the log base 10 value of the passed parameter.
• The round () function is used to round a number to the nearest integer.
Code:

Output:

Task 11: Find the sum of digits of a number.


Title: To find out the sum of digits of a number by using a loop in a C program.
Theory: The C program allows the user to enter a number, then uses a while loop to calculate the sum of its digits,
resulting in a sum of 21 for the example number 231456.
Basic Input: If you want to find the sum of the digits of a given number, you can follow these steps:
1. Ask the user to input a number and store it in a variable called 'num'.
2. Find the last digit of the number by using modulo division. Simply divide the number by 10 and take the remainder.
Store this value in a variable called 'Last Digit'.
3. Add the last digit found in step 2 to a variable called 'sum'. This will initially be set to 0.
4. Remove the last digit from the number by dividing it by 10. Store the result back in the 'num' variable.
5. Repeat steps 2-4 until the 'num' variable becomes 0. At this point, the 'sum' variable will contain the sum of the digits
in the original number.
Code:

Output:

Task 12: Find the reverse of a number.


Title: To reverse a number using a loop in a C program.
Theory: The program prompts the user to enter a number and then displays the reverse pattern of the given number
using a for loop in C language. When 45678 is entered, the output will be displayed as 87654.
Basic Input: Reversing a number in real life is a simple task of flipping it, but when it comes to programming, it
requires a different approach. Let's explore the logic involved in reversing a number through a program. Here are the
steps to follow:
1. Declare two integer variables, 'Number' and 'Reversed Number'. Initialize 'Reversed Number' to 0.
2. Use the formula: Reversed Number = Reversed Number * 10 + Number % 10.
3. This formula divides the number by 10 and stores the remainder multiplied by 10 as the value of a 'Reversed
Number'.
4. After applying the formula, divide the number by 10 and update its value to eliminate the last digit that has already
been reversed.
5. Repeat steps 2-4 until the value of 'Number' becomes less than 0.
6. When the 'Number' value goes below 0, the 'Reversed Number' variable will have the result.
Now you know how to reverse a number through a program.
Code:

Output:

Task 13: Find the frequency of digits in a number.


Title: To calculate the frequency of digits in a number using a loop in a C program.
Theory: This program calculates the number of digits in a user-provided integer. For example, if the user inputs 2319,
the program will output 4.
Basic Theory: Here is a step-by-step detailed explanation of how to find the frequency of digits in an integer:
1. First, we need to create a storage where we can store the frequencies of each digit. We will use an integer array of
size 10 and call it freq[10]. We have used an array of size 10 because the decimal system has a base 10, and there are
only 10 digits that make up any decimal number.
2. Next, we need to initialize every element of the array with 0, assuming that every digit has occurred 0 times.
3. Now, we can move on to the main logic. Find the last digit of the given number by performing modular division by
10, i.e. last Digit = num % 10 (where num is the number whose frequency of digits is to be found).
4. Increment freq [last Digit] by one.
5. Remove the last digit from num as it is not needed anymore. Perform num = num / 10.
6. Repeat steps 3-5 until num! = 0. Finally, you will be left with an array freq having the frequency of each digit in that
number. Now, let's implement this in the code.
Code:
Output:

Task 14: Find the factorial of a number.


Title: To find the factorial of a number by using a loop in a C program.
Theory: In this program, we will use a loop to find the factorial of a number in a C program.
Basic Input: This program uses a for loop to perform a series of instructions. At the start of the loop, the variable 'i' is
set to 1 and will continue running until the value of 'i' is no longer less than or equal to the value of the variable 'num'.
During each iteration of the loop, the value of the variable 'f' is updated by multiplying it with the value of 'i' using the
assignment operator (*=). The loop will increment the value of 'i' by 1, and this process will continue until the condition
i<=num is no longer true.
Code:

Output:

You might also like