Lab 7
Lab 7
Aim:
Explanation:
A number is called a palindrome when it reads the same forward and backward. The program
reverses the number and compares it with the original number to check if they are the same.
Algorithm:
Code: #include<stdio.h>
int main() {
scanf("%d", &num);
temp = num;
while (num != 0) {
num /= 10;
if (temp == rev)
printf("Palindrome");
else
printf("Not a Palindrome");
return 0;
The program checks if the input number is a palindrome successfully. It uses loops and
conditionals effectively.
Learning Outcomes:
Lab 7 – Program 2
a. Full Pyramid
b. Half Pyramid
Aim:
Explanation:
Patterns in C are created using nested loops. Based on the number of rows and the required
structure, loops manage spacing and star printing.
a. Full Pyramid
Algorithm:
Code : #include<stdio.h>
int main(){
int i, j, rows;
scanf("%d", &rows);
printf(" ");
printf("*");
printf("\n");
return 0;
b. Half Pyramid
Algorithm:
Code : #include<stdio.h>
int main() {
int i, j, rows;
scanf("%d", &rows);
for(i = 1; i <= rows; i++) {
for(j = 1; j <= i; j++)
printf("*");
printf("\n");
}
return 0;
}
Algorithm:
Code : #include<stdio.h>
int main() {
int i, j, rows;
scanf("%d", &rows);
for(i = rows; i >= 1; i--) {
for(j = 1; j <= rows - i; j++)
printf(" ");
for(j = 1; j <= 2 * i - 1; j++)
printf("*");
printf("\n");
}
return 0;
}
Algorithm:
Code : #include<stdio.h>
int main() {
int i, j, rows;
scanf("%d", &rows);
for(i = rows; i >= 1; i--) {
for(j = 1; j <= i; j++)
printf("*");
printf("\n");
}
return 0;
}
All types of star patterns were displayed correctly using nested loops. Proper alignment was
achieved by using spaces.
Learning Outcomes:
Lab 7 – Program 3
Aim:
To print a hollow mirrored inverted right triangle star pattern using loops.
Explanation:
The triangle has only borders made of stars while the inside remains empty. We use
conditionals inside loops to control where to print a star or space.
Algorithm:
Code : #include<stdio.h>
int main() {
int i, j, rows;
scanf("%d", &rows);
printf(" ");
if(j == 0 || j == rows - i - 1 || i == 0)
printf("*");
else
printf(" ");
printf("\n");
return 0;}
The hollow mirrored triangle was printed correctly. The conditions inside loops helped
manage spaces and stars properly.
Learning Outcomes: