0% found this document useful (0 votes)
5 views7 pages

Lab 7

The document outlines a lab exercise focused on writing C programs to check for palindromes and print various triangle star patterns. It includes algorithms and code for a palindrome checker, full pyramid, half pyramid, inverted full pyramid, inverted half pyramid, and a hollow mirrored inverted right triangle. The discussion highlights successful implementation and learning outcomes related to loops, conditions, and pattern logic.

Uploaded by

devrishinain063
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views7 pages

Lab 7

The document outlines a lab exercise focused on writing C programs to check for palindromes and print various triangle star patterns. It includes algorithms and code for a palindrome checker, full pyramid, half pyramid, inverted full pyramid, inverted half pyramid, and a hollow mirrored inverted right triangle. The discussion highlights successful implementation and learning outcomes related to loops, conditions, and pattern logic.

Uploaded by

devrishinain063
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

LAB -7

Aim:

To write a C program to check whether a given number is a palindrome or not.

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:

1. Start the program.


2. Input a number from the user.
3. Store the original number in a temporary variable.
4. Reverse the number using a loop.
5. Compare the reversed number with the original.
6. If they are equal, print palindrome; else, print not palindrome.
7. End the program.

Code: #include<stdio.h>

int main() {

int num, rev = 0, rem, temp;

scanf("%d", &num);

temp = num;

while (num != 0) {

rem = num % 10;

rev = rev * 10 + rem;

num /= 10;

if (temp == rev)

printf("Palindrome");

else

printf("Not a Palindrome");
return 0;

Discussion on Results and Conclusion:

The program checks if the input number is a palindrome successfully. It uses loops and
conditionals effectively.

Learning Outcomes:

 Understood how to reverse a number.


 Practiced using loops and conditions.
 Gained knowledge of checking palindrome logic.

Lab 7 – Program 2

a. Full Pyramid

b. Half Pyramid

c. Inverted Full Pyramid

d. Inverted Half Pyramid

Aim:

To write C programs to print different triangle star patterns.

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:

1. Input number of rows.


2. Loop from 1 to rows.
3. Print spaces and stars for each row.
4. End the loop and the program.

Code : #include<stdio.h>

int main(){

int i, j, rows;

scanf("%d", &rows);

for(i = 1; i <= rows; i++) {

for(j = 1; j <= rows - i; j++)

printf(" ");

for(j = 1; j <= 2 * i - 1; j++)

printf("*");

printf("\n");

return 0;

b. Half Pyramid
Algorithm:

1. Input number of rows.


2. Loop through each row and print increasing stars.
3. End the loop and the program.

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;
}

c. Inverted Full Pyramid

Algorithm:

1. Input number of rows.


2. Loop from rows down to 1.
3. Print spaces and stars accordingly.
4. End the program.

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;
}

d. Inverted Half Pyramid

Algorithm:

1. Input number of rows.


2. Print stars decreasing with each row using loop.
3. End the program.

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;
}

Discussion on Results and Conclusion:

All types of star patterns were displayed correctly using nested loops. Proper alignment was
achieved by using spaces.
Learning Outcomes:

 Learned how to implement nested loops.


 Understood star and space logic for pyramid shapes.
 Improved loop control logic in C.

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:

1. Input the number of rows.


2. Loop through each row.
3. Print initial spaces.
4. Print a star at the start and end of each row, and blank spaces in between.
5. Continue until pattern is complete.

Code : #include<stdio.h>

int main() {

int i, j, rows;

scanf("%d", &rows);

for(i = 0; i < rows; i++) {

for(j = 0; j < i; j++)

printf(" ");

for(j = 0; j < rows - i; j++) {

if(j == 0 || j == rows - i - 1 || i == 0)

printf("*");

else
printf(" ");

printf("\n");

return 0;}

Discussion on Results and Conclusion:

The hollow mirrored triangle was printed correctly. The conditions inside loops helped
manage spaces and stars properly.

Learning Outcomes:

 Learned hollow pattern logic.


 Practiced complex conditions inside loops.
 Improved formatting using loops.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy