[Go to site: main page, start]

0% found this document useful (0 votes)
4 views5 pages

Java For Loop Programs

The document contains Java programs that demonstrate various uses of for loops, including calculating sums of natural numbers, even and odd numbers, squares, cubes, and factorials. It also includes programs for generating multiplication tables and summing their products. Each program prompts the user for input where necessary and displays the results.

Uploaded by

guha.p
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views5 pages

Java For Loop Programs

The document contains Java programs that demonstrate various uses of for loops, including calculating sums of natural numbers, even and odd numbers, squares, cubes, and factorials. It also includes programs for generating multiplication tables and summing their products. Each program prompts the user for input where necessary and displays the results.

Uploaded by

guha.p
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

JAVA FOR LOOP PROGRAMS

1) Sum of First 10 Natural Numbers

import [Link].*;

public class SumFirst10 {


public static void main() {
int sum = 0;
for(int i = 1; i <= 10; i++) {
sum = sum + i;
}
[Link]("Sum = " + sum);
}
}

2) Sum of First n Natural Numbers

import [Link].*;

public class SumN {


public static void main() {
[Link] sc = new [Link]([Link]);
[Link]("Enter value of n:");
int n = [Link]();

int sum = 0;
for(int i = 1; i <= n; i++) {
sum = sum + i;
}
[Link]("Sum = " + sum);
}
}

3) Sum of Even Numbers Between 1 and 50

import [Link].*;
public class EvenSum {
public static void main() {
int sum = 0;
for(int i = 2; i <= 50; i = i + 2) {
sum = sum + i;
}
[Link]("Sum = " + sum);
}
}

4) Sum of Odd Numbers Between 1 and 100

import [Link].*;

public class OddSum {


public static void main() {
int sum = 0;
for(int i = 1; i <= 100; i = i + 2) {
sum = sum + i;
}
[Link]("Sum = " + sum);
}
}

5) Sum of Squares from 1 to 10

import [Link].*;

public class SquareSum {


public static void main() {
int sum = 0;
for(int i = 1; i <= 10; i++) {
sum = sum + (i * i);
}
[Link]("Sum of squares = " + sum);
}
}

6) Sum of Cubes from 1 to n


import [Link].*;

public class CubeSum {


public static void main() {
[Link] sc = new [Link]([Link]);
[Link]("Enter value of n:");
int n = [Link]();

int sum = 0;
for(int i = 1; i <= n; i++) {
sum = sum + (i * i * i);
}
[Link]("Sum of cubes = " + sum);
}
}

7) Series: 2 + 4 + 6 + ... up to 20

import [Link].*;

public class SeriesEven {


public static void main() {
int sum = 0;
for(int i = 2; i <= 20; i = i + 2) {
[Link](i + " ");
sum = sum + i;
}
[Link]("\nSum = " + sum);
}
}

8) Series: 1 + 3 + 5 + ... up to n terms

import [Link].*;

public class SeriesOddTerms {


public static void main() {
[Link] sc = new [Link]([Link]);
[Link]("Enter number of terms:");
int n = [Link]();
int sum = 0;
int num = 1;

for(int i = 1; i <= n; i++) {


[Link](num + " ");
sum = sum + num;
num = num + 2;
}
[Link]("\nSum = " + sum);
}
}

9) Factorial of a Number

import [Link].*;

public class Factorial {


public static void main() {
[Link] sc = new [Link]([Link]);
[Link]("Enter a number:");
int n = [Link]();

int fact = 1;
for(int i = 1; i <= n; i++) {
fact = fact * i;
}
[Link]("Factorial = " + fact);
}
}

10) Multiplication Table & Sum of Products

import [Link].*;

public class TableSum {


public static void main() {
[Link] sc = new [Link]([Link]);
[Link]("Enter a number:");
int n = [Link]();

int sum = 0;
for(int i = 1; i <= 10; i++) {
int product = n * i;
[Link](n + " x " + i + " = " + product);
sum = sum + product;
}
[Link]("Sum of products = " + sum);
}
}

You might also like