Monday, 1 September 2014

if else statement and flowchart

if else statement and flowchart

Decision Control Statements and Flowchart

The if Statement
It is used to execute an instruction or sequence/block of instruction only if a condition is fulfilled.
Difference forms of implements if-statement are:

  • Simple if statement
  • if-else statement
  • Nested if-else statement
  • else if statement
simple if statement syntax and flowchart
Figure: Simple if statement syntax and flowchart

if-else statement syntax and flowchart
Figure: if-else statement syntax and flowchart


Nested if-else statement
In nested if...else statement, an entire if...elseconstruct is written within either the body of the if statement or the body of  an else statement.
The syntax is as follows:
if(condition_1)
{
 if(condition_2)
 {
   block statement_1;
 }
 else
 {
   block statement_2;
 }
}
else
{
  block statement_3;
}
block statement_4;

nested if else flowchart
Figure: nested if-else statement flowchart


Else if statement
It is used in multi-way decision on several conditions. This works by cascading comparisons. As soon as one of the conditions is true, the statement or block of statements following them is executed and no further comparison are performed.
The else...if syntax is as follows:

if(condition_1)
  block statement_1;
else if(condition_2)
  block statement_2;
else if(condition_n)
  block statement_n;
else
  default statement;

else...if flowchart statement
Figure: flowchart for else...if statement

Algorithm,Rules for constructing an Algorithm

Algorithm

An algorithm is a finite set of steps defining the solution of a particular problem. An algorithm can be expressed in human readable language like as English. Algorithm is language depended, well structured and detailed.

Rules for constructing an Algorithm

When you are going to create an algorithms, keep following point in mind as:
  • Input: There should be zero or more values which are to be supplied.
  • Output: At least one result is to be produced.
  • Definiteness: Each step must be clear and unambiguous.
  • Finiteness: If we trace the steps of an algorithm, then for all cases, the algorithm must terminate after a finite number of steps.
  • Effectiveness: Each step must be sufficiently basic that a person using only paper and pencil can in principle carry it out. In addition, not only each step is definite, it must also be feasible.
  • Comment Session: Comment is additional info of program for easily modification. In algorithm comment would be appear between two square bracket []For example: [ this is a comment of an algorithm ]
The demonstration of for loop algorithm through calculate factorial number C program/algorithm:
Algorithm for calculate factorial number
Figure: Algorithm of demonstration of how to
write "for loop" in algorithm factorial program

Let's understand to algorithm by example:

Q. Write algorithm to calculate the sum and average of two numbers.

Ans.

[procedure for calculate sum and average of two numbers]
Step1 : Start
Step2 : Read two numbers n,m
Step3 : Calculate sum=n+m
Step4 : Calculate avg=sum/2
Step5 : Print sum,avg
Step5 : Stop
[End of procedure for calculate sum and average of two numbers]


Q. Write an algorithm to convert a decimal number into binary.

Ans.

[procedure for convert decimal to binary number]
Step1 : Start
Step2 : Read number num
Step3 : Set x=1
Step4 : B(x)= num MOD 2
Step5 : num=num/2
Step6 : If num is equal to 0 then goto step8
Step7 : x=x+1
Step8 : goto step3
Step9 : Print B(x)
Step10 : x=x-1
Step11 : If x is greater than 0 then goto step8
step12 : Stop
[end of procedure for convert decimal to binary number]

Flowchart for finding Armstrong number

Flowchart for finding Armstrong number

Q. Write down the program for finding Armstrong number and also draw flowchart.

Ans.

for Armstrong number C program source code click below link:
Finding Armstrong number C program

Flowchart for finding Armstrong number C program:

Flowchart for check, a number is Armstrong or not C program
Figure: Flowchart for finding number is 
Armstrong or not C program

Factorial C program,Algorithm,Flowchart

Factorial C program,Algorithm,Flowchart

Q. Write a C program to find the factorial value of a number. Also write the algorithm and draw flowchart.

Ans.

/*c program to find out factorial value of a number*/
#include<stdio.h>
#include<conio.h>
int main()
{
 int n,i,fact=1;
 printf("Enter any number : ");
 scanf("%d"&n);
 for(i=1; i<=n; i++)
    fact = fact * i;
 printf("Factorial value of %d = %d",n,fact);
 return 0;
}

The output of above program would be:
Output of calculate factorial value  of a number C program
Screen shot for calculate factorial value
of a number C program



Algorithm for calculate factorial value of a number:

[algorithm to calculate the factorial of a number]
step 1. Start
step 2. Read the number n
step 3. [Initialize]
        i=1, fact=1
step 4. Repeat step 4 through 6 until i=n
step 5. fact=fact*i
step 6. i=i+1
step 7. Print fact
step 8. Stop
[process finish of calculate the factorial value of a number]

Flowchart for calculate factorial value of a number:

flowchart for calculate factorial value of a number
Figure: Flowchart for calculate factorial value of
a number C program

Flowchart for prime number

Flowchart for prime number

Q. Draw the flowchart diagram for check a number is prime number or not.

Ans.

Flowchart for check a number is prime or not as following:

flowchart of check a number is prime number or not
Figure: Flowchart for check given number is
prime number or not

Algorithms and Flowchart

Algorithms and Flowchart

Algorithms
To make a computer do anything, you have to write a computer program. To write a computer program, you have to tell the computer, step by step, exactly what you want it to do. The computer then "executes" the program, following each step mechanically, to accomplish the end goal.
When you are telling the computer what to do, you also get to choose how it's going to do it. That's where computer algorithms come in. The algorithm is the basic technique used to get the job done. Let's follow an example to help get an understanding of the algorithm concept.
Let's say that you have a friend arriving at the airport, and your friend needs to get from the airport to your house. Here are four different algorithms that you might give your friend for getting to your home:
The taxi algorithm:
  1. Go to the taxi stand.
  2. Get in a taxi.
  3. Give the driver my address.
