Monday, January 30, 2017

C Program to find area and circumference of circle

#include<stdio.h>
 
int main() {
 
   int radius;
   float PI = 3.14, area, circle;
 
   printf("\nEnter radius of circle:- ");
   scanf("%d", &radius);
 
   area = PI * rad * rad;
   printf("\nArea of circle :- %f ", area);
 
   circle = 2 * PI * rad;
   printf("\nCircum. :- %f ", circle);
 
   return (0);
}



OUTPUT

Enter radius of a circle :- 1
Area of circle :- 3.14
Circum.  :- 6.28

Thursday, November 3, 2016

Newton - Raphson Method

Find by Newton-Raphson method  the real root of 
   3x - Cosx - 1 = 0 in the interval [1,2]

Program Fixed_method
    Implicit none
    Real :: l , m , c , g
    Integer :: i
    Write ( * , * ) ' Input interval a and b ' 
    Read ( * , * ) l , m
    Write ( * , 67 ) ' Number' , 'value of x' , 'value of f(x)' , 'Absolute error'
    67 Format ( / , / , A7 , 7x , A11 , 5x , A14 , 2x , A15 )
    c = l
    do i = 1 , 100
        g = ( c + 10.0 ) ** ( 1.0 / 4.0 )
        Write ( * , 66 ) i , c , g , abs (c - g )
        66 Format (2x , i3 , 11x , f8.6 , 8x , f8.6 , 8x , f8.6 )
        If( abs ( c - g ) .le. 0.0000001 ) Exit
        c = g
        End do
        Write( * , 21 ) ' The approximate root of the function is  :  ' , c
        21 Format (  /  ,  / , A42 , F12.9 )
        End program


        Subroutine func ( g , x )
            Real,intent(in)::x
            Real,intent(out)::g
            g = x**4 - x - 10
            End subroutine


            Subroutine interval ( l , m )
                Real , intent(in) : : l , m
                Real :: g , h , x
                call func ( g , l )
                call func ( h , m )
                k = g * h
                If ( k .lt. 0 )then
                    x = l
                    Write ( * , * ) "There is a root in this interval . "
                    Else if ( k .gt. 0 ) then
                        x = 0
                        Write ( * , * ) 'There is no root in this interval . '
                        Elseif  ( g == 0 ) then
                            Write( * , * ) g
                            Else
                                Write( * , * ) h
                                Endif
                                End Subroutine

Input : 



Output :



Thursday, October 6, 2016

Bisection Method

Consider finding the root of  f(x) = Cos[x] – x . Let εstep = 0.01 ,  εabs = 0.001 and start with the interval [1.0 ,  0.5] .


Program Bisection_method
    Implicit none
    Real :: a , b , f , c , error
    
Write( * , * ) " Enter numbers between which the root is to be found : "   !(Can be change)
      10 read ( * , * ) a , b
 Write ( * , 61 ) " Input error value : "    !(Can be change)
       61 Format ( / , / , a18 )
        Read ( * , * ) error
     
  15 If ( f ( a ) * f ( b ) . lt . 0 )then
            c = ( a+b ) / 2.0
         else
  Write ( * , 62 ) " There is no zeros in this interval. Try with  another value of a & b "
       62 Format  ( / , / , a65 )
           go to 10
        Endif
        if ( f (a) * f (c) . lt . 0) then
            b = c
           else
          a = c
        Endif
       if ( abs (b-a) . gt . error ) goto 15
       write ( * , 63 ) " The approximate root of the function is : " , c
           63 Format ( / , / , a42 , f12.9 )
        end

    function f (x)
            implicit none
            real : : f , x
            f = cos (x) - x   !(Can be change)
    end function


Input ::




Output : :


Monday, October 3, 2016

Transpose matrix

Save   the   matrix  (A)  in  a  file  as  it  is 

 2        3          5
 7       11         0
-1      -13       -17

Print  A  and  transpose  of  A  in matrix  form .

Program coding

Program transpose_matrix
Integer , Dimension ( 3 , 3 ) : : a , b

