Showing posts with label algorithm and flowchart. Show all posts
Showing posts with label algorithm and flowchart. Show all posts

Program to find Armstrong Number between 1 to 500

#include<stdio.h>
#include<math.h>

int main()
{
    printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");
    int n,sum,i,t,a;
    printf("\n\n\nThe Armstrong numbers in between 1 to 500 are : \n\n\n");

    for(i = 1; i <= 500; i++)
    {
        t = i;  // as we need to retain the original number
        sum = 0;
        while(t != 0)
        {
            a = t%10;
            sum += a*a*a;
            t = t/10;
        }

        if(sum == i)
        printf("\n\t\t\t%d", i);
    }

    printf("\n\n\n\n\t\t\tCoding is Fun !\n\n\n");
    return 0;
}

Mixing variable types

Mixing variable types

Exercise 3.1

Copy   divide.f95 from Downloads
Make sure you understand the following program thoroughly!
  program divide
   implicit none
   integer  :: x
   real  :: y
   x  = 1
   y = x/3
   print  *, y
   end program divide
Run it. This program produces the following output:
  0.00000
Something odd is happening. The problem is the line:
  y=x/3
FORTRAN evaluates the right  hand side of the assignment first using integer arithmetic, because both x and 3 are integer. 1 divided by 3 cannot be stored as an integer, and so the value 0 is returned. The result, 0, is then converted to a real number and the assigned to y.
Replace the line in program divide
  
  x = 1       
by  
  
  x = 10
Your output should now be:
  3.00000
Can you see what is happening? FORTRAN is keeping the integer part of the answer and throwing the rest away.
To get over this problem, we have to signal to FORTRAN that we want it to calculate the right hand side of the expression using real arithmetic. If we want  to keep x as integer data type, we could re-write our expression as follows:
                y=x/3.0
The presence of a real number on the right hand side causes the right hand side of the expression to be evaluated using floating point arithmetic.
Actually, the problem is even more complicated! Where we have an expression like
                y=x * ((2**i)/3)
where x and y are real and i is integer, FORTRAN computes the result in stages:
First it calculates (2**i)/3 and evaluates it as an integer number, then multiplies the result by x and evaluates it as real.

Exercise 3.2

Copy check.f95 to your filestore.
    
  program check
    !Integer and real arithmetic
    implicit none
    real :: x,y
    integer i
    x=2.0
    i=2
    y=x*((2**i)/3)
    print *,y
    y=x*((2.0**i)/3)
    print *,y
    end program check 
   
… and examine its output. Make sure you understand why this is happening.

The do loop

Unless we are able to re-execute code, we might as well use a calculator… Now we start to take advantage of the power of the computer.

Exercise 3.3

Copy program loop.f95
      
  program  loop
  implicit none     
    integer  :: i
    do  i=0,20
     print  *,i
    end  do
    end  program loop
  
  
Run the program. It prints out the numbers from 0 to 20 in steps of 1.
Note:
  • i is called a loop counter. In this example, it has a start value of zero.
  • All the statements within the do and end do are executed. In this example there is just the one statement, ie print.
  • Each time the statements are executed, the loop counter, i, is incremented by 1.
  • When the value of i is 20, the loop terminates, and the program resumes after the end do.
Change the do statement in program loop as follows:
  do i = 50,70,2
Run the program. What happens?
The third argument in the do statement, is the increment step.  If omitted, the value is taken as 1.
Loops can also decrement: try this
 
  do i = 5,-5,-2
 

Exercise 3.4

Using a do loop to generate integer values of x between –10 and 10 in steps of 1, write a program that constructs a table of values of
  y = 1.0/x 
What happened when x had the value zero? Use an if, end if to test for the condition that gives the incorrect value, and print out an appropriate message. Compare your result with divbyzero.f95.
Division by zero
is one of the commonest reasons for a program to fail

Nested Do Loops

We want to construct a table of values for z where
  z = xy           
for values of          x in the range 1 to 2 in steps of 0.5 and
y in the range 1 to 2 in steps of 0.5
Work through the next exercise which illustrates this:

Exercise 3.5

