C program to print triangle pattern having same number in a row

C program to print triangle pattern having same number in a row
  • Write a program in C to print right triangle having same number in a row.

For a right triangle of 6 rows, program’s output should be:
Triangle_Pattern_Same_Number_Row

Required Knowledge

Algorithm to print triangle pattern having same number in a row using for loop

  • Take the number of rows(N) of right triangle as input from user using scanf function.
  • Number of integers in Kth row is always K.
  • We will use two for loops to print right triangle of natural numbers.
    • Outer for loop will iterate N time. Each iteration of outer loop will print one row of the pattern.
    • For Kth row, Inner loop will iterate K times. Each iteration of inner loop will print row number on screen.

Here is the matrix representation of the above mentioned pattern. The row numbers are represented by i whereas column numbers are represented by j.
Triangle_Pattern_Same_Row

C program to print right triangle pattern having same number in a row

C program to print triangle pattern having same number in a row

#include<stdio.h>
 
int main() {
    int i, j, rows;
 
    printf("Enter the number of rows\n");
    scanf("%d", &rows);
 
    for (i = 0; i < rows; i++) {
        for (j = 0; j <= i; j++) {
            printf("%d ", i+1);
        }
        printf("\n");
    }
    return(0);
}

Output

Enter the number of rows
6
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
6 6 6 6 6 6

C program to print binary rectangular pattern where all boundary elements are 0 and all inner elements are 1

C program to print binary rectangle pattern
  • Write a C program to print binary rectangle pattern where all boundary elements are 0 and all inner elements are 1.

Binary_Number_Rectangle_Pattern

Required Knowledge

Algorithm to print rectangular pattern of binary digit 0 and 1

This program is similar to rectangle star pattern. At every position of rectangle we will check for boundary condition (first and last row and column of rectangle). If true, then we print 0 othewise 1.
Here is the matrix representation of the above mentioned binary pattern. The row numbers are represented by i whereas column numbers are represented by j.
Binary_Rectangle_Pattern

C program to print binary rectangle pattern

C program to print binary rectangle pattern

#include<stdio.h>
 
int main(){
    int rows, cols , i, j;
     
    printf("Enter rows and columns of rectangle\n");
    scanf("%d %d", &rows, &cols);
     
    /* Row iterator for loop */
    for(i = 0; i < rows; i++){
     /* Column iterator for loop */
        for(j = 0; j < cols; j++){
            /* Check if currely position is a boundary position */
            if(i==0 || i==rows-1 || j==0 || j==cols-1)
                printf("0");
            else
                printf("1");
        }
        printf("\n");
    }
    return 0;
}

Output

Enter rows and columns of rectangle
4 7
0 0 0 0 0 0 0
0 1 1 1 1 1 0
0 1 1 1 1 1 0 
0 0 0 0 0 0 0

C program to print right triangle star pattern

  • Write a program in C to print right triangle star (*) pattern of n rows using for loop.
  • Write a C program to print a right angles triangle pattern of star (*) character using loops.

For a right triangle star pattern of 7 rows. Program’s output should be:

Right_Triangle_Star_Pattern_Program

Required Knowledge

Algorithm to print right triangle star pattern using loop

  • Take the number of rows(N) of right triangle as input from user using scanf function.
  • Number of stars in Kth row is always K. 1st row contains 1 star, 2nd row contains 2 stars, 3rd row contains 3 stars. In general, Kth row contains K stars.
  • We will use two for loops to print right triangle star pattern.
  • For a right triangle star pattern of N rows, outer for loop will iterate N time. Each iteration of outer loop will print one row of the pattern.
  • For Kth row of right triangle pattern, inner loop will iterate K times. Each iteration of inner loop will print one star (*).

Here is the matrix representation of the triangle star pattern. The row numbers are represented by i whereas column numbers are represented by j.
Triangle_Star_Pattern_Matrix

C program to print right triangle star pattern

C program to print right triangle star pattern

#include<stdio.h>
 
int main() {
    int i,j,rows;
    printf("Enter the number of rows\n");
    scanf("%d", &rows);
 
    for(i = 0; i < rows; i++) {
        /* Prints one row of triangle */
        for(j = 0; j <= i; ++j) {
           printf("* ");
        }
        /* move to next row */
        printf("\n");
    }
    return 0;
}

Output

Enter the number of rows
6
*
* *
* * *
* * * *
* * * * *
* * * * * *

C program to print diamond star pattern

  • Write a program in C to print diamond star pattern using for loop.
  • How to print a diamond shape pattern of using loops in C.

Diamond star pattern program’s output should be:

Diamond_Star_Pattern

Required Knowledge

Algorithm to print diamond star pattern using loop
Diamond star pattern is a combination of pyramid star pattern and inverse pyramid star pattern. This program is combination of both, it first prints a pyramid followed by a reverse pyramid star pattern.

C program to print diamond star pattern

C program to print diamond star pattern

#include<stdio.h>
 
int main() {
    int i, space, rows=7, star=0;
     
    /* Printing upper triangle */
    for(i = 1; i <= rows; i++) {
        /* Printing spaces */
        for(space = 1; space <= rows-i; space++) {
           printf(" ");
        }
        /* Printing stars */
        while(star != (2*i - 1)) {
            printf("*");
            star++;;
        }
        star=0;
        /* move to next row */
        printf("\n");
    }
    rows--;
    /* Printing lower triangle */
    for(i = rows;i >= 1; i--) {
        /* Printing spaces */
        for(space = 0; space <= rows-i; space++) {
           printf(" ");
        }
        /* Printing stars */
        star = 0;
        while(star != (2*i - 1)) {
            printf("*");
            star++;
        }
        printf("\n");
    }
 
    return 0;
}

Output

     *
    ***
   *****
  *******
 *********
***********
 *********
  *******
   *****
    ***
     *

C program to print hollow diamond star pattern

  • Write a C program to print hollow diamond star pattern.

Hollow Diamond star pattern program’s output should be:

Hollow_Diamond_Star_Pattern

Required Knowledge

Algorithm to print hollow diamond star pattern using for loop

This program is similar to diamond star pattern. The only difference is, here we will only print first and last star character of any row and we will replace all internal star characters by space character.

C program to print hollow or empty diamond star pattern

C program to print hollow diamond star pattern

#include<stdio.h>
 
int main() {
    int i, space, rows=7, star=0;
     
    /* Printing upper triangle */
    for(i = 1; i <= rows; i++) {
        /* Printing spaces */
        for(space = 1; space <= rows-i; space++) {
           printf(" ");
        }
        /* Printing stars */
        while(star != (2*i - 1)) {
         if(star == 0 or star==2*i-2)
                printf("*");
            else
                printf(" ");
            star++;
        }
        star=0;
        /* move to next row */
        printf("\n");
    }
    rows--;
    /* Printing lower triangle */
    for(i = rows;i >= 1; i--) {
        /* Printing spaces */
        for(space = 0; space <= rows-i; space++) {
           printf(" ");
        }
        /* Printing stars */
        star = 0;
        while(star != (2*i - 1)) {
         if(star == 0 or star==2*i-2)
                printf("*");
            else
                printf(" ");
            star++;
        }
        printf("\n");
    }
 
    return 0;
}

Output

     *
    * *
   *   *
  *     *
 *       *
*         *
 *       *
  *     *
   *   *
    * *
     *

C program to print Hut star pattern

  • Write a C program to print a Hut star pattern.

Hut star pattern program’s output should be:

Hut_Star_Pattern

Required Knowledge

Algorithm to print hut star pattern

Printing a hut star pattern is a two step process.

  • Step 1: Print pyramid star pattern for top 5 rows. Check this c program to print pyramid star pattern.
  • Step 2: Print the bottom half of the hut. Each row of the bottom half of hut contains 3 stars then three space characters followed by 3 stars.

Here is the matrix representation of the Hut star pattern. The row numbers are represented by i whereas column numbers are represented by j.

Hut_Star_Pattern

C program to print Hut star pattern on screen

C program to print Hut star pattern

#include<stdio.h>
 
int main() {
    int i, j, space, rows = 8, star = 0;
 
    /* Printing upper triangle */
    for (i = 0; i < rows; i++) {
        if (i < 5) {
            /* Printing upper triangle */
            for (space = 1; space < 5 - i; space++) {
                printf(" ");
            }
            /* Printing stars */
            while (star != (2 * i + 1)) {
                printf("*");
                star++;;
            }
            star = 0;
            /* move to next row */
            printf("\n");
        } else {
            /* Printing bottom walls of huts */
            for (j = 0; j < 9; j++) {
                if ((int) (j / 3) == 1)
                    printf(" ");
                else
                    printf("*");
            }
            printf("\n");
        }
    }
    return 0;
}

Output

    *
   ***
  *****
 *******
*********
***   ***
***   ***
***   ***

C program to print heart star pattern

  • Write a C program to print a heart shape star pattern.
  • How to draw a Heart on screen using star characters in C.

Heart star pattern program’s output should be:

Heart_Star_Pattern

Required Knowledge

C program to print Heart star pattern on screen

C program to print heart star pattern

#include <stdio.h>  
   
int main() {  
    int i,j, rows;
     
    printf("Enter the number of rows\n");
    scanf("%d", &rows);  
    /* printing top semi circular shapes of heart */
    for(i = rows/2; i <= rows; i+=2){ 
     /* Printing Spaces */
        for(j = 1; j < rows-i; j+=2) {  
            printf(" ");  
        }
        /* printing stars for left semi circle */
        for(j = 1; j <= i; j++){  
            printf("*");  
        }  
        /* Printing Spaces */
        for(j = 1; j <= rows-i; j++){  
            printf(" ");  
        }  
        /* printing stars for right semi circle */
        for(j = 1; j <= i; j++){  
            printf("*");  
        }  
        /* move to next row */
        printf("\n");  
    }  
     
    /* printing inverted start pyramid */
    for(i = rows; i >= 1; i--){  
        for(j = i; j < rows; j++){  
            printf(" ");  
        }  
        for(j = 1; j <= (i*2)-1; j++){  
            printf("*");  
        }  
        /* move to next row */
        printf("\n");  
    }  
   
    return 0;  
}

Output

Enter the number of rows
7
 **   **
**** ****
*********
 *******
  *****
   ***
    *

C program to print hollow pyramid star pattern

  • Write a C program to print hollow pyramid star pattern.

Hollow pyramid star pattern program’s output should be:

Hollow_Pyramid_Pattern

Required Knowledge

Algorithm to print hollow pyramid star pattern using for loop

This program is similar to pyramid star pattern. The only difference is, from first to second last row we will only print first and last star character of any row and we will replace all internal star characters by space character. Then we will print 2*N-1 (N = number of rows in pattern) star characters in last row.

Here is the matrix representation of the hollow pyramid star pattern. The row numbers are represented by i whereas column numbers are represented by j.
Hollow_Pyramid_Star_Pattern

C program to print hollow pyramid star pattern

C program to print hollow pyramid star pattern

#include<stdio.h>
 
int main() {
    int i, space, rows, star=0;
    printf("Enter the number of rows\n");
    scanf("%d",&rows);
 
    /* printing one row in every iteration */
    for(i = 0; i < rows-1; i++) {
        /* Printing spaces */
        for(space = 1; space < rows-i; space++) {
            printf(" ");
        }
        /* Printing stars */
        for (star = 0; star <= 2*i; star++) {
            if(star==0 || star==2*i)
                printf("*");
            else
                printf(" ");
        }
        /* move to next row */
        printf("\n");
    }
    /* print last row */
    for(i=0; i<2*rows-1; i++){
        printf("*");
    }
    return 0;
}

Output

Enter the number of rows
6
     *
    * *
   *   *
  *     *
 *       *
***********

Lists in Python: How to create a list in Python

The Python lists are widely used in python. Lists are one of the most used data structures in Python. It’s an unordered data store. In this article, we will learn how to work with lists in Python. You should know Python syntax and what is the lists in python. We have talked in a previous article, about the python dictionary data structure.

The lists are a sequential data store. Item saved in the list by its index. The list index starts with 0. This mean that a simple list  x = [1, 2, 3]. To get the 1st item you will need the item by index. This might be confusing. Don’t worry we will explain. Let’s start with

Create a list in Python

To define lists in Python there are two ways. The first is to add your items between two square brackets.

Example:

items = [1, 2, 3, 4]

The 2nd method is to call the Python list built-in function by passing the items to it.

Example:

Items = list(1, 2,3,4)

In both cases, the output will be

[1, 2, 3, 4]

The list can accept any data type. You can have a list of integers and strings. List in python doesn’t enforce to have a single item type in it. You can have a list of different items.

[1, 'name', {"key" : "value"}, list(1, 2, 3)]

his gives you the flexibility to add multiple data types in the list. You can add a list inside this list. This is called a nested list. Now we store our data into a python list it’s time to know how to do more with these data.

Append items to the list in Python

The list is a mutable data structure. This means you can create a list and edit it. You can add, insert, delete items to the created list. To add items to the list you can use the function and passing the value you want to add. The append function will add the item at the end of the list. The function lets you insert data in the place you want on the list. It’ll take two parameters, the index, and the value. Let’s see an example:

items = ["mobile", "laptop", "headset"]

# append the keyboard item to the list 
items.append("keyboard")
print(items)

# output
['mobile', 'laptop', 'headset', 'keyboard']


# insert the mouse item to the list in before the laptop item
items.insert(1, "mouse")
print(items)

# output
['mobile', 'mouse', 'laptop', 'headset', 'keyboard']

Sort lists in Python

We mentioned above that the Python list is unordered. The list is stored in memory like this. You can see a detailed implementation of the Python list here.

Sort lists in Python

This means that to access the value in item inside the list you have to call it by its index. More simply if we have student’s name list `students = [“John”, “Jack”, “Christine”]` and you want to get the name of the 1st student. You will need to know the index of this student’s name. In our case, it’s the zero index. The syntax will be student[0]

Let’s see a real-world example to understands it clearly.

Students = ["John", "Jack", "Christine"]
for i in Students:
    print(Students [i])

# Output
John
Jack
Christine

The list has unordered items. To sort them you can make use of the built-in python function sorted(). It’ll go through the list items and sort them.

The usage of the sorted() function is very simple. You need to pass the list to the sorted function. It’ll return the sorted list and change the original list too.

Example:

x = [4, 5, 1, 8, 2]
print(sorted(x))

# output
[1, 2, 4, 5, 8]

The first question that will come to your mind is how it works? it can sort the integers. What about the other types of the data string, dictionaries..etc. The sort function is more dynamic in sorting. This means that you can pass the sorting mechanism you want the list to be sorted based on. The first argument we can pass it to the sort function is reverse.

Note:  The difference between sorted() and sort() The sort()  change the orginal list. The sorted() doesn’t change the orginal list. It’ll retun the new soted list.

Reverse lists in Python

The sort function can reverse the list order. Set the reverse key to True will make Python automatically reverse the list sort. Let’s see an example.

chars = ["z", "y", "o", "b", "a"]
print(sorted(chars)) 

# output
['a', 'b', 'o', 'y', 'z'] 

chars = ["z", "y", "o", "b", "a"]
print(sorted(chars, reverse=True)) 

# output
['z', 'y', 'o', 'b', 'a']

This example shows you how to reverse a list. In this example, we reverse the alphabetical order of the list.

Advanced sorting

You can add a customized sorting for the list by passing the sorting function in the key parameter.

chars = ["z", "y", "o", "b", "a"]
print(sorted(chars))

# output
['a', 'b', 'o', 'y', 'z']

words = ["aaaa", "a", "tttt", "aa"]
print(sorted(words, key=len))

# output
['a', 'aa', 'aaaa', 'tttt']


engineers = [
    {'name': 'Alan Turing', 'age': 25, 'salary': 10000},
    {'name': 'Sharon Lin', 'age': 30, 'salary': 8000},
    {'name': 'John Hopkins', 'age': 18, 'salary': 1000},
    {'name': 'Mikhail Tal', 'age': 40, 'salary': 15000},
]

# using custom function for sorting different types of data.
def get_engineer_age(engineers):
    return engineers.get('age')

engineers.sort(key=get_engineer_age)
print(engineers)

# output
[
    {'name': 'John Hopkins', 'age': 18, 'salary': 1000},
    {'name': 'Alan Turing', 'age': 25, 'salary': 10000},
    {'name': 'Sharon Lin', 'age': 30, 'salary': 8000},
    {'name': 'Mikhail Tal', 'age': 40, 'salary': 15000}
]

In the above examples, we used the key option to pass a sorting method to the sort function. The default one we used in chars array is sorting based on the order. In this list, the order was alphabetical. In the words list, we have a list of different words length. We want to sort it by the length of the word. The key we passed to the sort function is the built-in len() function. This will tell Python to sort the list based on the word length.

In the engineer’s example. This more likely to be an issue you need to solve in a more real-world example. You have a list of engineers data and you want to sort them based on the customized method. In our example, we sorted it by age.

Conclusion

Python List is a very powerful data structure. Mastering it will get you out of a lot of daily issues in Python. You can create a list with single or multiple data types. You can append the list and insert data in the index you want. The most used function in the list is the sorted method. You can sort the list based on the different criteria you want. You can know more about List form the Python official documentation.

C program to print square star pattern

  • Write a C program to print square star pattern of n rows using for loop.

For a square star pattern of side 5 stars. Program’s output should be:

Square_Star_Pattern_program

Required Knowledge

Algorithm to print square star pattern using loop

  • Take the number of stars in each side of square as input from user using scanf function. Let it be N.
  • We will use two for loops to print square star pattern.
  • In one iteration, outer for loop will print one row of pattern.
  • In one iteration, inner for loop will print one star (*) characters in a row.

Here is the matrix representation of the square star pattern. The row numbers are represented by i whereas column numbers are represented by j.
Square_Star_Pattern_Matrix

C program to print square star pattern

C program to print square star pattern

#include<stdio.h>
 
int main(){
    int side, i, j;
     
    printf("Enter side of square\n");
    scanf("%d", &side);
     
    /* Row iterator for loop */
    for(i = 0; i < side; i++){
     /* Column iterator for loop */
        for(j = 0; j < side; j++){
           printf("*");
        }
        printf("\n");
    }
    return 0;
}

Output

Enter side of square
5
*****
*****
*****
*****
*****