Saturday, October 13, 2012

Convert Feet to Meter and Meter to Feet

Q. Write a C program to convert meter to foot and feet to meter.

Ans.

keep in mind:

1 Meter = 3.28 Foot
1 Foot  = 0.30 Meter

/*c program for convert meter to foot and vice-versa*/
#include<stdio.h>
int main()
{
 int ch;
 double meter,foot;
 printf("\nEnter 1 for convert meter to foot.");
 printf("\nEnter 2 for convert foot to meter.");
 printf("\nEnter 0 for exit.");
 printf("\n\nEnter your choice : ");
 scanf("%d", &ch);
 switch(ch)
 {
  case 1:
    printf("\nEnter value in meter: ");
    scanf("%lf", &meter);
    foot = (3.28084) * meter;
    printf("\n\t-- Convert Meter to Foot --\n");
    printf("\n%lf meter = %lf foot",meter,foot);
    break;
  case 2:
    printf("\nEnter value in foot: ");
    scanf("%lf", &foot);
    meter = (.3048) * foot;
    printf("\n\t-- Convert Foot to meter --\n");
    printf("\n%lf foot = %lf meter",foot,meter);
    break;
  case 0:
    goto exit;
  default:
    printf("\nYou enter invalid options.");
 }
 exit:
 return 0;
}


The output of above program would be:


Output of convert meter to feet C program
Figure: Screen shot for convert meter to feet C program



Output of convert feet to meter C program
Figure: Screen shot for convert feet to meter C program

Friday, October 12, 2012

Flowchart for switch-case

How to write switch case flowchart?

switch case statement:

switch( choice )
{
  case 1:
      statement 1;
      break;

  case 2:
      statement 2;
      break;

  case 3:
      statement 3;
      break;

  case n:
      statement n;
}

(in above module n = number of cases.)


switch case flowchart:



switch case statement flowchart in C
Figure: flowchart of switch case statement in C

Related programs:

  1. Flowchart for if, if..else, nested if..else, if..else if
  2. Flowchart for prime number
  3. Flowchart for finding Armstrong number
  4. Sum of square - C program, Algorithm and Flowchart
  5. Factorial - C program, Algorithm and Flowchart

Tuesday, October 9, 2012

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]

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]

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