Question 11 : Write a program in Java to compute the sum of the first ten odd numbers using the for loop.
Java Program :
class sum_even
{
public static void main()
{
int s=0,i,c=0;
for(i=1;;i++)
{
if(i%2!=0)
{
s=s+i;
c=c+1;
}
if(c==10)
break;
}
System.out.print("Sum : "+s);
}
}
Output :
Sum : 100
Note : Use the calculator and the notepad provided, at the top, to dry run the program.
Java Program :
class sum_even
{
public static void main()
{
int s=0,i,c=0;
for(i=1;;i++)
{
if(i%2!=0)
{
s=s+i;
c=c+1;
}
if(c==10)
break;
}
System.out.print("Sum : "+s);
}
}
Output :
Sum : 100
Note : Use the calculator and the notepad provided, at the top, to dry run the program.
No comments:
Post a Comment