Monday, 1 September 2014

Sum of square program, Algorithm and Flowchart

Sum of square program, Algorithm and Flowchart

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

Ans.

/*c program for accept number and calculate sum of square*/
#include<stdio.h>
int main()
{
 int num,sum=0,i;
 printf("Enter any number : ");
 scanf("%d"&num);
 for(i=1; i<=num; i++)
    sum = sum + (i*i);
 printf("Sum of square of %d = %d",num,sum);
 return 0;
}

The output of above program would be:
Output of sum of square of any number C program
Figure: Screen shot for calculate sum of
square of number C program



Algorithm for accept a number from user and calculate sum of square:

[Sum of square procedure:accept number num from user, and set sum=0 and calculate sum of square.]
Step 1. Start
Step 2. Read number num
Step 3. [Initialize]
        sum=0, i=1
Step 4. Repeat step 4 through 6 until i<=num
Step 5. sum=sum+(i*i)
Step 6. i=i+1
Step 7. print the sum of square
Step 8. stop
[end of loop step 4]
[end of sum of square procedure]


Flowchart for sum of square C program as following:
flowchart for accept number and calculate sum of square
Figure: Flowchart for calculate sum of square C progra

Algorithm for number pyramid

Algorithm for number pyramid

Q. write a C program for following number pyramid and also write down algorithm:

1234554321
1234__4321
123____321
12______21
1________1

Ans.


C program for above number pyramid structure:
click here


Algorithm for above number pyramid as:

[procedure of number pyramid]

step1: Start
step2: Read number num
step3: [initialize]
       r=1
step4: Repeat step 4 to 21 until num>=1
step5: [initialize]
       c=1
step6: Repeat step 6 to 8 until c<=num
step7: print value of c
step8: c=c+1
       [end of step6 loop]
step9: [initialize]
        sp=r
step10: Repeat step 10 to 12 until sp>1
step11: print _
step12: sp=sp-1
        [end of step10 loop]
step13: [initialize]
        sp=r
step14: Repeat step 14 to 16 until sp>1
step15: print _
step16: sp=sp-1
        [end of step14 loop]
step17: [initialize]
        c=1
step18: Repeat step 18 to 20  until c>=1
step19: print value of c
step20: c=c-1
        [end of step18 loop]
step21: Go to next line
        [end of step4 loop]
step22: Stop
[end procedure of number pyramid]

Algorithm for star pyramid

Algorithm for star pyramid

Q. Write a C program to print the following star program. Also write down the algorithm.

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

Ans.

C program for above star pyramid at:

click here

Algorithm for above star pyramid as follows:

[star pyramid procedure]
step1: Start
step2: Read number num
step3: [initialize]
       r=1
step4: Repeat step 4 through 10 until num>=r
step5: [initialize]
       c=1 
step6: Repeat step 6 through 8 until c<=r
step7: print */#
step8: c=c+1
       [end of loop step6]
step9: go to next line
step10: r=r+1
        [end of loop step4]
step11: stop
[end of star pyramid procedure]

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