C Program To Print Floyd’s Triangle using For Loop

Lets write C program to print Floyd’s Triangle, using nested for loop.

Floyd’s Triangle: is a right angled Triangle formed with natural numbers.

Related Read:
For Loop In C Programming Language
Nested For Loop In C Programming Language
C Program To Print Floyd’s Triangle

Video Tutorial: C Program To Print Floyd’s Triangle using For Loop


[youtube https://www.youtube.com/watch?v=cfN_uSXJW_w]

YouTube Link: https://www.youtube.com/watch?v=cfN_uSXJW_w [Watch the Video In Full Screen.]

Logic To Print Floyd’s Triangle using For Loop

We ask the user to input the number of rows for Floyd’s Triangle, we store it inside variable num.

Outer For loop
In our C program, row number and the total numbers to be printed for that row is present inside variable row. So the outer for loop selects the row number and the number of elements to be printed in that row.

We initialize the row to 1 and iterate through the outer for loop until row is less than or equal to user entered number. For each iteration of the outer for loop we increment the value of row by 1. That way selecting the next row for each iteration.

Inner For loop
Inner for loop prints natural numbers in each selected row. Variable col is assigned to 1 for each iteration of outer for loop, so that the numbers gets printed from the first position in any selected row.

Inner for loop prints the natural number from the first position till the selected row number of times.

For Example, if user enters num = 5, the following Triangle will be printed:

1
2 3
4 5 6
7 8 9 10
11 12 13 14 15

Note that the Triangle printed is a right angled Triangle and has 5 rows of natural numbers.

Important Note:
Also note that first row has 1 number. Second row has 2 numbers. Third row has 3 numbers and so on. So row number and total numbers in that particular row are always equal in any Floyd’s Triangle.

Source Code: C Program To Print Floyd’s Triangle using For Loop

#include<stdio.h>

int main()
{
    int num, row, col, count = 1;

    printf("Enter number of rows for Floyd's Triangle\n");
    scanf("%d", &num);

    printf("\n");
    for(row = 1; row <= num; row++)
    {
        for(col = 1; col <= row; col++)
        {
            printf("%d  ", count++);
        }
        printf("\n");
    }

    return 0;
}

Output 1:
Enter number of rows for Floyd’s Triangle
5

1
2 3
4 5 6
7 8 9 10
11 12 13 14 15

Output 2:
Enter number of rows for Floyd’s Triangle
14

1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31 32 33 34 35 36
37 38 39 40 41 42 43 44 45
46 47 48 49 50 51 52 53 54 55
56 57 58 59 60 61 62 63 64 65 66
67 68 69 70 71 72 73 74 75 76 77 78
79 80 81 82 83 84 85 86 87 88 89 90 91
92 93 94 95 96 97 98 99 100 101 102 103 104 105

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

C Program To Print Floyd’s Triangle In Reverse

Lets write C program to print Floyd’s Triangle in reverse, using nested while loop.

Floyd’s Triangle: is a right angled Triangle formed with natural numbers.

Related Read:
while loop in C programming
Nested While Loop: C Program
C Program To Print Floyd’s Triangle

Logic To Print Floyd’s Triangle In Reverse

We ask the user to input the number of rows of Floyd’s Triangle, we store it inside variable num. We assign 1 to variable nn(natural number). Variable num has the number of natural numbers to be printed in particular row. The inner while loop prints the natural numbers upto num.

For Example, if user enters num = 5, the following Triangle will be printed:

1 2 3 4 5
6 7 8 9
10 11 12
13 14
15

Note that the Triangle printed is a right angled Triangle and has 5 rows of natural numbers.

Source Code: C Program To Print Floyd’s Triangle In Reverse

#include < stdio.h >

int main()
{
    int num, nn = 1, count;

    printf("Enter no of rows of Floyd's Triangle\n");
    scanf("%d", &num);
    
    printf("\n");
    while(num)
    {
        count = 1;
        while(count <= num)
        {
            printf("%d  ", nn);
            nn++;
            count++;
        }
        printf("\n");
        num--;
    }

    return 0;
}

Output 1:
Enter no of rows of Floyd’s Triangle
5

1 2 3 4 5
6 7 8 9
10 11 12
13 14
15

Output 2:
Enter no of rows of Floyd’s Triangle
14

1 2 3 4 5 6 7 8 9 10 11 12 13 14
15 16 17 18 19 20 21 22 23 24 25 26 27
28 29 30 31 32 33 34 35 36 37 38 39
40 41 42 43 44 45 46 47 48 49 50
51 52 53 54 55 56 57 58 59 60
61 62 63 64 65 66 67 68 69
70 71 72 73 74 75 76 77
78 79 80 81 82 83 84
85 86 87 88 89 90
91 92 93 94 95
96 97 98 99
100 101 102
103 104
105

Video Tutorial: C Program To Print Floyd’s Triangle In Reverse


[youtube https://www.youtube.com/watch?v=7CTZGsGm6yE]

YouTube Link: https://www.youtube.com/watch?v=7CTZGsGm6yE [Watch the Video In Full Screen.]

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

C Program To Print Floyd’s Triangle

Lets write C program to print Floyd’s Triangle, using nested while loop.

Floyd’s Triangle: is a right angled Triangle formed with natural numbers.

Related Read:
while loop in C programming
Nested While Loop: C Program

Logic To Print Floyd’s Triangle

We ask the user to input the number of rows of Floyd’s Triangle, we store it inside variable num. We assign 1 to variables count and count1. Variable count is used to print the natural numbers for Floyd’s Triangle. Variable count1 is used to keep track of outer while loop. Variable count2 is used to keep track of inner while loop.

For Example, if user enters num = 5, the following Triangle will be printed:

1
2 3
4 5 6
7 8 9 10
11 12 13 14 15

Note that the Triangle printed is a right angled Triangle and has 5 rows of natural numbers.

Important Note:
Also note that first row has 1 number. Second row has 2 numbers. Third row has 3 numbers and so on. So row number and total numbers in that particular row are always equal in any Floyd’s Triangle.

Outer While loop
In our C program, row number and the total numbers to be printed for that row is present inside variable count1.

Inner While loop
Inner while loop prints natural numbers in each row. Variable count2 is assigned to 1 for each iteration of outer while loop, so that the numbers gets printed from the first position in any selected row.

Source Code: C Program To Print Floyd’s Triangle

#include<stdio.h>

int main()
{
    int num, count = 1, count1 = 1, count2;

    printf("Enter no of rows for Floyd's Triangle\n");
    scanf("%d", &num);

    printf("\n");

    while(count1 <= num)
    {
        count2 = 1;
        while(count2 <= count1)
        {
            printf("%d  ", count);
            count++;
            count2++;
        }
        count1++;
        printf("\n");
    }

    return 0;
}

Output 1:
Enter no of rows for Floyd’s Triangle
5

1
2 3
4 5 6
7 8 9 10
11 12 13 14 15

Output 2:
Enter no of rows for Floyd’s Triangle
14

1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31 32 33 34 35 36
37 38 39 40 41 42 43 44 45
46 47 48 49 50 51 52 53 54 55
56 57 58 59 60 61 62 63 64 65 66
67 68 69 70 71 72 73 74 75 76 77 78
79 80 81 82 83 84 85 86 87 88 89 90 91
92 93 94 95 96 97 98 99 100 101 102 103 104 105

Video Tutorial: C Program To Print Floyd’s Triangle


[youtube https://www.youtube.com/watch?v=5olpgYeAUKU]

YouTube Link: https://www.youtube.com/watch?v=5olpgYeAUKU [Watch the Video In Full Screen.]

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

Create and Insert Documents: MongoDB

In this video tutorial, we’ll look at the basic differences between MongoDB and traditional Database Management Systems. Also, commands to create database, collection, documents – insertion.

Topic of discussion:
Key differences.
Keyword differences.
Schema-free collection.
Database creation.
Creation of Collection.
Insertion of Documents.
JavaScript Console/Shall.

Some Commands

Show Database Already Present

1
show dbs

Create Database

1
use company

In MongoDB, it’ll never actually create any database or collection until we start storing documents in it!

Point object ‘db’ to our new database, by switching to company database

1
2
3
4
>use company
Switched to db company 
>db
company

Naming our Collection

1
db.info

So our collection name is info

Number of documents present

1
db.info.count()

using count() method, we check the no of documents present inside info collection.

Inserting document into Collection

1
2
3
4
5
db.info.insert({
 name     : 'Apple',
 product  : 'iPhone5S',
 emp_no   : 100
});

This inserts our first record.
insert() is the method, which accepts key-value pair object as it’s parameter.

JavaScript Shall
Since, this is a JavaScript shall, we can write any valid JavaScript code and interact directly with MongoDB.

Lets see another method to insert documents into the same collection.

Using save method

1
2
3
4
5
6
7
8
9
>var data = {}
>data.name = 'Technotip IT Solutions'
>data.product = 'Video Tutorials - Educational'
>data.emp = [ 'Satish', 'Kiran' ]
>data.videos = {}
>data.videos.mongo = 'MongoDB videos'
>data.videos.php = 'PHP Video Tutorials'
 
>db.info.save(data);

We create a JSON object data.
We start storing { key: value } pair into it.
emp is an array, which contains name of 2 employees.
videos is a sub object.

Now using save method we insert this data object into info collection.

Save()
Save() is a higher lever method. It checks to see if the object we’re inserting is already present in our collection, by checking the _id value we’re inserting. _id field(which is a primary key in MongoDB document)
– If there is no matching _id field in a document, it’ll call insert method and insert the data. And if the data being entered has no _id value, insert() method will create _id value for us, and insert the data into the collection.
– If _id matches to any document in our collection, it’ll use update() instead to update the previously present data with the new data entered by the user.

Database, Collections, Documents: MongoDB


[youtube https://www.youtube.com/watch?v=yAjG_fC6fPU]

YouTube Link: https://www.youtube.com/watch?v=yAjG_fC6fPU [Watch the Video In Full Screen.]



Note:
There is no Schema for our collection. i.e., no column name and datatype.
Since documents are in BSON format, insertion and finding data is much faster.

Schema-free design is flexible and is of much use in modern day web application.

Example: Users upload photos and tag names of people in the pic. This can be stored as an array of tags in a key value pair, for only those pics which has tags. For other pics, we need not create this array itself. This is flexible. Also BSON format helps create application with high Scalability.

DBMS Basics: Getting Started Guide

Getting started with Database Management System.

In this video tutorial we’re trying our best to keep everything to the minimum, and make sure not to make it look complicated for beginners.

The tables and the normalization shown in the video are just for the purpose of demonstration.

dbms-basics

DBMS is a software system which helps in managing incoming data, organizes it and provides certain ways for the data to be modified and/or extracted by the users or other programs.

Some examples of Database Management System Software include MySQL, PostgreSQL, Microsoft Access, SQL Server, FileMaker, Oracle, RDBMS, dBASE, Clipper, and FoxPro.
Also: Some Native database systems that can be connected with PHP ?

Tables Relational databases are made up of relations, commonly known as TABLES.
Relationships exists between the tables and the data are inter-related by making use of Primary and Foreign keys.

Table Columns
It has unique names.
Each column has an associated datatype.
Columns are sometimes called as fields or attributes.

Table Rows
Each row in the table represents individual data.
Rows are also called as records or tuples.

Values
Every value must have the same data type, as specified by it’s column.

Key
To identify each row uniquely.
The identifying column in a table is called as key or primary key.

Schema
Complete set of table design for a database.
It’s like a blueprint for the database.

A Schema should show the tables along with their columns, and the primary and foreign keys. Usually primary key’s are underlines and foreign keys are italicized.

Database Management System: Basics


[youtube https://www.youtube.com/watch?v=aR44FbeeFH8]

YouTube Link: https://www.youtube.com/watch?v=aR44FbeeFH8 [Watch the Video In Full Screen.]



Anomalies
Assume that we’re running an online store.
We’ve a order table.

If a person called Satish orders Apple iPad, Mac Book and a iPhone from our site.
We store his name and address and quantity of his order.

Next, Satish moves to a different place before we process the order, now we will need to update his address at 3 places!

Doing 3 times as much work. We may miss updating Satish’s address in some place, making the data inconsistent. This is called modification anomaly.

If we design our database table in this way, we’ll need to take the address of Satish each time he orders something from our online store. This way, we need to always make sure the address(data) is consistent across all the rows. If we do not take care, we may end up with conflicting data.
Ex: One row may indicate Satish to be living in Bangalore and another row may indicated Satish to be living in New York!
This scenario is called Insertion anomaly.

Once all the orders of Satish has been processed, we delete the records. This way, we no longer have Satish’s address. So we can’t send any promotional offers etc to Satish in the future. If he want’s to order something again from our online store, he need to enter his address again. This scenario is called deletion anomaly.

We could solve these anomaly problems by making use of Primary and Foreign key’s and by developing the skill/art of normalization.

Database normalization is the process of organizing the fields and tables of a relational database to minimize redundancy and dependency. Normalization usually involves dividing large tables into smaller (and less redundant) tables and defining relationships between them. The objective is to isolate data so that additions, deletions, and modifications of a field can be made in just one table and then propagated through the rest of the database using the defined relationships.

Primary Key
The PRIMARY KEY constraint uniquely identifies each record in a database table.
Primary keys must contain unique values.
A primary key column cannot contain NULL values.
Each table should have a primary key, and each table can have only ONE primary key.

Foreign Key
A foreign key is a field in a relational table that matches a candidate key of another table. The foreign key can be used to cross-reference tables.

Relationships
We’ll cover
one-one
one-many
many-many
relationships in coming video tutorials, with some real time examples.