Copy program xytab.f95 to your filespace.
  program  xytab
    implicit none
    !constructs a table of z=x/y for values of x from 1 to 2 and 
    !y from 1 to 4 in  steps of .5
    real         ::   x, y, z 
    print *, '           x           y           z'
    do  x = 1,2
     do y = 1,4,0.5
      z = x/y
      print *, x,y,z
     end do
    end  do
    end  program xytab
  
Examine its output. Notice the use of the first print  to give a heading to the table.

Using loops to do summation

Earlier on, we discussed the idea of assignments.
  x = 1.0
means store the value 1.0 in the memory location called x.
If we had the following code:
  x = 1.0
  x = x +  1.0
  print *, x
        
Can you guess what value would be printed out for x?
The answer would be 2.0.
Really important!
Bearing in mind the definition of an assignment,  the statement
  x = x  + 1.0
        
means “add 1.0 to the value currently stored in memory location x and then store the result in memory location x”.

Exercise 3.6

Copy file increment.f95 to your file space and examine its output.
    program increment
    implicit none
    integer :: i
    real :: x
    x = 1.0
    do i=1,10
     x = x + 1.0
     print *, i,x
    end do
    end program increment
  • Note carefully that we have set the initial value of x outside of the do loop. Why have we done this? If you aren't sure – change the code to put the line x = 1.0 inside the loop – then examine the output.
  • It is important to understand that if we use constructions such as x = x + 1.0, then it is vital to initialise x to some value. If we don't, it is possible that the value might be set to any random number. Run the program, make a note of the final value of x then put an exclamation mark in front of the x = 1.0 statement and run the program again.

Exercise 3.7

Edit the line x = x + 1.0 in program increment.f95, and change it to x = x * i. Re-run the program and examine the output. What is significant mathematically about the sequence of numbers that has been generated?

Algorithm & Flowchart



                                                                    Algorithm

Introduction:

In order to develop any problem in computer science generally we write programs before writing programming languages . It is always better to write we write a informal discretional for the solution, Which is also called as algorithm.

An algorithm is just like a cooking recipes for example:

If you want to prepare any tea:
Ingredients:
1.       2 cups of water
2.       Tea powder
3.       2 table spoon sugar
4.       Match box to light the gas stove
5.       Bowl
6.       Cups are required to serve
7.       1 cup of Milk
Instructions:
    Step 1: switch on the gas stove
    Step 2: Bowl should be kept on the gas
    Step 3: pour the water into the bowl    
    Step 4: add two tea spoon of tea powder
    Step 5: Wait until it is boiled
    Step 6:  take 2 tea spoons of sugar and add into the boiled water
    Step 7:  Wait for 2 minus for boiling the water
    Step 8:  add milk to the boiled water
    Step 9:  Let it be boiled
    Step 10: Now Tea is ready
    Step 11: Switch off the gas stove.

In the above instructions are fairly precised these instruction are not required for Expert person. These instructions are useful for beginners, in the same way algorithms are very much important to the beginners.

Now we see we have some problem.

Problem-> programs like c

Before writing the program if u write a blue print it is informal language ie. English like language.
To describe what we want to write so that it will be easier for us to communicate before implementation.
So that is called algorithm.
So before going for any problem to solve the problem for writing the program we need the algorithm.
In general if there is a problem p1, there might be many solutions. Let me call these solutions are algorithms. So there may be many algorithms(solutions) like A1,A2,A3.,,,,
Now before implementing any algorithm as a program we have to find out which algorithms are good in terms of time and memory. So we should analyze every algorithm like time here time means how much time is taking to execute. And Second is Memory which one will take less memory. So less time and less memory. So this code design analysis of algorithm .
Design Analysis of Algorithm: here design talk about how to design various algorithms like for a given problem (example p1), how many algorithms are there and how to analyze them so what will be time taken this algorithm, what will be the memory by each algorithm.
Once we selected the best algorithm example A2 is the best algorithm, then I implement that algorithm. So that is the main process of designing the algorithms.


What is Algorithm?




Definetion:

Algorithm is used to solution of a problem step by step procedure using simple english language. It is the part of software designing. An algorithm defines as the step by step procedure or method that can be carried out for solvingprogramming problems. An algorithm tells computer that how to solve problem systematically to get desired output. Mainly we follow following steps to design an algorithm.

Step 1 - START
It represents beginning of the algorithm.

Step 2 - DECLARE
The variables used in algorithm are declared in this step.

Step 3 - INPUT
Here we input the values

Step 4 - FORMULA
The required result is generated in this step.

Step 5 - OUTPUT
It displays the output or result.

Step 6 - STOP
It is an end of Algorithm




                                                  
              FLOW CHART
Flow char is a graphical(pictorial) representation of algorithm. It is used to represent algorithm steps into graphical format. It also gives us an idea to organize the sequence of steps or events necessary to solve a problem with the computer. In other words flow charts are symbolic diagrams of operations and the sequence, data flow, control flow and processing logic in information processing. These charts are used to understand any working sequence of the program.

What are the Advantages(uses) of FLOW CHART?
Ans:
Advantages of flowchart:-

1. It provides an easy way of communication because any other person besides the programmer can understand the way they are represented.

2. It represents the data flow.

3. It provides a clear overview of the entire program and problem and solution.

4. It checks the accuracy in logic flow.

5. It documents the steps followed in an algorithm.

6. It provides the facility for coding.

7. It provides the way of modification of running program.

8. They shows all major elements and their relationship.

erere


Terminator
This symbol represents the beginning and end point in a program. We use start and stop option in it.

Input/Output Symbol
This symbol is used to take any input or output in the algorithm.

Process Symbol
A rectangle indicates the processing, calculation and arithmetic operations

Decision Symbol
It is used when we want to take any decision in the program.

Connector Symbol
This symbol is used to connect the various portion of a flow chart. This is normally used when the flow chart is split between two pages

Data Flow Symbol
This symbol is used to display the flow of the program. It shows the path of logic flow in a program.

1. Write a c program to display "this is my first c program" on monitor
ans:
ALGORITHM
1. START
2. use the printf() function to print the message "this is my first c program".
3. use getch() function to see the output
4. STOP

FLOW CHART

2. Write a c program to find the area of a triangle for the given three sides
ans:
Let a,b,c be the lengths of the sides of a triangle. The area is given by:
Area = √s(s−a)(s−b)(s−c)  
where s is semiperimeter (half the perimeter)
a+b+c/2

 

Algorithm:

1. START
2. declare the floating variables a,b,c,s,area.
3. use the print() to print the message "enter three sides"
4. use the scanf() to read the values of sides from keyboard
5. write the logic for s. s=a+b+c/2
6. write the logic for area=root of (s(s-a)(s-b)(s-c)
7. use the printf() to print the area.
8. STOP

Flow chart

3. WAP to find whether a given year is leap or not?

To check whether a year is a leap year or not, you need to check the following 3

conditions:
1. Any year that is divisible by 400 is definitely a leap year.
2. If it is not divisible by 400, then check if it is divisible by 100, if so, then it is

NOT a leap year (even if it is divisible by 4), and
3. If the above two conditions are not satisfied we check for divisibility by 4, it it is

divisible by 4 it is a leap year.

For example,
(we know that 2000 is a leap year)
if year = 2400, it is leap year,(Condition 1 satisfied)
but if year = 2200, is NOT a leap year, (Cond. 2 satisfied),
and if year = 2020, is a leap year, (Cond. 3 satisfied).

You can use simple if else statements to write the program.
You can read Leap Years or Leap year for better understanding of why every 400th year is

taken as a leap year and not every 100th.

Algorithm

1. START
2. declare year of type integer.
3. use the print() to print the message "enter the year"
4. use the scanf() to read the year from keyboard
5. check the following condition with if..else statement
 (year%4==0&&year%100!=0 || year%400==0)
 (if an year is divisible by 4 AND not divisible by 100, OR is divisible by 400 then it is called Leap Year
6. if the above condition is TRUE then Print "leap year"
7. if the above condition is FALSE then Print "not a leap year".
8. STOP




To Determin the given number is positive, negative or zero
to find sum of first number

Algorithm to find the sum of first n natural numbers:
1. Read n.
2. Initialize a=1.
3. Initialize sum s=0.
4. Calculate s=s+a.
5. Calculate a=a+1.
6. if a>n, then goto step 7 else goto step 4.
7. Write the sum s.
8. Stop.






Draw a flowchart to find the largest of three numbers A, B, and C.









OLDER POST

Head Constable (Ministerial)

Head Constable (Ministerial) Apply Now   Notification