The call-me algorithm:
  1. When your plane arrives, call my cell phone.
  2. Meet me outside baggage claim.
The rent-a-car algorithm:
  1. Take the shuttle to the rental car place.
  2. Rent a car.
  3. Follow the directions to get to my house.
The bus algorithm:
  1. Outside baggage claim, catch bus number 70.
  2. Transfer to bus 14 on Main Street.
  3. Get off on Elm street.
  4. Walk two blocks north to my house.
All four of these algorithms accomplish exactly the same goal, but each algorithm does it in completely different way. Each algorithm also has a different cost and a different travel time. Taking a taxi, for example, is probably the fastest way, but also the most expensive. Taking the bus is definitely less expensive, but a whole lot slower. You choose the algorithm based on the circumstances.
In computer programming, there are often many different ways -- algorithms -- to accomplish any given task. Each algorithm has advantages and disadvantages in different situations. Sorting is one place where a lot of research has been done, because computers spend a lot of time sorting lists. Here are five different algorithms that are used in sorting:
  • Bin sort
  • Merge sort
  • Bubble sort
  • Shell sort
  • Quicksort
If you have a million integer values between 1 and 10 and you need to sort them, the bin sort is the right algorithm to use. If you have a million book titles, the quicksort might be the best algorithm. By knowing the strengths and weaknesses of the different algorithms, you pick the best one for the task at hand.
Here are some interesting links:
  1. A sequential solution of any program that written in human language,called algorithm.
  2. Algorithm is first step of the solution process, after the analysis of problem, programmer write the algorithm of that problem.
  3. Example of Algorithms:
Q. Write a algorithem to find out number is odd or even?
Ans. 
step 1 : start
step 2 : input number
step 3 : rem=number mod 2
step 4 : if rem=0 then
               print "number even"
           else
               print "number odd"
           endif
step 5 : stop


Flowchart

1. Graphical representation of any program is called flowchart.
2. There are some standard graphics that are used in flowchart as following:

Start / Stop terminal box flowchart symbol
Figure: Start/Stop terminal box

Input/output flowchart symbol
Figure: Input/Output box

Process / Instructions box flowchart symbol
Figure: Process/Instruction box

Lines or Arrows to show the flow of flowchart
Figure: Lines or Arrows

Decision box flowchart symbol
Figure: Decision box

Connector box flowchart symbol
Figure: Connector box

Comment/Expression flowchart symbol
Figure: Comment box

Preparation flowchart symbol
Figure: Preparation box

Separate flowchart symbol
Figure: Separate box

Q. Make a flowchart to input temperature, if temperature is less than 32 then print "below freezing" otherwise print  "above freezing"?
Ans.
Flowchart Example of C progarm
Figure: Flowchart example of C program
Your average Pakistani boy in a not so average endless runner! Guide Sheeda as he saves his beloved chicken from a butcher who dreams only of making a tikka soup out of it.
Proudly made in Pakistan

Square number program and flowchart

Square number program and flowchart

Q. Write a C program to accept a number from user and print the square of number. Also draw the flowchart of program.

Ans.

/*c program to calculate the square of number*/
#include<stdio.h>
int main()
{
 double n,z;
 printf("Enter any number : ");
 scanf("%lf"&n);
 z = n * n;
 printf("Square of number %lf*%lf = %lf",n,n,z);
 return 0;
}

The output of above program would be:

Enter any number : 25
Square of number 25*25 = 625


Flowchart of above C square number program:

Flowchart of square number C program
Figure : Flowchart for square number C program

To make a computer do anything, you have to write a computer program. To write a computer program, you have to tell the computer, step by step, exactly what you want it to do. The computer then "executes" the program, following each step mechanically, to accomplish the end goal.
When you are telling the computer what to do, you also get to choose how it's going to do it. That's where computer algorithms come in. The algorithm is the basic technique used to get the job done. Let's follow an example to help get an understanding of the algorithm concept.
Let's say that you have a friend arriving at the airport, and your friend needs to get from the airport to your house. Here are four different algorithms that you might give your friend for getting to your home:
The taxi algorithm:
  1. Go to the taxi stand.
  2. Get in a taxi.
  3. Give the driver my address.
The call-me algorithm:
  1. When your plane arrives, call my cell phone.
  2. Meet me outside baggage claim.
The rent-a-car algorithm:
  1. Take the shuttle to the rental car place.
  2. Rent a car.
  3. Follow the directions to get to my house.
The bus algorithm:
  1. Outside baggage claim, catch bus number 70.
  2. Transfer to bus 14 on Main Street.
  3. Get off on Elm street.
  4. Walk two blocks north to my house.
All four of these algorithms accomplish exactly the same goal, but each algorithm does it in completely different way. Each algorithm also has a different cost and a different travel time. Taking a taxi, for example, is probably the fastest way, but also the most expensive. Taking the bus is definitely less expensive, but a whole lot slower. You choose the algorithm based on the circumstances.
In computer programming, there are often many different ways -- algorithms -- to accomplish any given task. Each algorithm has advantages and disadvantages in different situations. Sorting is one place where a lot of research has been done, because computers spend a lot of time sorting lists. Here are five different algorithms that are used in sorting:
  • Bin sort
  • Merge sort
  • Bubble sort
  • Shell sort
  • Quicksort
If you have a million integer values between 1 and 10 and you need to sort them, the bin sort is the right algorithm to use. If you have a million book titles, the quicksort might be the best algorithm. By knowing the strengths and weaknesses of the different algorithms, you pick the best one for the task at hand.
Here are some interesting links: