Question 12 : Write a program in Java to compute the sum of the even numbers upto n terms using the for loop.
Java Program :
class sum_even
{
public static void main(int n)
{
int s=0,i,c=0;
for(i=1;;i++)
{
if(i%2==0)
{
s=s+i;
c=c+1;
}
if(c==n)
break;
}
System.out.print("Sum : "+s);
}
}
Output :
When n=15;
Sum : 240
Note : Use the same procedure to print the sum of odd numbers upto n terms.
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 n)
{
int s=0,i,c=0;
for(i=1;;i++)
{
if(i%2==0)
{
s=s+i;
c=c+1;
}
if(c==n)
break;
}
System.out.print("Sum : "+s);
}
}
Output :
When n=15;
Sum : 240
Note : Use the same procedure to print the sum of odd numbers upto n terms.
Note : Use the calculator and the notepad provided, at the top, to dry run the program.
write a program to display 100 prime numbers
ReplyDeleteclass sudhi1{
Deletepublic static void main(String args[]){
int i,j,k;
for(i=0;i<=100;i=i+1){
k=0;
for(j=2;j<i;j=j+1){
if(i%j==0){
k=1;break;
}
}
if(k==0){
System.out.println(i);
}
}
}
}
This comment has been removed by the author.
Deletewrite a program to display 100 prime numbers
ReplyDeleteclass sudhi1{
Deletepublic static void main(String args[]){
int i,j,k;
for(i=0;i<=100;i=i+1){
k=0;
for(j=2;j<i;j=j+1){
if(i%j==0){
k=1;break;
}
}
if(k==0){
System.out.println(i);
}
}
}
}
1
ReplyDelete3 1
5 3 1
7 5 3 1
9 7 5 3 1
Please solve it out fr me
i have the same model program
ReplyDelete1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
if need i will send it to you because i dont have time to create a new one><<<<<<<<<<<<,,,,,,,,,,,,,,,,<,.,,.
i have the same model program
ReplyDelete1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
if need i will send it to you because i dont have time to create a new one><<<<<<<<<<<<,,,,,,,,,,,,,,,,<,.,,.
Wap to print the following pattern
ReplyDeleteschool
schoo
scho
sch
sc
s
Plz reply
Wap to accept a no. And calculate the sum of even and odd digits separately.
ReplyDeleteBLUEJ
ReplyDeleteLUEJB
UEJBL
EJBLU
JBLUE
how to print this pattern?