-moz-user-select:none; -webkit-user-select:none; -khtml-user-select:none; -ms-user-select:none; user-select:none;

Thursday 30 April 2015

Making a Pyramid

Hello and Asalam o Alikum. I hope you all are fine by the grace of Allah Almighty.

Today I am here with a new blog that will help you to make a pyramid in C++.

What is a pyramid?

The above figure is actually called a pyramid.
After reading this blog, you would be able to draw a pyramid very easily inshallah.So,below is the source code of this program.

Source code



Output


How the program works?


In this program I have used 3 loops. The “s” loop is actually used for spaces. So, according to the given condition, the value of loop “i” is from 0-10.It is actually the outer loop. As the condition is true, so it will move towards the next loop, “s”.

 The value of “s” loop is from i-10.It is used for printing spaces. In the first iteration, it will print 11 spaces(and in the next iteration,it will print 10 spaces). After printing spaces, it will move towards next loop that is used to print the stars.

The value of “j” loop is from 0 - i*2. It means that it will display only 1 star in the beginning.

Now, in the next iteration, it will display 3 stars as the value of j is from 0 - 1*2(note that the value of i is 1 in this condition).


This process continues again and again and as a result, a pyramid is displayed before the user.



Tuesday 28 April 2015

Making an inverted right-angled triangle

Hello and Asalam o Alikum.I hope you all are fine by the grace of Allah Almighty.
Today I am here with a very famous program of nested loops.

You have often made an inverted right-angled triangle on your notebook or register but today I will show you how to make it in C++. Its coding is very simple and interesting

As you know that the syntax of nested loops is

Syntax

for(initialization;condition;increment/decrement )//outer loop
{
for(initialization;condition;increment/decrement)//inner loop
statement(s)
}

Below is the source code of this program.

Source code


Output


How the program works?

The outer loop of this program is from 0-5. It starts its iteration when the value is 1.
Then it jumps to the inner loop whose loop is from "i"  to 5.So, the value of loop will be from 1-5.As a result 5 stars will be printed.

Again it jumps to the outer loop where the value now becomes 2.Then it jumps to the inner loop and the same process is repeated.This time 4 stars are displayed on the next line.

This process is repeated again and again until the value of outer loop becomes 6 which is against the given condition. So ,the loop is terminated.

Thursday 23 April 2015

Nested loops in C++

Hello and Asalam o Alikum. I hope you all are fine by the grace of Allah Almighty.

Today I here with one of the most important topics of C++,"Nested loops".

Nested Loops

A loop within another loop is known as nested loop or the body of a loop can itself contain a loop which is known as nested loop.
In nested loop, the inner loop is executed only when the outer loop is executed. If the condition in the outer loop becomes false, the inner loop will not execute at all.

Syntax

for(initialization;condition;increment/decrement )//outer loop
{
for(initialization;condition;increment/decrement)//inner loop
statement(s)
}
 
The source code of a simple "nested loop" program is given below:

Source code



Output


Explanation:

                      In the above example, the iteration for outer loop is from 0-7  and the iteration for inner loop is also from 0-7.

How the program works?

The value of outer loop is 1 in the start. As it is less than 7,so the condition becomes true. It will immediately moves towards the inner loop. The iteration in inner loop us from 0-7. So, it will display "01234567".It will again moves towards the outer loop.

Next time, the value in the outer loop becomes 2. As it is less than 7, so the condition becomes false. It will again moves towards the inner loop. The iteration in inner loop us from 0-7. So, it will display "01234567".It will again moves towards the outer loop.

This process continues until the value in the outer loop becomes 8. As 8 is less than 7, so the condition becomes false. As the condition in the outer loop becomes false, it will not jump towards the inner loop and the loop will terminate. 


Wednesday 22 April 2015

Example of "goto" statement

Hello and Asalam o Alikum. I hope you all are fine by the grace of Allah Almighty.
Today I am here with a new program that explains the purpose of "goto" statement in C++.

"Goto" statement

The "goto" statement is used to unconditional transfer of control to a named label. The label must be in the same function otherwise it will not jump to the label. A label has a specified name. A label is meaningful only to a "goto" statement.

