DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
TOPIC: Working with Loops (for, while, do-while, nested loop)
Mapping With Co’s
CO No Statement
CO2 To understand the way of execution and debug programs in C language.
CO3 To apply various constructs, loops, functions to solve mathematical and
scientific problem.
Program 1: Write a program to display natural numbers up to n using loops.
Solution:
Algorithm: Display Natural Numbers Up To n
# 1. START
# 2. INPUT: Read the integer value of n from the user.
# 3. INITIALIZE: Set a counter variable, i, to 1.
# 4. LOOP:
# 4.1. PRINT: Display the current value of i.
# 4.2. INCREMENT: Increase i by 1 (i = i + 1).
# 4.3. CHECK: If i is less than or equal to n, go back to step 4.
# 5. END
Source Code:
#include <stdio.h>
int main() {
int n, i;
printf("Enter a positive integer: ");
scanf("%d", &n);
printf("Natural numbers up to %d:\n", n);
// Using a for loop
printf("Using for loop:\n");
for (i = 1; i <= n; i++) {
printf("%d ", i);
}
printf("\n");
// Using a while loop
printf("Using while loop:\n");
i = 1;
while (i <= n) {
printf("%d ", i);
i++;
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
}
printf("\n");
// Using a do-while loop
printf("Using do-while loop:\n");
i = 1;
do {
printf("%d ", i);
i++;
} while (i <= n);
printf("\n");
return 0;
}
OUTPUT:
Program 2: Develop a program to print all even and odd numbers within a given range.
Solution:
Algorithm:
• Start
• Input: Obtain the starting and ending numbers of the range from the user.
• Loop: Iterate through each number within the given range.
• Check Even/Odd: For each number, use the modulo operator (%) to determine if it's
even or odd.
o If number % 2 == 0, the number is even.
o Otherwise, the number is odd.
• Output: Print the number along with its classification (even or odd).
• End
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
Program:
#include <stdio.h>
int main() {
int start, end, i;
printf("Enter the starting number: ");
scanf("%d", &start);
printf("Enter the ending number: ");
scanf("%d", &end);
printf("Even numbers between %d and %d are: ", start, end);
for (i = start; i <= end; i++) {
if (i % 2 == 0) {
printf("%d ", i);
}
}
printf("\n");
printf("Odd numbers between %d and %d are: ", start, end);
for (i = start; i <= end; i++) {
if (i % 2 != 0) {
printf("%d ", i);
}
}
printf("\n");
return 0;
}
OUTPUT:
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
Program 3: Create a multiplication table for a user-input number.
Algorithm:
1. Get User Input:
• Prompt the user to enter a number.
• Store the input number in a variable (e.g., num).
2. Determine Table Size:
• Decide the range of the multiplication table (e.g., up to 10, 12, or a user-
specified range).
• Store this range in a variable (e.g., tableSize).
3. Loop and Multiply:
• Use a loop (e.g., for loop) that iterates from 1 to the tableSize.
• Inside the loop:
• Multiply the num by the loop counter (e.g., i).
• Store the result in a variable (e.g., result).
• Print the equation (num * i = result).
4. Print the Table:
• The loop will print each multiplication equation sequentially, effectively
creating the table.
Solution:
#include <stdio.h>
int main() {
int number, i;
// Get input from the user
printf("Enter a number to generate its multiplication table: ");
scanf("%d", &number);
// Print the multiplication table
printf("Multiplication table for %d:\n", number);
for (i = 1; i <= 10; i++) {
printf("%d * %d = %d\n", number, i, number * i);
}
return 0;
}
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
OUTPUT:
Program 4: Write a c program to extract and display the first and last digit of a
number.
Algorithm:
1: Start
2. Input: Obtain the number from the user.
3. Extract Last Digit: Use the modulo operator (%) with 10 to get the last digit.
3. Extract First Digit:
o Repeatedly divide the number by 10 until it's less than 10.
o The remaining value is the first digit.
4. Display: Print both the first and last digits.
5. End
Solution:
#include <stdio.h>
#include <math.h>
int main() {
int number, lastDigit, firstDigit, numDigits;
// Input the number
printf("Enter a number: ");
scanf("%d", &number);
// Find the number of digits
numDigits = (int)log10(number) + 1;
// Extract the last digit
lastDigit = number % 10;
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
// Extract the first digit
firstDigit = number / (int)pow(10, numDigits - 1);
// Display the digits
printf("First digit: %d\n", firstDigit);
printf("Last digit: %d\n", lastDigit);
return 0;
}
Output:
Program 5: Write a program to check whether a given number is prime.
Algorithm:
STEP 1: Take num as input.
STEP 2: Initialize a variable temp to 0.
STEP 3: Iterate a “for” loop from 2 to num/2.
STEP 4: If num is divisible by loop iterator, then increment temp.
STEP 5: If the temp is equal to 0,
Return “Num IS PRIME”.
Else,
Return “Num IS NOT PRIME”.
Solution:
#include <stdio.h>
int main()
int i, num, temp = 0;
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
// read input from user.
printf("Enter any numb to Check for Prime: ");
scanf("%d", &num);
// iterate up to n/2.
for (i = 2; i <= num / 2; i++)
// check if num is divisible by any number.
if (num % i == 0)
temp++;
break;
// check for the value of temp and num.
if (temp == 0 && num != 1)
printf("%d is a Prime number", num);
else
printf("%d is not a Prime number", num);
return 0;
}
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
Output:
Program 6: Develop a program to verify if a number is an Armstrong number.
Algorithm:
1. Input:
An integer num.
2. Determine the number of digits:
Calculate the number of digits (n) in the input number. You can do this by converting the
number to a string and finding its length, or by repeatedly dividing the number by 10 until it
becomes 0, counting the divisions.
3. Extract digits and calculate the sum:
• Initialize a variable sum to 0.
• Iterate through each digit of the input number.
• Extract the digit using the modulo operator (% 10).
• Raise the extracted digit to the power of n.
• Add the result to the sum.
• Divide the input number by 10 to remove the extracted digit.
4. Check if the sum equals the original number:
If the sum is equal to the original input number, then it's an Armstrong number. Otherwise,
it's not.
Solution:
#include <stdio.h>
#include <math.h>
int main() {
int number, originalNumber, remainder, n = 0, result = 0;
printf("Enter an integer: ");
scanf("%d", &number);
originalNumber = number;
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
// Count the number of digits
while (originalNumber != 0) {
originalNumber /= 10;
n++;
}
originalNumber = number;
// Calculate the sum of digits raised to the power of n
while (originalNumber != 0) {
remainder = originalNumber % 10;
result += pow(remainder, n);
originalNumber /= 10;
}
// Check if the number is an Armstrong number
if (result == number) {
printf("%d is an Armstrong number.\n", number);
} else {
printf("%d is not an Armstrong number.\n", number);
}
return 0;
}
Output:
Program 7: Write a C program to print a right-angled triangle pattern using for loop
*
**
***
****
*****
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
Algorithm:
1. Outer loop:
Iterate from 1 to the desired number of rows. This loop controls the row number.
2. Inner loop:
Iterate from 1 to the current row number (from the outer loop). This loop controls the number
of stars printed in each row.
3. Print star:
Inside the inner loop, print a star * followed by a space.
4. Newline:
After the inner loop completes (all stars for the row are printed), print a newline
character \n to move to the next row.
Solution:
#include <stdio.h>
int main() {
int rows, i, j;
printf("Enter the number of rows: ");
scanf("%d", &rows);
for (i = 1; i <= rows; i++) {
for (j = 1; j <= i; j++) {
printf("* ");
}
printf("\n");
}
return 0;
}
Output:
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
Program 7: Write a C program to print a Floyd’s Triangle using for loop.
1
23
456
7 8 9 10
Algorithm:
Step 1: Start
2. Initialize variables: rows, i, j, num = 1. Get the number of rows from the user.
3. Use an outer loop for rows (from 11to rows).
4. Use an inner loop for columns (from 11 to i).
5. Print the current value of num and increment it.
6. Print a newline character after the inner loop completes.
7. End
Solution:
#include <stdio.h>
int main() {
int rows, i, j, number = 1;
printf("Enter the number of rows: ");
scanf("%d", &rows);
for (i = 1; i <= rows; i++) {
for (j = 1; j <= i; j++) {
printf("%d ", number);
number++;
}
printf("\n");
}
return 0;
}
Output:
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
Program 9: Demonstrate a C program to print an inverted right-angled triangle pattern
using for loop.
*****
****
***
**
*
Algorithm:
1. Start
2. Outer Loop: Iterate from i = number of rows down to 1,
3. Inner Loop: Iterate from j = 1 to i.
4. Print: Print "*" in the inner loop.
5. Newline: After the inner loop completes, print a newline character to move to the next
row.
6. End
Solution :
#include <stdio.h>
int main() {
int rows, i, j;
printf("Enter the number of rows: ");
scanf("%d", &rows);
for (i = rows; i >= 1; i--) {
for (j = 1; j <= i; j++) {
printf("* ");
}
printf("\n");
}
return 0;
}
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
Output: