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: