0% found this document useful (0 votes)
7 views

Nested Loops For C and Java

Uploaded by

Irfan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Nested Loops For C and Java

Uploaded by

Irfan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 41

for Loop

for (initializationStatement; testExpression; updateStatement)


{
// statements inside the body of loop
}

// Print numbers from 1 to 10


#include <stdio.h>

int main() {
int i;

for (i = 1; i < 11; ++i)


{
printf("%d ", i);
}
return 0;
}

-----------------------------------------------

// Program to calculate the sum of first n natural numbers


// Positive integers 1,2,3...n are known as natural numbers

#include <stdio.h>
int main()
{
int num, count, sum = 0;

printf("Enter a positive integer: ");


scanf("%d", &num);

// for loop terminates when num is less than count


for(count = 1; count <= num; ++count)
{
sum += count;
}

printf("Sum = %d", sum);

return 0;
}
----------------------------------------------

while loop

while (testExpression) {
// the body of the loop
}

// Print numbers from 1 to 5

#include <stdio.h>
int main() {
int i = 1;

while (i <= 5) {
printf("%d\n", i);
++i;
}

return 0;
}

----------------------------

// Program to add numbers until the user enters zero

#include <stdio.h>
int main() {
double number, sum = 0;

// the body of the loop is executed at least once


do {
printf("Enter a number: ");
scanf("%lf", &number);
sum += number;
}
while(number != 0.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;
}

*
**
***
****
*****

------------------------------------

Half Pyramid of Numbers

#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

---------------------------------------------------

Inverted half pyramid of *

#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;
}

*****
****
***
**
*

-----------------------------------------------

Inverted half pyramid of numbers

#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() {

int rows, i, j, number = 1;

printf("Enter the number of rows: ");

scanf("%d", &rows);

for (i = 1; i <= rows; i++) {

for (j = 1; j <= i; ++j) {

printf("%d ", number);

++number;

printf("\n");
}

return 0;

23

456

7 8 9 10

----------------------------------

Inverted full pyramid of *

*********

*******

*****

***

#include <stdio.h>

int main() {

int rows, i, j, space;


printf("Enter the number of rows: ");

scanf("%d", &rows);

for (i = rows; i >= 1; --i) {

for (space = 0; space < rows - i; ++space)

printf(" ");

for (j = i; j <= 2 * i - 1; ++j)

printf("* ");

for (j = 0; j < i - 1; ++j)

printf("* ");

printf("\n");

return 0;

------------------------------------------------------------

Factorial of a Number

#include <stdio.h>

int main() {

int n, i;

unsigned long long fact = 1;

printf("Enter an integer: ");


scanf("%d", &n);

// shows error if the user enters a negative integer

if (n < 0)

printf("Error! Factorial of a negative number doesn't exist.");

else {

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

fact *= i;

printf("Factorial of %d = %llu", n, fact);

return 0;

-------------------------------------

Factorial of a Number Using Recursion

#include<stdio.h>

long int multiplyNumbers(int n);


int main() {

int n;

printf("Enter a positive integer: ");

scanf("%d",&n);

printf("Factorial of %d = %ld", n, multiplyNumbers(n));

return 0;

long int multiplyNumbers(int n) {

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.

The Fibonacci sequence: 0, 1, 1, 2, 3, 5, 8, 13, 21


Fibonacci Series up to n terms

#include <stdio.h>

int main() {

int i, n;

// initialize first and second terms

int t1 = 0, t2 = 1;

// initialize the next term (3rd term)

int nextTerm = t1 + t2;

// get no. of terms from user

printf("Enter the number of terms: ");

scanf("%d", &n);

// print the first two terms t1 and t2

printf("Fibonacci Series: %d, %d, ", t1, t2);


// print 3rd to nth terms

for (i = 3; i <= n; ++i) {

printf("%d, ", nextTerm);

t1 = t2;

t2 = nextTerm;

nextTerm = t1 + t2;

return 0;

Output

Enter the number of terms: 10

Fibonacci Series: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34,

------------------------------------------------

Fibonacci Sequence Up to a Certain Number

#include <stdio.h>

int main() {
int t1 = 0, t2 = 1, nextTerm = 0, n;

printf("Enter a positive number: ");

scanf("%d", &n);

// displays the first two terms which is always 0 and 1

printf("Fibonacci Series: %d, %d, ", t1, t2);

nextTerm = t1 + t2;

while (nextTerm <= n) {

printf("%d, ", nextTerm);

t1 = t2;

t2 = nextTerm;

nextTerm = t1 + t2;

return 0;

Output

Enter a positive integer: 100

Fibonacci Series: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89,


------------------------------------------

Program to Count the Number of Digits

#include <stdio.h>

int main() {

long long n;

int count = 0;

printf("Enter an integer: ");

scanf("%lld", &n);

// iterate at least once, then until n becomes 0

// remove last digit from n in each iteration

// increase count by 1 in each iteration

do {

n /= 10;

++count;

} while (n != 0);

printf("Number of digits: %d", count);

}
----------------------------------------

Reverse an Integer

#include <stdio.h>

int main() {

int n, rev = 0, remainder;

printf("Enter an integer: ");

scanf("%d", &n);

while (n != 0) {

remainder = n % 10;

rev = rev * 10 + remainder;

n /= 10;

printf("Reversed number = %d", rev);

return 0;

Output

Enter an integer: 2345

Reversed number = 5432


-------------------------------------

Program to Check Palindrome

#include <stdio.h>

int main() {

int n, reversed = 0, remainder, original;

printf("Enter an integer: ");

scanf("%d", &n);

original = n;

// reversed integer is stored in reversed variable

while (n != 0) {

remainder = n % 10;

reversed = reversed * 10 + remainder;

n /= 10;

// palindrome if orignal and reversed are equal

if (original == reversed)
printf("%d is a palindrome.", original);

else

printf("%d is not a palindrome.", original);

return 0;

Output

Enter an integer: 1001

1001 is a palindrome.

----------------------------------------

Program to Check Prime Number

#include <stdio.h>

int main() {

int n, i, flag = 0;

printf("Enter a positive integer: ");

scanf("%d", &n);

for (i = 2; i <= n / 2; ++i) {


// condition for non-prime

if (n % i == 0) {

flag = 1;

break;

if (n == 1) {

printf("1 is neither prime nor composite.");

else {

if (flag == 0)

printf("%d is a prime number.", n);

else

printf("%d is not a prime number.", n);

return 0;

---------------------------------------------------------
---------------------------------------------------------

Half-Pyramid Pattern: Type #1

#include <stdio.h>

void main() {

int rows, i, j;

printf("Enter Number Of Rows You Want: ");

scanf( "%d", &rows );

for(i = rows; i >= 1; i--) {

for(j = 1; j <= i; j++) {

printf("%d ", j);

printf("\n");

Output:

Enter Number Of Rows You Want: 6


123456

12345

1234

123

12

--------------------------------------

Half-Pyramid Pattern: Type #2

Same Loop as above, but printing (i)

#include <stdio.h>

void main() {

int rows, i, j;

printf("Enter Number Of Rows You Want: ");

scanf("%d", &rows);

for(i = rows; i >= 1; i--) {

for(j = 1; j <= i; j++) {

printf("%d ", i);


}

printf("\n");

Output:

Enter Number Of Rows You Want: 5

55555

4444

333

22

-----------------------------------------

Half-Pyramid Pattern: Type #3

#include <stdio.h>

void main(){
int rows, i, j;

printf("Enter Number Of Rows You Want: ");

scanf("%d", &rows);

for(i = rows; i>=1; i--){

for(j = i; j>=1; j--){

printf("%d ", j);

printf( "\n" );

Output:

Enter Number Of Rows You Want: 6

654321

54321

4321

321

21

1
--------------------------------------

Half-Pyramid Pattern: Type #4

#include <stdio.h>

void main() {

int rows, i, j;

printf("Enter Number Of Rows You Want: ");

scanf("%d", &rows);

for(i = 1; i <= rows; i++) {

for(j = rows; j >= i; j--) {

printf("%d ", j);

printf("\n");

Output:

Enter Number Of Rows You Want: 6


654321

65432

6543

654

65

---------------------------------

Half-Pyramid Pattern: Type #5

#include <stdio.h>

void main() {

int rows, i, j;

printf("Enter Number Of Rows You Want: ");

scanf("%d", &rows);

for(i=1; i<=rows; i++) {

for(j=i; j<=rows; j++) {

printf("%d ", j);


}

printf( "\n" );

Output:

Enter Number Of Rows You Want: 6

123456

23456

3456

456

56

--------------------------------

Half-Pyramid Pattern: Type #6

#include <stdio.h>

void main() {
int rows, i, j;

printf("Enter Number Of Rows You Want: ");

scanf("%d", &rows);

for(i=1; i<=rows; i++) {

for(j=1; j<=i; j++) {

printf("%d ", j);

printf("\n");

Output:

Enter Number Of Rows You Want: 6

12

123

1234

12345

123456
---------------------------------

Half-Pyramid Pattern: Type #7

#include <stdio.h>

void main(){

int rows, i, j;

printf("Enter Number Of Rows You Want: ");

scanf("%d", &rows);

for(i=rows; i>=1; i--) {

for(j=rows; j>=i; j--) {

printf("%d ", j);

printf("\n");

Output:

Enter Number Of Rows You Want: 6


6

65

654

6543

65432

654321

-----------------------------------

Half-Pyramid Pattern: Type #8

#include <stdio.h>

void main() {

int rows, i, j;

printf("Enter Number Of Rows You Want: ");

scanf("%d", &rows);

for(i=1; i<=rows; i++) {

for(j=1; j<=i; j++) {


printf("%d ", j);

printf("\n");

Output:

Enter Number Of Rows You Want: 5

12

123

1234

12345

---------------------------------

Half-Pyramid Pattern: Type #9

#include <stdio.h>
void main() {

int rows, i, j;

printf("Enter Number Of Rows You Want: ");

scanf("%d", &rows);

for(i=1; i<=rows; i++) {

for(j=1; j<=i; j++) {

printf("%d ", i);

printf("\n");

Output:

Enter Number Of Rows You Want: 5

22

333

4444

55555
-----------------------------------

Half-Pyramid Pattern: Type #10

#include <stdio.h>

void main() {

int rows, i, j;

printf("Enter Number Of Rows You Want: ");

scanf("%d", &rows);

for(i=1; i<=rows; i++) {

for(j=i; j<=rows; j++) {

printf("%d ", j);

printf("\n");

Output:
Enter Number Of Rows You Want: 5

12345

2345

345

45

------------------------------

Half-Pyramid Pattern: Type #11

#include <stdio.h>

void main(){

int rows, i, j;

printf("Enter Number Of Rows You Want: ");

scanf("%d", &rows);

for(i=rows; i>=1; i--) {

for(j=rows; j>=i; j--) {


printf("%d ", j);

printf("\n");

Output:

Enter Number Of Rows You Want: 5

54

543

5432

54321

-------------------------------------

Half-Pyramid Pattern: Type #12

#include <stdio.h>
void main() {

int rows, i, j;

printf("Enter Number Of Rows You Want: ");

scanf("%d", &rows);

for(i=rows; i>=1; i--) {

for(j=rows; j>=i; j--) {

printf("%d ", i);

printf("\n");

Output:

Enter Number Of Rows You Want: 6

55

444

3333

22222

111111
------------------------------

Half-Pyramid Pattern: Type #13

#include <stdio.h>

void main() {

int rows, i, j;

printf("Enter Number Of Rows You Want: ");

scanf("%d", &rows);

for(i=1; i<=rows; i++) {

for(j=i; j<=rows; j++) {

printf("%d ", i);

printf("\n");

Output:

Enter Number Of Rows You Want: 6


111111

22222

3333

444

55

------------------------------------

Java samples

public class Main

public static void main(String[] args) {

int i;

// initialize first and second terms

int t1 = 0, t2 = 1;
// initialize the next term (3rd term)

int nextTerm = t1 + t2;

// get no. of terms from user

int n =Integer.parseInt(args[0]);

//System.out.println(countPositive(new int[]));

// print the first two terms t1 and t2

System.out.print("Fibonacci Series: "+ t1 + " " + t2);

// print 3rd to nth terms

for (i = 3; i <= n; ++i) {

System.out.print(" "+ nextTerm + " ");

t1 = t2;

t2 = nextTerm;

nextTerm = t1 + t2;

Factorial of a Number
public class Main

public static void main(String[] args) {

int i;

long fact = 1;

// get the number during command line argument

int n =Integer.parseInt(args[0]);

// shows error if the user enters a negative integer

if (n < 0)

System.out.println("Error Factorial of a negative number doesn't exist.");

else {

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

fact *= i;

System.out.print("Factorial of " + n + " = " + fact);

}
}

Half-Pyramid Pattern: Type #1

public class Main

public static void main(String[] args) {

// get the rows during command line argument

int rows =Integer.parseInt(args[0]);

int i, j;

for(i = rows; i >= 1; i--) {

for(j = 1; j <= i; j++) {

System.out.print(" " + j);

System.out.println(" ");

Enter Number Of Rows You Want: 6


123456

12345

1234

123

12

public class Main

public static void main(String[] args) {

// get the rows during command line argument

int rows =Integer.parseInt(args[0]);

int i, j;

for(i=1; i<=rows; i++) {

for(j=1; j<=i; j++) {

System.out.print(" " + j);

System.out.println(" ");

}
}

public class Main

public static void main(String[] args) {

// get the rows during command line argument

int rows =Integer.parseInt(args[0]);

int i, j;

for(i=1; i<=rows; i++) {

for(j=1; j<=i; j++) {

System.out.print(" " + j);

System.out.println(" ");

System.out.println(" "+args[1]);

System.out.println(" "+args[2]);

You might also like