Syntax

goto label ;
.....
.....
label: statement

OR

label:statement:
........
..........
goto label ;

Below is the source code of an program that will determine whether a number entered by user is positive or not using "goto" statement.

Source code


Output


Explanation:

                      In this program, I have used a simple "if" condition and "goto" statement. 
It will take a number as input from the user and it will determine whether is is positive or not.

If the user enters a negative number, then it will jump to the label,"Positive" and then again it will ask the user to enter the number again and continues to display this message until the user enters a positive number. 


Sunday 19 April 2015

Use of "continue" statement

Hello and Asalam o Alikum. I hope you all are fine by the grace of Allah Almighty.

Today I am here with a new program that explains the purpose of "continue" statement in C++.

"Continue" statement

It is used in the body of the loop. It is used to move the control to the start of the loop body. When this statement is executed, the remaining statements of current iteration are not executed. The control directly moves to the next iteration.
Below is the source code of an example of "continue" statement.

Source code


Output


Explanation

There are two "cout" statements in the above example. One statement is before the "continue" statement and the next "cout" statement is after the "continue" statement.

Remember that the second "cout" statement,"Good bye." is never executed. The reason is that each time the "continue" statement is executed, the control moves back to the start of the loop. This process continues until the condition of the loop becomes false.So, "Hello world." will be displayed 5 times and "Good bye." will never be displayed.

Saturday 18 April 2015

Use of "break" statement

Hello and Asalam o Alikum. I hope you all are fine by the grace of Allah Almighty.

Today I am here with a program that explains the purpose of “break” statement.

"Break" statement

The “break” statement is used in the body of the loop to exit from the loop. When this statement is used in the body of the loop, the remaining iterations are skipped and the statement outside the body of the loop is executed.

Below is the source code of an example of "break" statement.

Source code


Output


Explanation

The above program contains “break” statement in the body of the loop. According to the condition given in the loop, it should display the statement,” Welcome to my blog” for 5 times but due to “break” statement, it will display this statement for only one time. The control will skip the remaining iterations and comes out of the body of the loop. It will then display, “Good bye”.

Friday 17 April 2015

"Sizeof" operator

Hello and Asalam o Alikum. I hope you all are fine by the grace of Allah Almighty.
Today I am here with a program that explains the purpose of “sizeof” operator.

 "Sizeof" operator


The “sizeof” operator is used to find the size of any date value. It determines the number of bytes occupied by the value.
Its syntax is as follows:

 sizeof (operand) ;

Remember that the operand can be a variable or a constant.
Some examples of “sizeof” operator are discussed below:
sizeof(n);
sizeof(11);
sizeof(“Pakistan”);

sizeof(3.1416);

Below is the sample program of "sizeof" operator.

Source code


Output


Wednesday 15 April 2015

Height conversion using "switch"

Hello and Asalam o Alikum. I hope you all are fine by the grace of Allah Almighty.
Today I am here with another interesting program that will convert the height entered in centimeters into inches and feet using "switch" statement.
As I have discussed in my previous blog that a "switch" statement is used to test different conditions.So, below is the explanation of this program.

Explanation

In this program, I will take the height as input from the user.Then, there would be two conditions before the user.If the user enters "1", then it will convert the height into feet by using the formula,

Feet=Height/30.48


If the user enters "2", then it will convert the height into inches by using the formula,

Inches=Height/2.54.


If the user enters any other number except "1" or "2", it will simply display a message, " Invalid choice !".

Below is the source code of this program.

Source code


Output






Tuesday 14 April 2015

Calculating the area and circumference by "switch" statement

Hello and Asalam o Alikum. I hope you all are fine and enjoying good health by the grace of Allah Almighty.Today I am here with another example of "switch" statement.

Explanation

 In this program, I will take radius as input from the user.
In the next step, there would be 2 options before the user. If the user enters "1", then it will calculate the area of the circle by the following formula,

Area=3.1416*radius*radius


If the user enters "2", then it will calculate the circumference of the circle by the following formula,

Circumference=2*3.1416*radius 


If the user enters any other number except "1" and "2", it will display a message,"Invalid operator".

So,below is the source code of this interesting program.

Source code


Output



Monday 13 April 2015

Temperature conversion using "switch" statement

Hello and Asalam o Alikum. I hope you all are fine by the grace of Allah Almighty .
Today I am here with an important program that is related to temperature conversion using "switch" statement.

Explanation


As I have discussed in my previous blogs that a switch statement is used to test different condition.So,in the below mentioned program, there would be two choices before the user. If the user enters "1",then it will ask him to enter the temperature in celsius. It then converts it into fahrenheit according to the formula

f=c*9/5+32
Suppose the user enters "37" as input.So,
f=37*9/5+32
 =98.599998


. If the user enters "2",then it will ask him to enter the temperature in fahrenheit. It then converts it into celsius according to the formula 

c=(f-32)*5/9
Suppose the user enters "98.599998" as input.So,
c=(98.599998-32)*5/9
 =37


. So,this  is the logic on which this program works.

Below is the source code of this program.

Source code


Output

Sunday 12 April 2015

Determining a leap year

Hello and Asalam o Alikum. I hope you all are fine by the grace of Allah Almighty.
I hope you all have visited my previous blogs.Now its time to revise some previous concepts.

Today I am here with an important program that will input a year from the user and determine whether it is leap or not.
 But what is a leap year?

Its simple logic is that a year which is exactly divisible by 4 is a leap year otherwise it is not a leap year.

I have used simple "if-else" condition in this program.So, below is the source code of this program:

Source code


Output

Saturday 11 April 2015

Determining a vowel using "switch" statement

Hello and Asalam o Alikum. I hope you all are fine by the grace of Allah Almighty.
Today I am here with a new program that will determine a vowel using “switch” statement.We know that vowel characters are:"a,e,i,o,u".
As I have discussed in my previous blog that a “switch” statement is used to test different conditions. So, in the below mentioned program, I will take a character from the user and it will determine whether it is vowel or not.
Suppose I enter “E” as input. As we know that “E” is a vowel, so it will compare it with different cases. If it matches with a certain case, it will display “You entered a vowel.” If it does not matches with any of the case, the statement(s) below the default case will be executed and it will display, “You entered a consonant.”

Below is the source code of this program.

Source code


Output

If you have any ambiguity in understanding the logic of this program, feel free to contact me at "G.A.Mujtaba11@gmail.com".


Friday 10 April 2015

Making a simple calculator using switch statement

Asalam o Alikum. I hope you all are fine by the grace of Allah Almighty.
Today I am here with another program that will help you to make a simple arithmetic calculator using switch statement.
Actually switch statement is a good alternative of “if-else-if” or nested if-else. It is very useful in a condition when there are many choices available and only one should be executed.Its syntax is as follows:

Syntax

switch(expression)
{
case 1:
statements;
break;
case 2:
statements;
break;
.
.
default:
statements;
}


Below is the source code of the above mentioned program:

Source code

khgamujtaba,C++,making calculator using switch statement

 Output

khgamujtaba,C++,making calculator using switch statement

Explanation:
               In this program,the result of a single expression is compared with multiple cases.If it matches with a certain case,the statement(s) below that case is executed.After execution,it comes out of the "switch" body due to "break" statement.This is actually the logic of this program.


Thursday 9 April 2015

ASCII character codes in C++

Hello and Asalam o Alikum. I hope you all are fine by the grace of Allah Almighty.
Today I am here with a new program that is about character codes in ASCII.
ASCII(American Standard Code for Information Interchange) is a coding scheme in computer programming.It is used to represent different characters.
Each character has a different code assigned to it.e.g.,
A=65
B=66
C=67
D=68 and so on........
By practicing the below code,you would be able to understand the concept of character codes in ASCII.

Source code

khgamujtaba,C++,ASCII character codes

Output

khgamujtaba,C++,ASCII character codes


Wednesday 8 April 2015

calculating the reverse of a number

Reverse of a number

Hello and Asalam o Alikum.I hope you all are fine by the grace of Allah Almighty.
Today I am here with a new program that will take a number as input from the user and then  calculate its reverse.

Below is the source code of this program.

Source code

khgamujtaba,C++,finding reverse of a number

Output

khgamujtaba,C++,finding reverse of a number

Explanation:

                 Suppose I take a number "123" as input from the user.So, according to given condition,
1. x=0*10+(123%10)
  =0+3
  =3
n=123/10=12
2. x=3*10+(12%10)
     =30+2=32
   n=12/10=1
3. x=32*10+(1%10)
      =320+1
      =321
   n=1/10
     =0
    So,it will be terminated as the value of x now becomes 0 and output will be shown as "321" which is reverse of "123".
   

Tuesday 7 April 2015

Extracting odd numbers from a given range

Hello and Asalam o Alikum.I hope you all are fine by the grace of Allah Almighty.
Today I am here with a new program that will extract odd numbers from a given range.So,below is the source code of this program.

Source code

khgamujtaba,C++,extracting odd numbers

Output

khgamujtaba,C++,extracting odd numbers


You can also used the logic "if(c%2!=0)".

If you have any difficulty in understanding the logic of this program,feel free to contact me at "G.A.Mujtaba11@gmail.com".


Monday 6 April 2015

Cubelist in C++

Hello and Asalam o Alikum.I hope you all are fine by the grace of Allah Almighty.

Today I am here with another important program,"Cubelist".

In this program,I will take a series of numbers as input from the user and then calculate their cube.
I will show the number and its cube side by side using setw manipulator.This manipulator is defined in the library file"#include<iomanip.h>.It is used to set the width of output.
Below is the source code of this program.


Output

khgamujtaba,C++,finding cubelist

Output

khgamujtaba,C++,finding cubelist





Sunday 5 April 2015

Fibonacci series in C++

Hello and Asalam o Alikum.I hope you all are fine by the grace of Allah Almighty.

Today I am here with another important program,"Fibonacci series" in C++.

Remember that a series of number in which each number is the sum of preceding two numbers is known as Fibonacci series(reference taken from www.programiz.com).
The example of Fibonacci series is:
1,1,2,3,5,8,13,21,34......
So,below is the source code of this program:

Source code

khgamujtaba,C++,finding fibonacci series

Output
khgamujtaba,C++,finding fibonacci series

Saturday 4 April 2015

Determining whether a number is prime or not

Hello and Asalam o Alikum! I hope that you all are fine by the grace of Allah Almighty and visiting my blogs regularly.
Today I am here with a very important program.In this program,we will determine whether a number entered by user is prime or not.
Remember that a number which is divisible by 1 and itself is called as a prime number.Below is the source code of this program.

Source code

khgamujtaba,C++,finding prime number in C++

Output

khgamujtaba,C++,finding prime number in C++

If you want any ambiguity in understanding the logic of this program, feel free to e-mail me at "G.A.Mujtaba11@gmail.com".


Thursday 2 April 2015

Factorial of a number by "for" loop

Hello and Asalam o Alikum.I hope you all are fine by the grace of Allah Almighty.
In my previous blogs,I created the programs of creating factorial of a number being entered by the user by using while and do-while loop.Today I will do the same process by using for loop.
Remember that factorial in Mathematics means that suppose a user enters "4",then its factorial will be calculated as
4*3=12
12*2=24
24*1=24
So,the factorial of "4" will be 24.Below is the source code of the program.

Source code

khgamujtaba,C++

Output 

khgamujtaba,C++


Wednesday 1 April 2015

Creating multiplication table by "for" loop

Hello and Asalam o Alikum.I hope you all are fine by the grace of Allah Almighty.
In my previous blogs,I created  multiplication table of number being entered by the user by using while and do-while loop.Today I will do the same process by using for loop.
Remember that if your concept of for loop is good,you can become a good programmer otherwise it would be difficult for you to survive in the world of programming.So,below is the source code of the program:

Source code


khgamujtaba,C++

Output

khgamujtaba,C++