Nested Loops For C and Java
Nested Loops For C and Java
int main() {
int i;
-----------------------------------------------
#include <stdio.h>
int main()
{
int num, count, sum = 0;
return 0;
}
----------------------------------------------
while loop
while (testExpression) {
// the body of the loop
}
#include <stdio.h>
int main() {
int i = 1;
while (i <= 5) {
printf("%d\n", i);
++i;
}
return 0;
}
----------------------------
#include <stdio.h>
int main() {
double number, sum = 0;
printf("Sum = %.2lf",sum);
return 0;
}
--------------------------------------
Nested loops
Half Pyramid of *
#include <stdio.h>
int main() {
int i, j, rows;
printf("Enter the number of rows: ");
scanf("%d", &rows);
for (i = 1; i <= rows; ++i) {
for (j = 1; j <= i; ++j) {
printf("* ");
}
printf("\n");
}
return 0;
}
*
**
***
****
*****
------------------------------------
#include <stdio.h>
int main() {
int i, j, rows;
printf("Enter the number of rows: ");
scanf("%d", &rows);
for (i = 1; i <= rows; ++i) {
for (j = 1; j <= i; ++j) {
printf("%d ", j);
}
printf("\n");
}
return 0;
}
1
12
123
1234
12345
---------------------------------------------------
#include <stdio.h>
int main() {
int i, j, rows;
printf("Enter the number of rows: ");
scanf("%d", &rows);
for (i = rows; i >= 1; --i) {
for (j = 1; j <= i; ++j) {
printf("* ");
}
printf("\n");
}
return 0;
}
*****
****
***
**
*
-----------------------------------------------
#include <stdio.h>
int main() {
int i, j, rows;
printf("Enter the number of rows: ");
scanf("%d", &rows);
for (i = rows; i >= 1; --i) {
for (j = 1; j <= i; ++j) {
printf("%d ", j);
}
printf("\n");
}
return 0;
}
12345
1234
123
12
1
---------------------------------------
Floyd's Triangle.
#include <stdio.h>
int main() {
scanf("%d", &rows);
++number;
printf("\n");
}
return 0;
23
456
7 8 9 10
----------------------------------
*********
*******
*****
***
#include <stdio.h>
int main() {
scanf("%d", &rows);
printf(" ");
printf("* ");
printf("* ");
printf("\n");
return 0;
------------------------------------------------------------
Factorial of a Number
#include <stdio.h>
int main() {
int n, i;
if (n < 0)
else {
fact *= i;
return 0;
-------------------------------------
#include<stdio.h>
int n;
scanf("%d",&n);
return 0;
if (n>=1)
return n*multiplyNumbers(n-1);
else
return 1;
-------------------------------------------
The Fibonacci sequence is a sequence where the next term is the sum of the previous two
terms. The first two terms of the Fibonacci sequence are 0 followed by 1.
#include <stdio.h>
int main() {
int i, n;
int t1 = 0, t2 = 1;
scanf("%d", &n);
t1 = t2;
t2 = nextTerm;
nextTerm = t1 + t2;
return 0;
Output
------------------------------------------------
#include <stdio.h>
int main() {
int t1 = 0, t2 = 1, nextTerm = 0, n;
scanf("%d", &n);
nextTerm = t1 + t2;
t1 = t2;
t2 = nextTerm;
nextTerm = t1 + t2;
return 0;
Output
#include <stdio.h>
int main() {
long long n;
int count = 0;
scanf("%lld", &n);
do {
n /= 10;
++count;
} while (n != 0);
}
----------------------------------------
Reverse an Integer
#include <stdio.h>
int main() {
scanf("%d", &n);
while (n != 0) {
remainder = n % 10;
n /= 10;
return 0;
Output
#include <stdio.h>
int main() {
scanf("%d", &n);
original = n;
while (n != 0) {
remainder = n % 10;
n /= 10;
if (original == reversed)
printf("%d is a palindrome.", original);
else
return 0;
Output
1001 is a palindrome.
----------------------------------------
#include <stdio.h>
int main() {
int n, i, flag = 0;
scanf("%d", &n);
if (n % i == 0) {
flag = 1;
break;
if (n == 1) {
else {
if (flag == 0)
else
return 0;
---------------------------------------------------------
---------------------------------------------------------
#include <stdio.h>
void main() {
int rows, i, j;
printf("\n");
Output:
12345
1234
123
12
--------------------------------------
#include <stdio.h>
void main() {
int rows, i, j;
scanf("%d", &rows);
printf("\n");
Output:
55555
4444
333
22
-----------------------------------------
#include <stdio.h>
void main(){
int rows, i, j;
scanf("%d", &rows);
printf( "\n" );
Output:
654321
54321
4321
321
21
1
--------------------------------------
#include <stdio.h>
void main() {
int rows, i, j;
scanf("%d", &rows);
printf("\n");
Output:
65432
6543
654
65
---------------------------------
#include <stdio.h>
void main() {
int rows, i, j;
scanf("%d", &rows);
printf( "\n" );
Output:
123456
23456
3456
456
56
--------------------------------
#include <stdio.h>
void main() {
int rows, i, j;
scanf("%d", &rows);
printf("\n");
Output:
12
123
1234
12345
123456
---------------------------------
#include <stdio.h>
void main(){
int rows, i, j;
scanf("%d", &rows);
printf("\n");
Output:
65
654
6543
65432
654321
-----------------------------------
#include <stdio.h>
void main() {
int rows, i, j;
scanf("%d", &rows);
printf("\n");
Output:
12
123
1234
12345
---------------------------------
#include <stdio.h>
void main() {
int rows, i, j;
scanf("%d", &rows);
printf("\n");
Output:
22
333
4444
55555
-----------------------------------
#include <stdio.h>
void main() {
int rows, i, j;
scanf("%d", &rows);
printf("\n");
Output:
Enter Number Of Rows You Want: 5
12345
2345
345
45
------------------------------
#include <stdio.h>
void main(){
int rows, i, j;
scanf("%d", &rows);
printf("\n");
Output:
54
543
5432
54321
-------------------------------------
#include <stdio.h>
void main() {
int rows, i, j;
scanf("%d", &rows);
printf("\n");
Output:
55
444
3333
22222
111111
------------------------------
#include <stdio.h>
void main() {
int rows, i, j;
scanf("%d", &rows);
printf("\n");
Output:
22222
3333
444
55
------------------------------------
Java samples
int i;
int t1 = 0, t2 = 1;
// initialize the next term (3rd term)
int n =Integer.parseInt(args[0]);
//System.out.println(countPositive(new int[]));
t1 = t2;
t2 = nextTerm;
nextTerm = t1 + t2;
Factorial of a Number
public class Main
int i;
long fact = 1;
int n =Integer.parseInt(args[0]);
if (n < 0)
else {
fact *= i;
}
}
int i, j;
System.out.println(" ");
12345
1234
123
12
int i, j;
System.out.println(" ");
}
}
int i, j;
System.out.println(" ");
System.out.println(" "+args[1]);
System.out.println(" "+args[2]);