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

Tuesday 31 March 2015

Simple example of "for" loop

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 contains a  simple example of for loop.
Remember that for loop is used in a condition when we know in advance how many times we want to print a certain statement,etc.So,below is the simple example of for loop.For example,I want to write "Welcome to my blog" for 10 times.So,in this case,"for" loop will be best.

Source code

khgamujtaba,C++

Output

khgamujtaba,C++

Monday 30 March 2015

Addition of two numbers by "do-while" loop

Hello and Asalam o Alikum.I hope you all are fine by the grace of Allah Almighty.
In this blog,I will tell you how to add two numbers being entered by the user by using do-while loop.The reason of using this loop is to perform the same operation again and again same like our calculator in which we add two of more numbers several times.
So,the source code of this program is as follows:

Source code

khgamujtaba,C++

Output


khgamujtaba,C++

When the user enters "n",the loop will be terminated.

Saturday 28 March 2015

Calculating factorial by "do-while" loop

Calculating factorial

Asalam o Alikum. Hope you are enjoying good health by the grace of Allah Almighty.
Today I am here with a new blog that will calculate factorial by using do-while loop.
Remember that statements in do-while loop are executed at lease once.So,below is the source code and it's output.Hope you will like it.


Source code

khgamujtaba,C++

Output

khgamujtaba,C++



Creating multiplication table by "do-while"

Multiplication table
Asalam o Alikum.I hope you are enjoying good health by the grace of Allah Almighty.
In this blog,I will tell you a simple procedure of making  multiplication table of a number being entered by the user.So,below is the source code of this simple program.

Source code
khgamujtaba,C++

Output

khgamujtaba,C++


Thursday 26 March 2015

Example of do-while loop

In this blog,I will concentrate on the do-while loop.
The difference between "While" and "do-while" loops are as below:

1. In while loop,the condition comes before the body of the loop while in do-while loop,it comes below the body of the loop.
2. In while loop,semi-colon comes after the condition while in do-while loop,semi-colon does not comes after the condition.
3.In while loop,the loop body is not executed at least once while in do-while loop,the loop body is executed at least once.

Now below is a simple example of do-while loop.


Source code


khgamujtaba,C++


Output

khgamujtaba,C++



Wednesday 25 March 2015

Calculating factorial by While loop

Hello and Asalam o Alikum.
Today I am here with a blog that will calculate factorial of a number entered by the user.

Source Code

khgamujtaba,C++

Output

khgamujtaba,C++


Tuesday 24 March 2015

Multiplication table by While loop

By reading this blog,you would be able to learn how to make the multiplication table of a number  entered by the user.
This is actually another simple example of While loop.

Source code


khgamujtaba,C++


Output

khgamujtaba,C++

I hope it would be beneficial to you.If you have any difficulties about this program,message me on my facebook page.

Monday 23 March 2015

Example of While loop

Asalam o alikum. I hope you are enjoying good health by the grace of Allah Almighty.
In this previous blog,I have discussed about loops.Now,in this blog,I will discuss a simple example of While loop.

Remember that While loop is used to repeat a statement,etc if we do not know in advance how many times we want to repeat that statement,etc.So,the syntax of While loop is


Syntax

while(condition)
statement;

Or


while(condition)
{
statement(s);
}

Example 

Source code

khgamujtaba,C++

Output


khgamujtaba,C++

So,in this example,we have initialized the value of n.The initial value is 1 and we have used a while loop to repeat the statement "Happy Pakistan Resolution Day" for 5 times."n++" means to increment the value of n.The loop will continue to execute as long as the condition becomes false.

Sunday 22 March 2015

Concept of loops in C++

Today I am here with the most awaiting topic of C++,Loops.

There would be many definitions in your mind about loops and probably you might be confused about the actual concept.
So,the simple definition of loops is repetition of something.This could be either variables,constants,program statements of a block of code.


Suppose you want to write "Hello world" for 50 times.What would you do?
Will you write cout<<"Hello world"; for 50 times?

Yeah,this is a method but surely it is very time-consuming.

Loops are very useful in this type of condition.

To repeat a statement 50 times,you will use a loop and surely you will save your time as well.

So,there are mainly three type of loops in C++.

1.While loop

2.Do-while loop

3.For loop

While and do-while loops are used in a condition when we do not know in advance how many times we want to repeat a block of code or a particular statement,etc.

For loop is used in a condition when we know in  advance how many times we want to repeat a block of code or a particular statement,etc.

The examples of this topic will be discussed in the next blogs inshallah.

Saturday 21 March 2015

Conditional operator

In this blog,I will explain the concept of conditional operator.
Many students got confused with it.So,don't get confused and try to understand this simple concept.

Conditional operator or ternary operator works same as "if-else" statement.The only difference is the syntax.e.g., the syntax for "if-else" is

if(condition)
statement;
else
statement;

while the syntax of conditional operator is

(condition)?statement(if true):statement(if false);

So,now to explain conditional operator more clearly,I will use a simple example.Hope that you will find it beneficial.


Source code


khgamujtaba,C++

Output

khgamujtaba,C++



Friday 20 March 2015

Example of "if-else-if"

By practicing the below source code,you would be able to understand the concept of if-else-if statement.

Source code

khgamujtaba,C++

Output

khgamujtaba,C++

I hope it would be helpful to you.

Thursday 19 March 2015

"if-else" example

In this blog,I will show you the simple concept of "if-else".

Source code

khgamujtaba,C++

Output

khgamujtaba,C++

So the simple concept of "if-else" statement is that if a condition is true,the program statement will be executed and if a condition is false,then the alternative program statement will be executed

Wednesday 18 March 2015

Simple "if" example

Thanks every one for visiting my blog.
In this blog,I will write a simple source code that will clear your concept of "if" statement.

Source code

khgamujtaba,C++


Output

khgamujtaba,C++

It will not display -5 as it is a negative number.

Tuesday 17 March 2015

Program to determine whether two numbers are equal or not

In this program,I will tell you how to input two numbers from the user and then determine whether they are equal or not?

Source code


#include<iostream.h>
#include<conio.h>
void main( )
{
int m,n;
cout<<"Enter the first number=";
cin>>m;
cout<<"Enter the second number=";
cin>>n;
if(m==n)
cout<<"The numbers are equal.";
else
cout<<"The numbers are not equal.";
getch( );
}

Output

Program to get the absolute number

Here in this program I will tell you how to make a make a number absolute entered by the user.

Source code

#include<iostream.h>
#include<conio.h>
void main( )
{
int n;
cout<<"Enter any number=";
cin>>n;
if(n<0)
cout<<Absolute number="<<-n;
else
cout<<"Absoulte number="<<n;
getch( );
}

Output


Sunday 8 March 2015

Square root of a number entered by user

In this blog, I will tell you how to make a program that inputs a number from the user and then calculates its square root.

#include<iostream.h>
#include<conio.h>
#include<math.h>
void main( )
{
int a,b;
cout<<"Enter any number=";
cin>>a;
b=sqrt(a);
cout<<"The square root="<<b;
getch( );
}

 Output