Sunday, 23 September 2018

Printing Floyd's triangle n number of times in C++

Floyd's triangle 

Floyd's triangle is a right-angled triangular array of natural numbers. This triangle was named after Robert Floyd. It is obtained by writing natural numbers serially in right-angled triangular shape. We can also observe that nth row contain n numbers.E.g the fifth row contains five numbers (11,12,13,14,15).


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


Write a program in C++ to print Floyd's triangle with n number of rows. 

Firstly we will take the number of rows the user wants to print in Floyd's triangle. We will use a nested for loop statement to print Floyd's triangle. In this program, we will use two nested for loop. The first loop is used for the execution of the number of rows and the second loop is used for the execution of the number of columns. We will declare a counter with initial value 1 and print it inside the inner for loop(i.e. the loop that executes the no of columns).


Program

#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
void main()
{
int i,j,n,count=1;
clrscr();
cout<<"Enter the number of rows=";
cin>>n;
for(i=1;i<=n;i++)
          {
for(j=1;j<=i;j++)
{
cout<<setw(2)<<count<<" ";
count++;
}
     cout<<"\n";
}
getch();
}



Output



Program to print Floyd's triangle
PRINTING OF FLOYD'S TRIANGLE











Explanation

In the program, the user wants to print 6 rows(i.e. n=6). The nested for loop are executed as follows:

i=1 to 6        j=1 to i                   Action                                                count++
1                   j=1 to 1                 
                     j=1                        1 is printed on the screen.                    2
2                   j=1 to 2                   
                     j=1                        2 is printed on the screen.                    3 
                     j=2                        3 is printed on the screen.                    4
3                   j=1 to 3                   
                     j=1                        4 is printed on the screen.                    5 
                     j=2                        5 is printed on the screen.                    6
                     j=2                        6 is printed on the screen.                    7

The process continues till i=6. Finally, we get Floyd's triangle with n number of rows.                     





Printing an inverted pyramid pattern of an asterisk(*) in C++

Printing inverted pyramid of symbols


A pattern is an array formed by repeating, serial or recurring elements. Elements like numbers, symbols or characters may form a pattern. Patterns basically are well organized and systematic. Patterns can be of various shapes and sizes. Basically, in C++ we print shapes like an upright pyramid, inverted pyramid, rectangle, flag, and right-angled triangle. The basic concept used to print pattern in C++ is the use of nested for loop statement.


Write a program in C++ to print an upright pyramid pattern of asterisk.


In this program, we will print an inverted pyramid using for loop statement. To print the inverted pyramid we will need three for loop statements. The first loop is for the execution of no of rows, the second loop is for the printing of spaces and the third loop is for the execution of no of columns. You can use the same method to any other symbols in place of an asterisk(*).

INVERTED PYRAMID

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

Nested loop

If we use a loop inside a loop then it is called a nested loop. It is implemented by considering a new loop as a statement of the previous loop.


Syntax:
for (condition 1)
{
      for(condition 2)
          { 
               for (condition 3)
              {
               .
               .
               }
          }
}



Program


#include<iostream.h>
#include<conio.h>
void main()
{
int i,j,k,n;
clrscr();
cout<<"Enter the number of rows=";
cin>>n;
for(i=1;i<=n;i++)
     {
          for(k=1;k<i;k++)
          {
          cout<<" ";
          }
          for(j=n;j>=i;j--)
          {
          cout<<"* ";
          }
     cout<<"\n";
     }
getch();
}


Output



Program to print the inverted pyramid.
PRINTING OF INVERTED PYRAMID
                               










Explanation

In this program, the user wanted to print 6 no of rows(i.e. n=6). The following cases occurred:

i=1 to 6                k=1 to (i-1)                                                         j=6 to i
1                          k=1 to 0; No space was printed.                        j=6 to 1
                                                                                                       * was printed six times.
2                          k=1 to 1; One space was printed.                    j=6 to 2
                                                                                                       * was printed five times.
3                          k=1 to 2; Two spaces were printed.                  j=6 to 3
                                                                                                       * was printed four times.

The process continued till i=6 and we obtained the inverted pyramid with 6 rows.






Printing an upright pyramid pattern of an asterisk(*) in C++

Printing upright pyramid of symbols


A pattern is an array formed by repeating, serial or recurring elements. Elements like numbers, symbols or characters may form a pattern. Patterns basically are well organized and systematic. Patterns can be of various shapes and sizes. In C++ we print shapes like the upright pyramid, inverted pyramid, rectangle, flag, and right-angled triangle. The basic concept used to print pattern in C++ is the use of nested for loop statement.


Write a program in C++ to print an upright pyramid pattern of asterisk.


In this program, we will print an upright pyramid using for loop statement. To print the upright pyramid we will need three for loop statements. The first loop is for the execution of no of rows, the second loop is for the printing of spaces and the third loop is for the execution of no of columns. You can use the same method for any other symbols in place of an asterisk(*).


UPRIGHT PYRAMID

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

Nested loop

If we use a loop inside a loop then it is called a nested loop. It is implemented by considering a new loop as a statement of the previous loop.


Syntax:
for (condition 1)
{
      for(condition 2)
          { 
               for (condition 3)
              {
               .
               .
               }
          }
}


Program


#include<iostream.h>
#include<conio.h>
void main()
{
int i,j,k,n;
clrscr();
cout<<"Enter number of rows of pyramid= ";
cin>>n;
for(i=1;i<=n;i++)        
     {
     for(k=n;k>i;k--)
           {
           cout<<" ";
           }
      for(j=1;j<=i;j++)
          {
          cout<<"* ";
          }
          cout<<"\n";
     }
getch();
}


Output


Program to print upright pyramid with n number of rows
PRINTING OF AN UPRIGHT PYRAMID










Explanation

In this program, the user wanted to print 6 no of rows(i.e. n=6). The following cases occurred:

i=1 to 6                k=6 to (i+1)                                                         j=1 to i
1                          k=6 to 2; Five spaces were printed.                  j=1 to 1
                                                                                                       * was printed once.
2                          k=6 to 3; Four spaces were printed.                 j=1 to 2
                                                                                                       * was printed twice.
3                          k=6 to 4; Three spaces were printed.               j=1 to 3
                                                                                                       * was printed thrice.

The process continued till i=6 and we obtained the upright pyramid with 6 rows.


Fact about c++

C++ programming language was also called 'The New C' when it was introduced.



Checking whether a number is a palindrome or not in C++

Palindrome number

Palindrome numbers are those numbers which remain the same even after reversing.12321 is a palindrome number as the number remains the same even after reversing i.e.12321. This program uses the same idea used in finding the reverse of a number. 


Write a program in C++ to input a multi-digit number and check whether it is a palindrome or not.


We need to input a multi-digit number from the user and copy it to another variable. Then we extract the last digit of the multi-digit number by performing the mod operation with 10. The extracted value is added to a new variable which is multiplied by 10 every time. This increases one decimal place of each digit of the number in a new variable before adding the last digit to it. The last digit is then removed by using slash(/) operator and storing it in int data type. This process is continued until the multi-digit number is reduced to zero(0). Finally, after the while loop is terminated, we compare the reverse with the value copied initially. If the two value matches the number is a palindrome else it is not.




Program


#include<iostream.h>
#include<conio.h>
void main()
{
int a,b=0,rev=0,check;
clrscr();
cout<<"Enter a multidigit number=";
cin>>a;
check=a;
while(a!=0)
{
b=a%10;
rev=(rev*10)+b;
a=a/10;
}
if(check==rev)
cout<<"\nThe number is palindrome";
else
cout<<"\nThe number is not palindrome";
getch();
}



Output



Program to check palindrome numbers.
CHECKING OF PALINDROME NUMBER








Explanation


In the program, we entered a=565 which first is assigned to variable check. Since the value of a is not equal to zero(565!=0 is true), while loop will be executed as follows:

a                 b=a%10                  rev=(rev*10)+b                      a=a/10        
565             565%10=5              (0*10)+5=5                            565/10=56
56               56%10=6                (5*10)+6=56                          56/10=5
5                 5%10=5                  (56*10)+5=565                      5/10=0
0

