Skip to main content

NESTED FOR LOOP WITH SIMPLE PATTERN PROGRAMS #DOTC_Mdb

NESTED FOR LOOP WITH SIMPLE PATTERN PROGRAMS. 


Hi  Everyone,

In my previous blog we have discussed about nested for loop and written some basic program in C .  Today we will write some more programs like half pyramid by using special character * , Half Pyramid of number etc by using Nested for loops for better understanding . Before writing and understanding the Programs please go through my previous blog which will help you to understand in easy way.  Below is the blog for your reference.

https://dheeraj60.blogspot.com/2020/06/loop-concept-in-c-with-examples-dotcmdb.html

 

Let’s start with first program with Hallow Right Angle Triangle  Program by using Nested for loop . In Program we use inner and outer loop which we discussed in my previous blog :

Below is the Program to get the result .

 

#include <stdio.h>

#include <stdlib.h>

int main()

{

     int i, j;

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

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

         printf("* ");

      }

      printf("\n");

   }

       return 0;

}


In above program j data type act as inner loop and print * and i act as number of rows needs to be printed with next line that’s why printf(“\n”) will work for outer loop and for inner j loop we use printf(“*”) . So in the above program in outer loop i work for  rows and printing new line and inner loop print  number of *  according to j <= i; condition . . condition 6 i have given as per requirement for rows you can give any number or you can one more variable to pass for user input.


Below is the output of above program in compiler Code block


Inverted Right Angle triangle with special character * using Nested for loop by using row input from user.

 

Below is the Program .

#include <stdio.h>

#include <stdlib.h>

 

int main()                       

{

int i, j, k;

   printf(" Please enter the number of rows: ");

   scanf("%d", &k);

   for (i = k; i >= 1; --i) {

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

         printf("* ");

      }

      printf("\n");

   }

       return 0;

}

 

Here we passing k as input parameter for rows and in condition we use decrement in  outer loop for i variable and increment in inner loop for j variable so that it will display in inverted order , and through k variable user can give any numbers and loop will work display it accordingly . inner loop and print * and i act as number of rows needs to be printed with next line that’s why printf(“\n”) will work for outer loop and for inner j loop we use printf(“*”). see the condition i >= 1  for inner loop and j <= i for outer loop you will able to understand the logic.

Output of Above Program in compiler Code block is as below :



Printing Number’s instead of special Character by using same logic .

 

#include <stdio.h>

#include <stdlib.h>

 

int main()

{

 

int i, j, k;

   printf("Please enter the number of rows: ");

   scanf("%d", &k);

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

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

         printf("%d ", j);

      }

      printf("\n");

   }

       return 0;

}

In Above Program we applied the same logic only in printing we put %d for printing numbers and taking rows input from user.Inner and outer loop works as same which we used in another Program.

Below is the output of above Program in Compiler code Block.




Hope you are able to understand printing pattern  programs in C , for more practice i am writing one more same program with reversal order in numbers. Logic we applied the same for printing special character reversal program

 

Below is the Program :

#include <stdio.h>

#include <stdlib.h>

 

int main()

{

 

int i, j, k;

   printf("Enter the number of rows: ");

   scanf("%d", &k);

   for (i = k; i >= 1; --i) {

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

         printf("%d ", j);

      }

      printf("\n");

   }

 

       return 0;

}

Logic we applied the same for printing special character reversal program only difference printing for number we use %d and increment and decrement as per requirement and number of rows passed by user. Inner and outer loop works as same which we used in another Program.

 

Output of the above Program is as below .


Hope you are able to understand the nesting for loop in C , and you can write any program related to Pattern and other programs for calculation and other things as per your requirement also see my previous blog where i have written some programs by using for loop and nested for loop for your reference. Please try to make more practice on nested for loop and try to make shape Pyramid , Heart etc . You may require one for loop to use so do that . Only thing you need to keep in mind Parent and child relations of loops which we discussed outer and inner loop which loop you want to execute in outer and you can use multiple inner in one parent that is outer loop. Once we start Array Topic nested for loop is very useful for program like Matrix , printing various numbers stored in Array  etc.

In next blog i will come up with while  do while as well as nested loop of both .

Please go through this blog very carefully as this is very important for programmer and nested loop vastly used in programming part which we will see in array, functions , pointer etc. Please Practice to write as much program as you can with your requirement.

In next blog i will come up with while  do while as well as nested loop of while which is very similar for loop , but most programmer like for loop .

For any Doubt please let me know.

Thanks.

Comments

Post a Comment

Popular posts from this blog

SQL and Classification of SQL in Oracle Database #Diksha Oracle Training Centre

  SQL and Classification of SQL in Oracle Database.   SQL is Structured Query Language , which is used for storing, manipulating and retrieving data stored in a relational database .SQL is the standard language for RDBMS. All the Relational Database Management Systems (RDMS) like Oracle, MySQL, Sybase, Informix, IBM DB2 and Microsoft SQL Server use SQL as their standard database language. Oracle is one of the more secured database as compared to other databases. Importance of   SQL : SQL and PL/SQL is a backend process where all data is stored and retrieved in GUI which created either by any programming languages like Java, C++, PHP etc. so we need to have very secure database so that there will be no impact for users. SQL allows users to access data in the relational database management systems. SQL is used to communicate with a database.SQL and PL/SQL allows users to create and drop databases tables , views , stored procedures , functions , packages , trigger etc. SQL allows

Materialized View uses and Benefits in Database #DOTC_Mdb

Materialized View uses and Benefits in Database. Hello Everyone, Today we will discuss about Materialized view as it’s play important role in database. We already discussed about Simple Views and complex views in my previous blog. Before Materialized view go through my previous blog which related to simple view. https://dheeraj60.blogspot.com/2020/05/benefits-of-creating-and-using-view-in.html As we know View is not a database object and not like table which is stored in database, but view can be created from base table a view is an SQL statement that’s stored in the database. This statement, or view, has a name.A view looks and acts a lot like a table. It has columns and rows, and can be included in SELECT queries just like a table. In other word we can say View is a virtual/logical table which is basically used for security purpose. Let’s understand   about   Materialized view : A materialized view is a view that stores the results of the view’s query. Whenever you query the ma

Top 50 Interview Questions On SQL and PL/SQL #DOTC_Mdb

                    Top 50 Interview Questions On SQL and PL/SQL. Today we will Discuss Top 50 interview questions and answers of SQL and PL/SQL which is frequently asked in interview.     Question 1: What is SQL and Classification of SQL? Answer SQL is a Structure Query Language which is vastly used in RDBMS database like Oracle, Sybase, DB2 , Microsoft SQL server etc.   Classification of SQL is as below: DDL (Data Definition Language):  Commands are  create , alter , drop , truncate etc DML (Data Manipulation Language) : Commands are  insert,update and delete . TCL (Transaction Control Language ) : Commands are  Commit , Rollback and Save point. DCL (Data Control Language) : Commands are Grant , Revoke Question 2:    What is meant by Joins? What are the types of join? Answer Joins are basically used to extract/get data from multiple tables using some common columns or conditions and also by using alias to fulfill the condition.   There are various types of Joins as li