Write ( * , 11 )  ' Input matrix  A '
11 format  ( / , A16 )

Read ( * , * ) ( ( a ( i , j ) , j = 1 , 3 ) , i = 1 , 3 )

Do i = 1 , 3
    Do j = 1,  3
        b ( i , j ) = a ( j , i )
    End do
End do

Write ( * , 12 ) ' A = '
12 Format ( / , A5 )

Do i = 1 , 3
Write ( * , * ) ( a ( i , j ) , j = 1 , 3 )
End do

Write ( * , 13 ) ' Then transpose of A  = '
13 Format  ( / , A20 )

Write ( * , 14 ) ( (  b ( i , j ) , j = 1 , 3 ) , i = 1 , 3 )
14 Format ( 3I4 , / , 3I4 ,  / , 3I4)


Stop
End

Input  :


Output :


Thursday, September 22, 2016

GDC ( Greatest Common Divisor )

Write  a  program  to  find  the GCD ( Greatest  Common  Divisor ) of  many  positive  integers .


Programming  Coding .... 

Program GCD_Program
    Implicit none
    Integer :: n , a(100) , i , c , gcd
    Write ( * , 2 ) 'How  many  numbers  input  for  GCD ?'
    2 Format ( A40 )
    Read ( * , *)  n
    Write( * , 3 ) 'Input numbers :'
    3 Format ( / , A20 )
    Read ( *, * ) ( a(i) , i = 1 , n )
    Do i = 1 , ( n - 1 )
     7   c = mod (a(i) , a(i+1))
        If  ( c == 0 ) then
            gcd = a( i + 1)
            Else
                a(i) = a ( i +1 )
                a( i+1 ) = c
                Goto 7
                End if
                End do
                Write ( * , 12 ) 'GCD = ' , gcd
                12 Format ( / , A7 , I7 )

                End Program


 Input ::





Output ::                                                           
                                    


Saturday, September 10, 2016

Decimal Number to Binary Equivalent

Write  a  Program  to  convert  a  Decimal  number  to  its  Binary  equivalent .

Program Coding    : :



Program  Binary_Number
 Implicit  none
 Integer :: i_part , rem , i_base
 Real ( kind=8 ) :: Bina_num ,  Deci_num ,  r_part , r_base , ir_part
 21 Print *, "Decimal Number :"
 Read (*,*) Deci_num
 Bina_num = 0.0 !For Integer Part of Deci_num equivalent Binary
 i_base = 1
 i_part = INT(Deci_num)

 Do while (i_part>0)
     rem = Mod(i_part, 2)
     Bina_num = Bina_num + rem*i_base
   i_base = i_base*10
   i_part = Int(i_part/2)
 end do           !For Real Part of Deci_num equivalent Binary

r_base = 0.1
 r_part = Deci_num -INT(Deci_num) !Real part of Decimal number

Do while(r_part > 0)
 r_part = r_part * 2
 ir_part = Int(r_part)     !Integer part of Real_part*2
 Bina_num = Bina_num  +  ir_part * r_base
 r_base = r_base / 10.0
 r_part = r_part - ir_part      !Again Real part is only decimal value
 End do

 Write(*,121) "Binary Number :"  ,  Bina_num
 121 format(2x, A15, f20.6 , / , / )
 Goto 21
 End program

Input  : :




Output  ::




Friday, September 2, 2016

Read Underlined Digits and Print

Write  a  program  that  reads  the  underlined  digits  as  individual  integers  . Then  add 1  to  each  integer   and  print  them  .
 
                     123456
           297645
           356987


Program Coding :

Program Individuals_Integer
 Implicit none
 integer ::  N(3) , i
  open(unit=1 ,  File = "Input.dat" )
  open(unit=2 ,  File = "Output.dat" )
 Read(1,21) ( N(i), i = 1, 3)
 21 format ( / , / , 3 (1x , I1))
       Do i = 1 , 3
           N(i) = N(i) + 1             !Adding 1 to each integer
      end do
      Do i = 1 , 3
          write (2,12)  N(i)
          12 format (I2)
          end do

 end program


Input:




Output: