WizIQ helps you learn and teach online - any subject you can think of!
Join for FREE

FOR LOOP FOR understanding

Add to Favourites
Post to:
Comments
Presentation Transcript Presentation Transcript

Slide 1 : public class p13 { public void printNos() { int i=0; for( i=1;i <= 10; ++i) { System.out.println(i); } } }

OUTPUT : OUTPUT 1 2 3 4 5 6 7 8 9 10

New example : New example public class P14 { public void test(int n) { int i,sum=0; for( i=1;i <= n; ++i) { System.out.println(i); sum = sum + i; } System.out.println("The sum of first " + n + "natural numbers is " + sum); } }

OutPut : OutPut 1 2 3 4 5 The sum of first 5natural numbers is 15

Next Problem : Next Problem public class p15 { public void fnTest() { int x=0; double fx; for( x=-10;x <= 10; x = x+2) { fx = (x*x + 1.5*x +5)/(x-3); System.out.println("For x = "+ x + " f(x) is " + fx + "\n"); } } }

New project : New project public class p15 { public void fnTest() { int x=0; double fx; for( x=-10;x <= 10; x = x+2) { fx = (x*x + 1.5*x +5)/(x-3); System.out.println("For x = "+ x + " f(x) is " + fx + "\n"); } } }

Out Put : Out Put For x = -10 f(x) is -6.923076923076923 For x = -8 f(x) is -5.181818181818182 For x = -6 f(x) is -3.5555555555555554 For x = -4 f(x) is -2.142857142857143 For x = -2 f(x) is -1.2 For x = 0 f(x) is -1.6666666666666667 For x = 2 f(x) is -12.0 For x = 4 f(x) is 27.0 For x = 6 f(x) is 16.666666666666668 For x = 8 f(x) is 16.2 For x = 10 f(x) is 17.142857142857142

Want to learn?

Sign up and browse through relevant courses.

Name:
Your Email:
Password:
Country:
Contact no.:


Area code Number
Subject you are interested in:
Word verification: (Enter the text as in image)


Sign Up Already a member? Sign In
I agree to WizIQ's User Agreement & Privacy Policy
St.Peter's School
The Oldest & best Boys residential School
User

Your Facebook Friends on WizIQ