+ 4

Tell me please ...whats wrong with this program?

import java.util.*; class sum { public static void main(String[] args) { float x,n,s=0,i,g,h; Scanner in= new Scanner(System.in); System.out.println("enter value of x and term of the series"); x=in.nextFloat(); n=in.nextFloat(); g=math.pow(x,i); h=math.sqrt(i); for(i=1;i<=n;i++) { s=s+ g/h; } System.out.println("sum series="+s); } }

5th Mar 2018, 9:24 AM
Swati Kumari
Swati Kumari - avatar
7 Answers
+ 15
//I have listed all errors , U must have followed mine comment carefully , //btw here is the corrected code import java.util.*; class sum { public static void main(String[] args) { float x,n,s=0,i=2,g,h; Scanner in= new Scanner(System.in); System.out.println("enter value of x and term of the series"); x=in.nextFloat(); n=in.nextFloat(); g=(float)Math.pow(x,i); h=(float)Math.sqrt(i); for(i=1;i<=n;i++) { s=s+ g/h; } System.out.println("sum series="+s); } }
5th Mar 2018, 9:56 AM
Gaurav Agrawal
Gaurav Agrawal - avatar
+ 13
if u don't initialize i , then how the compiler will know that what power should x be raised ... and that value to be stored in variable g
5th Mar 2018, 9:40 AM
Gaurav Agrawal
Gaurav Agrawal - avatar
+ 12
1)System.out.print 2)Math.pow 3)Math.sqrt 4)Math.pow & Math.sqrt returns a double value , so there will be loss of precision when assigned to float type , its better to use typecasting 5)In Math.pow(x,i) //here i is not initialized //only these errors comed to my notice 😅
5th Mar 2018, 9:33 AM
Gaurav Agrawal
Gaurav Agrawal - avatar
+ 6
Variable i is not initialized
5th Mar 2018, 9:36 AM
David Akhihiero
David Akhihiero - avatar
+ 4
we don't initialize 'i'... m talking about your 5th point
5th Mar 2018, 9:38 AM
Swati Kumari
Swati Kumari - avatar
+ 3
and what's wrong...I'm not getting required output
5th Mar 2018, 9:31 AM
Swati Kumari
Swati Kumari - avatar
+ 3
Thanks...the way I have written g&h ...there it's shows a error...even after intializing 'i' ... what's wrong there....
5th Mar 2018, 9:42 AM
Swati Kumari
Swati Kumari - avatar