Wednesday, November 28, 2012

work with if statement


** Find odd numbers between 1 to 100 by using both Do and If  statement.
Solution
1234567890
                 Write(*,*) ‘Enter the value of n’
                   Read(*,*) n
                Do 10 k = 1,n ,2
                Write(*,*)  ‘ the value of odd number ‘ , k
10                         Continue
                End
Description
Here we put n=100 and I use Do 10 k (here 10 is statement level) and continue is the placed at the statement End do. This is another way to write End do statement.
Here I also use k=1,n,2 . Here increment is 2 that means k takes values 1,3,5,7………..,99 here we see increment is 2.

**Now by using if statement we have to find out the odd numbers
1234567890
                Write(*,*) ‘enter the value of n’
                  Read(*,*)n
                  Y=1
    20        Write(*,*) ‘the value of the odd number ‘ , Y
                   Y=Y+2
                  If ( Y .lt. n) go to 20
                   End    
Here we put n = 100. Because we have to show the odd numbers  between 1 to 100.  
     
** Find all even number between 0 to 100 by using  if statement.
1234567890
                  write(*,*) 'Enter the value of N'
                  read(*,*) N
                  Y = 0
 10             write(*,*) 'The value of the even number' , Y
                 Y= Y+ 2
                 If (Y .lt. n) go to 10
            End

** Find all even number between 1 to 100 by using do statement
1234567890
                   write(*,*) 'Enter the value of N'
                   read(*,*) N
                    Do i = 0, N
                   write(*,*) 'The value of even number is ' , i
                   End do
                    End
Here we put N = 100.

Mean variance and standard deviation


** Find mean, variance and standard deviation for the following data      
6
4
9
12
43
51
23
13
76
56
45
36
45
34
23


Solution :To calculate the following at first we have to read the data mention above.
1234567890
               write(*,*) 'Enter the value of N'
               read(*,*) N
              sum = 0.0
              Do 10  k = 1,N
              write(*,*) 'Enter the value of x'
              read(*,*) x
             sum =sum + x
 10        Continue
              Ave = sum/N
              sum1= 0.0
              Do 20 m = 1 , N
              sum1 = sum1+(x-Ave)**2/N
 20         Continue
             Sd= sqrt( sum1)
              write(*,*)'The result of Mean is = ', Ave
              write(*,*)'The result of variance is =', sum1
              write(*,*) 'The result of s.d is =', Sd
              End
  Description :
In this problem N is the total number of observation. Here we see in the that there are 15 observation so we put N = 15 and in do loop we use up to N so that the compiler  automatically want 15 values of x and  perform the following calculation.

**Find correlation of the following data.
x
15
18
24
17
31
26
39
34
25
12
y
10
14
19
24
26
34
37
25
21
41

Solution:
We know the correlation between x and y is








From the above formula we see that we need sum of x , sum of y , sum square of x , sum square of y and sum product of xy. So at first we have to calculate following terms.
1234567890
                 Real x,y,sumx,sumy,ssqrx,ssqry, spxy, Num , Den, r
                Write(*,*) 'Enter the value of N'
                 read(*,*) N
                sumx= 0.0
                sumy= 0.0
                ssqrx= 0.0
                ssqry= 0.0
                spxy= 0.0
                Do j = 1,N
                Write(*,*) 'Enter the value of x',j
                read(*,*) x
               Write(*,*) 'Enter the value of y',j
               REad(*,*) y
               sumx = sumx+ x
               sumy = sumy+ y
               Avex= sumx/N
              Avey= sumy/N
              ssqrx= ssqrx+ x**2
              ssqry= ssqry+ y**2
              spxy=spxy+(x*y)
              End do
              Num= spxy- ((sumx*sumy)/n)
                  Den= sqrt((ssqrx-(Avex)**2)*(ssqry-(Avey)**2))
                 r = Num/Den
                 write(*,*) 'The correlation between x and y is =' , r
                  End

If statement


