
- C Programming Tutorial
- C - Home
- C - Overview
- C - Environment Setup
- C - Program Structure
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Constants
- C - Storage Classes
- C - Operators
- C - Decision Making
- C - Loops
- C - Functions
- C - Scope Rules
- C - Arrays
- C - Pointers
- C - Strings
- C - Structures
- C - Unions
- C - Bit Fields
- C - Typedef
- C - Input & Output
- C - File I/O
- C - Preprocessors
- C - Header Files
- C - Type Casting
- C - Error Handling
- C - Recursion
- C - Variable Arguments
- C - Memory Management
- C - Command Line Arguments
- C Programming useful Resources
- C - Questions & Answers
- C - Quick Guide
- C - Useful Resources
- C - Discussion
C program to print name inside heart pattern using for loop.
Problem
Write a program to print heart shaped pattern with the name in centre using for loop.
Solution
The user has to enter the name that should be printed in centre along with the number of rows the stars has to be print.
Algorithm
Refer an algorithm given below to print the name in heart pattern by using for loop.
Step 1 − Declare variables.
Step 2 − Read a name at runtime that should be printed on centre.
Step 3 − Read number of rows.
Step 4 − Find the length of name.
Step 5 − Print upper part of the heart.
Step 6 − Print lower part of the heart.
Step 7 − Print the name on screen.
Example
Following is the C program to print the name in heart pattern by using for loop −
#include <stdio.h> #include <string.h> int main(){ int i, j, n; char name[50]; int len; printf("Enter your name: "); gets(name); printf("Enter no of rows: "); scanf("%d", &n); len = strlen(name); // Print upper part of the heart shape with stars for(i=n/2; i<=n; i+=2){ for(j=1; j<n-i; j+=2){ printf(" "); } for(j=1; j<=i; j++){ printf("*"); } for(j=1; j<=n-i; j++){ printf(" "); } for(j=1; j<=i; j++){ printf("*"); } printf("
"); } // Prints lower triangular part with stars for(i=n; i>=1; i--){ for(j=i; j<n; j++){ printf(" "); } // Print the name on screen if(i == n){ for(j=1; j<=(n * 2-len)/2; j++){ printf("*"); } printf("%s", name); for(j=1; j<(n*2-len)/2; j++){ printf("*"); } }else{ for(j=1; j<=(i*2)-1; j++){ printf("*"); } } printf("
"); } return 0; }
Output
When the above program is executed, it produces the following output −
Enter your name: Tutorials POint Enter no of rows: 10 ***** ***** ******* ******* ********* ********* **Tutorials POint* ***************** *************** ************* *********** ********* ******* ***** *** *
- Related Articles
- C Program for Print the pattern by using one loop
- C program to print multiplication table by using for Loop
- Print a pattern without using any loop in C++
- C program to print number series without using any loop
- C program to print four powers of numbers 1 to 9 using nested for loop
- Python Program for Print Number series without using any loop
- How to print a name multiple times without loop statement using C language?
- Program to print Interesting pattern in C++
- Program to print Kite Pattern in C++
- Program to print number pattern in C
- Program to print Diamond Pattern in C
- Program to print numeric pattern in C
- Program to print pyramid pattern in C
- C++ Program to Print Square Star Pattern
- C++ Program to Print 8 Star Pattern

Advertisements