Wednesday, December 5, 2012

Sum of odd numbers

**Find the sum of odd numbers between 1 to 500 by using do statement.
1234567890
                 sum = 0.0
                 Do i = 1,500,2
                sum =sum+i
                end do
                write(*,*) 'The sum of odd numbers =', sum
               End
We can write it as
1234567890
                  Write(*,*) 'Enter the value of N'
                  read(*,*) N
                  sum = 0.0
                  Do i = 1,N,2
                 sum =sum+i
                end do
                write(*,*) 'The sum of odd numbers is =', sum
                End
Here we put N =500

** Find the sum of odd numbers between 1 to 200 by using If statement
Solution :
1234567890
                  Write(*,*) 'Enter the value of N'
                  read(*,*) N
                 sum = 0.0
                 Y=1
    50         sum =sum+Y
                Y =Y+2
                If ( Y .le. N) go to 50
                write(*,*) 'The sum of odd numbers is =', sum
                End
              

No comments:

Post a Comment