এখন আমরা শিখব কিভাবে If statement এর সাহায্যে Fortran এ একটি problem  solve করা যায়।
What is if statement ?
             If statement এ্কটি conditional statement যা একটি statement বা একটি arithmetic expression সত্য আথবা অসত্য নির্দেষ করে।
সাধারণত তিন ধরণের if statement পাওয়া যায়।
1.    logical if statement
2.    Block if statement
3.    Arithmetic if statement
How to solve a problem in Force2.0
Find the sum of the series by using if statement
Y=1+2+3+………….+N
Solution             
1234567890
                 Print*, ‘enter the value of n’
                 Read*, n
                 K=1
                 Sum = 0.0
10                           sum =sum + k
k=k+1    
if (k .le. n)   go to 10
print*, ‘the value of the result is ‘
print*, sum
 End
It’s works like as do statement . Here we use the  if statement if(k.le.n) means that , if k  less then equal n then  go to 10 , 10 is the statement level . And the statement (sum =sum+k) is defined as the level 10. The procedure will continue until k is less then equal n .i.e k=1,2,3,4…………..<= n. if k is > n then the process will be brought to path of end.

**Let’s do another problem
Given that


Values of  x
Result
5.65
The value of x is less then 10
8.45
The value of x is less then 10
12.7
The value of Y is = 17.9005585
21.65
The value of Y is = 48.869709
15.7
The value of Y is = 26.5071354
Given the formula  

 Then calculate Y if  x greater then 10  and if x is less then 10 then print ‘ the value of x is less then 10’
Solution
1234567890
               Do N = 1,5
                Write(*,*) ‘Enter the value of x’
                Read(*,*) x
                 If( x .gt. 10) then
                 Y=sqrt(x**0.45)+(x**2/10)
                 Print*, ‘The value of Y is = ‘, Y
                 Else
                 Print*, ‘The value of x is less then 10’
                  End if
                  End do
                 End
Description:
Here is use do statement because we have 5 values of x so N = 1, 5 as the program run 5 times for each values of x. We have to remind that do statement must be stop by the statement End do and If statement must be stop by End if statement.
If you try more you can learn more.

Saturday, November 24, 2012

sum of series using do statement

** Find sum of the series
 


1234567890
              Write(*,*) 'Enter the value of N'
               Read(*,*) N
                Sum =0.0
               Do m=1,N
               Sum =sum +(1/float(m))
               End do
               Write(*,*) ‘The result is = ' , sum
                End
(Here we use the float for converting the integer number into real . Because 1 is integer and m is also integer so in fortran the division of two integer is also integer . So we make the integer m into real . Because the division of integer and real will be a real . So we convert m into real).(এখানে আমরা float ব্যাবহার করেছি কারণ integer এবং  integer ভাগফল একটি integer ই হবে। তাই আমরা integer m কে float ব্যাবহার করে real এ পরিণত কনেছি যাতে integer ভাগ real এর ভাগফল একটি real হয়।)

**Find the sum of series
 


1234567890
             Write(*,*) 'Enter the value of n'
              Read(*,*) n
               Write(*,*) 'Enter the value of x'
              Read(*,*) x
               Sum = 0.0
              Do m =1,N
              Sum = sum + (x**m/float(m))
              End do
               Write(*,*) 'The result of the series is = ' , sum
               End

  Just copy and paste this problem on your Force 2.0 and run and put the values up to x and n you will get the result
Here the value of x and n is fixed and the value of m is variable (1,2,3,4……..n)
 ** Find the sum of the series



1234567890
             write(*,*) 'enter the value of n'
              read(*,*) n
             Sum = 0.0               
             Do k = 1,n
             Sum = sum + (k**2/float((2*k-1)))
              End do
              Write(*,*) ' The result of the sum is = ' , sum
               End
                
             
(Here we need to input n . Here n = 10 Because 10 square = 100 and (2*10)-1 = 19 and here k is itself a variable and it varies as
 k=1, 2, 3 , ………,10.






sum of square of a series using do statement


** Find sum of the following series




  1234567890
                 Write(*,*) 'Enter the value of N'
                 Read(*,*) N
                 Sum=0.0
                 Do j=1,N
                Sum=sum+j**2
                 End do
                 Write(*,*) 'The result of the series' ,sum
                 End
  Just copy and paste this problem on your Force 2.0 and run and put the values up to N and you will get the result.
** Find the sum of series 
 

 
1234567890
                Write(*,*) 'Enter the value of N'
                 Read(*,*) N
                 Sum =0.0
                 Do k =1,N
                Write(*,*) 'Enter the value of x',k
                 Read(*,*) x
                 Sum = sum +x**2
                  End do
                 Write(*,*) 'The result of the series ' , sum
                  End
  Just copy and paste this problem on your Force 2.0 and run and put the values up to x’s and you will get the result .
               




sum of a series does not having sequence


Let’s sum the following series

 Here we see that there is no sequence in the series. In this case the solution is
1234567890
              Sum=0.0               
               Do k=1,6
               Write(*,*) 'Enter the value of x'
               Read(*,*) x
               Sum=sum+x
               End do
               Write(*,*) 'The sum of the above series is =',sum
               End 
              (Here the result is 48.) . Just copy this program and paste this in your Force2.0 and run . Put the values of x that is 5 then press enter then put 9 and in the same way put 3,10,7 and 14 and press enter . The output will be (The sum of above series is =  48.)


Let’s try yourself





Here do k=1,5
As the series  have 5 values



sum of a series having sequence


আমরা এখন শিখব Do statement দিয়ে কিভাবে Fortran এর বিভিন্ন ধরণের অংক সমাধান করা যায়্।
What is Do Loop?
      The explicit do statement is an executable statement which causes a portion of program to be repeated a given number of time then the procedure called do looping and the statement rapidly executed during looping procedure are referred to as do loop.
(Do statement হচ্ছে এমন একটি statement যার সাহায্যে আমরা একটি statement কে বা কয়েকটি statement কে একসাথে বা কোন কজকে বারবার সম্পাদন করতে পারি। )
উদাহরণ:
1234567890
               Do I =1,5
              Write(*,*) 'Enter the value of x'
               End do
               End

এই problem টির output হবে
( Enter the value of x
  Enter the value of x
  Enter the value of x
  Enter the value of x
  Enter the value of x)
  Just copy and paste this problem on your Force 2.0 and run and see the result .  then it will be clear to you. এখানে Do variable টি অবশ্যই integer variable character হতে হবে অথার্ত (I, j, k, l, m, n) হতে হবে। কারণ Do variable টি সব সময় পুর্ন সংখ্যা হতে হবে।
 সমাধান কর

do loop ব্যবহার করে।
1234567890
                   Write(*,*) 'enter the value of N'
                    Read(*,*) N
                   Sum=0.0
                   Do I =1,N
                   Sum=sum+I
                   End do
                   Write(*,*) 'the result of Y is =',sum
                   End
Description on above problem
যেহেতু এখানে variable গুলোর একটি sequence আছে তাই এখানে I variable হিসাবে কাজ করবে।
উপরে do i=1,n এখানে n এর মান হচ্ছে আমরা একটি statement বা একটি কাজকে কতবার করতে চাচ্ছি তার মান। ধরি n এর মান ১০ তাহলে উপরের problem I এর মার একবার 1,তারপর 2,3,এভাবে 10 পযর্ন্ত হবে।সব প্রথম আমাদের আছে sum=0.0 তারপর  যখন I এর মান ১ তখন sum=0.0+1=1 হবে অথার্ত sum=1 হবে। তারপর end do গিয়ে আবার থেকে ঘুরে এসে আবার do তে আসলে I এর মান হবে 2 আগের sum=1 এর সাথে যোগ sum=sum+2 হবে অথার্ত sum=1+2=3 হবে। আবার end do থেকে ঘুরে এসে sum=1+2+3 এভাবে sum=1+2+3+………+10 পযর্ন্ত যোগ হবে।
এখন আমাদেরকে যদি বলা হয় 100 পযর্ন্ত যোগ করতে তাহলে just N এর যায়গায় 100
বসালে আমরা 100 পযর্ন্ত sum এর মান পাব।