The while loop will terminate since a=0. Now the if...else statement will check whether the value rev(reverse of a number) is equal to the value of check(input number). If they are equal then the number is palindrome message will be displayed on the screen.





Printing reverse of a number in C++

The reverse of a number


A reverse of a number is simply obtained by writing the number in reverse order. The reverse of 95405 is 50459.
This program uses the idea of the sum of digits of a multi-digit number.

The plus(+) operator is used to perform arithmetic addition. E.g. 5+3=8.
The minus(-) operator is used to perform arithmetic subtraction. E.g. 5-3=5.


Write a program in C++ to input a multi-digit number and print its reverse.


We need to input a multi-digit number. Then we extract the last digit of the multi-digit number by performing the mod operation with 10. The extracted value is added to a new variable which is multiplied by 10 every time. This increases one decimal place of each digit of the number in a new variable before adding the last digit to it. The last digit is then removed by using slash(/) operator and storing it in int data type. This process is continued until the multi-digit number is reduced to zero(0).
   

Program


#include<iostream.h>
#include<conio.h>
void main()
{
int a,b=0,rev=0;
clrscr();
cout<<"Enter a multidigit number=";
cin>>a;
while(a!=0)
{
b=a%10;
rev=(rev*10)+b;
a=a/10;
}
cout<<"\nThe reverse of the entered number is="<<rev;
getch();
}


Output


Producing a reverse of a number
PRINTING THE REVERSE OF ENTERED NUMBER








Explanation

Now we entered a=3682 which is not equal to zero. The while loop will be executed as follows:

a                 b=a%10                  rev=(rev*10)+b                     a=a/10           
3682           3682%10=2            (0*10)+2=2                          3682/10=368
368             368%10=8              (2*10)+8=28                        368/10=36
36               36%10=6                (28*10)+6=286                    36/10=3
3                 3%10=3                  (286*10)+3=2863                3/10=0
0

The while loop will terminate since a=0 and our final answer will be stored in rev.



Saturday, 22 September 2018

Sum of the digit of a multi-digit number in C++

Sum of digits of a multi-digit number

The mod(%) operator is used to extract the remainder from a division operation.
 E.g.  27%5=2
In this example, the mod operator gives 2. This is because remainder obtained is 2 when 27 is divided by 5.

When any number is divided by 10 and its remainder is extracted then we obtain the last digit of the number.E.g. 29%10=9, here we obtain 9 the last digit of 29.

The slash(/) operator is used to perform arithmetic division.E.g. 100/20=5 which is a normal division.

When int a= 29/10 ;

Actually, the answer is 2.9 but the value is stored as an integer(int). This will neglect the decimal part leaving 2 as the answer because int can't store float(decimal) data type. We can remove the last digit of a multi-digit number by dividing it by 10 and storing it in int data type.


Write a program in C++ to input a multi-digit number and display the sum of the digits.


We will input a multi-digit number from the user. We will use the mod(%) operator to extract the last digit of the number. The last digit is added to a new variable. Then the last digit is removed by using the slash(/) operator. This operation is performed until the multi-digit number is reduced to zero. The while loop will be terminated when the multi-digit number is reduced to zero.
While loop is an iteration control structure.

The syntax of while loop:

while(condition)
{
statement 1;
statement 2;
.
.
.
}


Program


#include<iostream.h>
#include<conio.h>
void main()
{
int a,b=0,sum=0;
clrscr();
cout<<"Enter a multidigit number=";
cin>>a;
while(a!=0)
{
b=a%10;                                                        //Extraction of last digit of number
sum=sum+b;                                                  //Addition of last digit to new variable
a=a/10;                                                          //Removal of last digit of multi digit number
}
cout<<"\nThe sum of the entered number is="<<sum;
getch();
}

Output


Sum calculation of digits of a multi-digit number
DISPLAYING  SUM OF DIGITS OF MULTI DIGIT NUMBER








Explanation


In the program, the input is 4974 which is stored in a. The first statement inside while loop extracts last digit (i.e. 4). The second statement then stores the last digit (i.e. 4) in sum. After storing, the third statement discards the last digit i.e. 4 and a is updated. This process continues till the value of a is reduced to zero. Our required answer is stored in sum which is later displayed on the screen. 







