Sunday, October 7, 2012

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...else construct 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

Saturday, October 6, 2012

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 program

Rectangle number pyramid

Q. Write a C program for print the following number design or number pyramid or number rectangle:

33333
32223
32123
32223
33333

Ans.

To make the above program make module/program and add all module(tips):
Make 3 parts of the design and write down code and at the end, add all parts/module
Figure: How to write above program tips


/*c program for print number rectangle pyramid*/
#include<stdio.h>
int main()
{
 int i,j,k,n=3,r;
 for(j=1; j<=5; j++)
    printf("%d",n);
 printf("\n");
 for(r=1; r<=3; r++)
 {
  for(i=3; i>=2; i--)
    printf("%d", i);
  for(k=n-r; k>=0; k--)
  {
    if(k==0)
      printf("2");
    else
    {
      printf("%d",k);
      break;
    }
  }
  for(i=2; i<=3; i++)
     printf("%d",i);
  printf("\n");
 }
 for(j=1; j<=5; j++)
     printf("%d",n);
 return 0;
}

The output of above program would be:
Output of rectangle number pyramid C program
Screen shot for rectangle number pyramid C program


Wednesday, October 3, 2012

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

Related program:

  1. Search prime number
  2. Generate first n prime number
  3. Print prime number 1 to 100

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

Tuesday, October 2, 2012

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


Related programs:

  1. Print Armstrong number range C program
  2. Amicable number C program
  3. Perfect number C program

Monday, October 1, 2012

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

Related programs:

  1. What is Algorithm and Flowchart
  2. Basic of Algorithm
  3. Flowchart for search prime number
  4. Factorial C program, Algorithm and Flowchart
  5. Flowchart for check a number is Armstrong number or not