Calculating a factorial of a number in C++

Factorial of a number

Factorial of a number is obtained multiplying all the positive integer equal to or less than the number whose factorial is to be calculated. It is the product of all positive integer from 1 to the number.
It is represented by the number along with an exclamation symbol. 

E.g.  Factorial of 5 is represented as 5! and is calculated as:
                                                                       5!=5*4*3*2*1=120

For any number n, its factorial is obtained as:
                                                                         n!= n * (n-1)!

The factorial of Zero is one (i.e. 0!=1). The factorial of negative and decimal number doesn't exist.



Write a program in C++ to input a number from the user and calculate its factorial.


First, we need to input a number from the user and assign it to a variable. Then we execute a for loop
from 1 to the input number. We multiply all the number that is obtained during the execution of the for loop(i.e. from 1 to input number). Finally, we get the factorial of the number.


Program


#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a,i;
long int fact=1;
cout<<"Enter the number whose factorial you want to calculate=";
cin>>a;
for(i=1;i<=a;i++)
{
fact=fact*i;
}
cout<<"\n The factorial of "<<a<<" = "<<fact;
getch();
}


Output


Calculation of factorial of a number
FACTORIAL CALCULATION OF THE GIVEN NUMBER




   



Explanation


In the program, we entered the value 7. So the for loop was executed from 1 to 7(i.e. i=1 to 7). Each time the for loop was executed the value of i was multiplied with fact and stored in fact. In this way, the product of all values of i was calculated which is our required value. Finally, the value i.e factorial of 7 was displayed on the screen.





Checking vowel and consonant in C++

Vowels and consonants

In the English alphabet, a,e,i,o,u are considered as vowels. All the other alphabets are called consonants.
Among 26 English alphabets, 5 are vowels and 21 are consonants.


Write a program in C++ to input a character from the user and check whether it is a vowel or a consonant using the switch statement.


We have to input a character from the user. We compare the entered character with the vowels. If it matches with any of the vowels, then we will print it is a vowel message. Else we will print it is a consonant message. We are using the switch statement which is conditional control statement. It is used when we have multiple cases for a single input.


The syntax of Switch statement:

switch (conditional variable)
{
case value 1:
statement;
break;
case value 2:
statement;
break;
.
.
.
default:
statement;
}

Break is a jump statement which transfers the control of execution out of the loop. It reduces the extra time which is consumed in checking remaining false condition, even after the true statement is already found.


Program  

#include<iostream.h>
#include<conio.h>
void main()
{
char ch;
clrscr();
cout<<"PROGRAM TO CHECK A VOWEL OR CONSONANT";
cout<<"\nEnter a character=";
cin>>ch;
switch(ch)
{
case 'a':
cout<<"It is a vowel";
break;
case 'e':
cout<<"It is a vowel";
break;
case 'i':
cout<<"It is a vowel";
break;
case 'o':
cout<<"It is a vowel";
break;
case 'u':
cout<<"It is a vowel";
break;
default:
cout<<"It is a consonant";
break;
}
getch();
}


Output

Checking vowel or consonant in C++
CHECKING VOWEL AND CONSONANT







Explanation


We have entered the character i which is a vowel and matches the third case of the switch case. Then the break statement brings the control of execution out of the switch loop. This reduces the program execution time. If we enter any of the character other than vowel then the default case becomes true and it is a consonant message will be displayed on the screen.



Fact about C++

The advantage in C++ over C is that variables can be declared anywhere within the program. It must be declared just before the using the variable for the first time.





Printing of Pascal's triangle in C++


Pascal's triangle 

Pascal's triangle is a mathematical triangle named after French mathematician Blaise Pascal. This triangle consists of different numbers and has infinite numbers of rows. The main arithmetic followed to create this triangle is the addition. The number of elements in a row is equal to the row on which the elements are placed on. The following triangle is the example of Pascal's triangle having five rows:


                       1
                    1    1
                 1    2    1
              1    3    3    1
           1    4    6    4    1


The first row in triangle contains only one number i.e 1. In the second row, the first element 1 is the sum of its top left and right element i.e 0 and 1. Similarly, the second element 1 of the second row is the sum of its top left and right element i.e 1 and 0. In the third row first element is 1 which is the sum of its top left and right numbers i.e 0 and 1. The second element on the third row is 2 which is the sum of its top left and right number i.e 1 and 1. A similar process is carried out to find elements in other rows of the triangle.



Write a program in C++ to print Pascal's triangle with n number of rows.


We will ask the user about the number of row s/he wants to print.We will use a matrix to create the triangle.The first and last element of each row is declared as 1.The the elements a[i][j] in between are calculated by the sum of top left a[i-1][j-1] and top right element a[i-1][j] as:

a[i][j]=a[i-1][j-1]+a[i-1][j];

Then we print the matrix.


Program

#include<iostream.h>
#include<conio.h>
void main()
{
int i,j,k,n,x,y,z;
int a[20][20];
clrscr();
cout<<"Enter a number of rows= ";
cin>>n;
for(k=1;k<=n;k++)                                     //Loop for declaration of first and last element of row as 1
{                                                               
a[k][1]=1;                                                               
a[k][k]=1;                                                                         
}                                                                                       
for(i=3;i<=n;i++)                                      /
/Loop for calculation of middle elements of row    
{
for(j=2;j<i;j++)
{
a[i][j]=a[i-1][j-1]+a[i-1][j];
}
}
for(x=1;x<=n;x++)                                   //Loop for printing of matrix       
{                                                                               
for(z=n;z>x;z--)                                //Loop for manipulation of spaces                             
{
cout<<"  ";
}
for(y=1;y<=x;y++)
{
cout<<"  "<<a[x][y];
}
cout<<"\n";
}
getch();
}


Output

Program to print Pascal's triangle
PRINTING OF PASCAL'S TRIANGLE










Explanation

In the above program, the user wanted to print Pascal's triangle with 5 number of rows. The numbers were stored in an array and addition was performed as per the method discussed above. Pascal's triangle with 5 rows was displayed on the output screen.


Fact about C++

The output screen(i.e black in color) in C++ programming is also called Console screen.





Checking whether the given number is prime, composite or zero in C++

Prime and composite numbers

Prime numbers are those numbers which are divisible by 1 and itself only. A prime number doesn't have any pattern but can be determined by the division process.
      E.g. 2,3,5,7,...etc. (i.e. 7 is divisible by 1 and itself only, so it is a prime number.)

Composite numbers are those numbers which are not prime numbers. Those numbers which are divisible by any numbers other than 1 and itself are called composite numbers.
      E.g. 4,6,9...etc.(i.e. 4 is divisible by 1,2 and 4, so it is a composite number.)

Zero and one (0,1) are neither prime nor composite numbers. 


Write a program in C++ to check whether the given number is prime, composite or none.


Firstly the number entered by the user is stored in the integer type variable. A for loop is executed from 1 to the input number. A counter is set and increases if any number within the loop divides it. Finally, the prime numbers will have two(2) as their counter value. Composite numbers will have a number greater than two(2) as their counter value. For zero the for loop won't be executed and the statement in if condition will be executed.


Program 


#include<iostream.h>
#include<conio.h>
void main()
{
int num,i,b=0;
clrscr();
cout<<"Enter a number=";
cin>>num;
for(i=1;i<=num;i++)
{
if ((num%i)==0)
b++;
} 
if ( num==0)                                         
              cout<<"\nThe number is neither prime nor composite";
else if(b==2)
cout<<"\nThe number is prime";
else if(b>2)
cout<<"\nThe number is composite";
else
              cout<<"\nInvalid input";
getch();
}



Output


Program to check prime, composite or zero numbers
CHECKING WHETHER A NUMBER IS PRIME OR NOT








Explanation


In the above program, we entered a number 7 and it was stored in num. Then the for loop was executed from 1 to 7, 7 was divided by all numbers within the loop. It was divisible by only two number (1 and 7), so the value of counter b increased twice (i.e. b=2). This condition is true for prime numbers, so 7 is a prime number. 



Fact about C++

In C++ we can use endl instead of \n. [endl=end of line]
Syntax:  cout<<"Thank"<<endl<<"You"; 
Thank will be printed and You will be printed in the next line.
Output screen
Thank
You