Programming in C: Unit I (c): Decision Control and Looping Statements

Nested Loops

with Example C Programs

C allows its users to have nested loops, i.e., loops that can be placed inside other loops. Although this feature will work with any loop like while, do-while, and for, it is most commonly used with the for loop, because this is easiest to control.

NESTED LOOPS

C allows its users to have nested loops, i.e., loops that can be placed inside other loops. Although this feature will work with any loop like while, do-while, and for, it is most commonly used with the for loop, because this is easiest to control. A for loop can be used to control the number of times that a particular set of statements will be executed. Another outer loop could be used to control the number of times that a whole loop is repeated.

In C, loops can be nested to any desired level. However, loops should be properly indented in order to enable the reader to easily determine which statements are contained within each for statement. To see the benefit of nesting loops, we will see some programs that exhibit the use of nested loops.

31. Write a program to print the following pattern.

Pass 1- 12345

Pass 2-12345

Pass 3-12345

Pass 4-12345

Pass 5-12345

#include <stdio.h>

int main()

{

int i, j;

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

{

printf("\n Pass %d- ",i);

for(j=1;j<=5;j++)

printf("%d", j);

}

return 0;

}

32. Write a program to print the following pattern.

**********

**********

**********

**********

**********

#include <stdio.h>

int main()

{

int i, j;

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

{

printf("\n");

for (j=1;j<=10;j++)

printf("*");

}

return 0;

}

33. Write a program to print the following pattern.

*

**

***

****

*****

#include <stdio.h>

int main()

{

int i, j;

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

{

printf("\n");

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

printf("*");

}

return 0;

}

34. Write a program to print the following pattern.

1

12

123

1234

12345

#include <stdio.h>

int main()

{

int i, j;

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

{

printf("\n");

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

printf("%d", j);

}

return 0;

}

35. Write a program to print the following pattern.

1

22

333

4444

55555

#include <stdio.h>

int main()

{

int i, j;

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

{

printf("\n");

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

printf("%d", i);

}

return 0;

}

36. Write a program to print the following pattern.

0

12

345

6789

#include <stdio.h>

int main()

{

int i, j, count=0;

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

{

printf("\n");

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

printf("%d", count++);

}

return 0;

}

37. Write a program to print the following pattern.

A

AB

ABC

ABCD

ABCDE

ABCDEF

#include <stdio.h>

int main()

{

char i, j;

for(i=65;i<=70;i++)

{

printf("\n");

for(j=65;j<=i;j++)

printf("%c", j);

}

return 0;

}

38. Write a program to print the following pattern.

1

12

123

1234

12345

#include <stdio.h>

#define N 5

int main()

{

int i, j, k;

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

for (k=N; k>=i;k--)

{

printf(" ");

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

printf("%d", j);

printf("\n");

}

return 0;

}

39. Write a program to print the following pattern.

1

121

12321

1234321

123454321

#include <stdio.h>

#define N 5

int main()

{

int i, j, k, l;

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

{

for (k=N; k>=i;k--)

printf(" ");

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

printf("%d", j);

for (1-j-2;1>0;1--)

printf("%d", 1);

printf("\n");

}

return 0;

}

40. Write a program to print the following pattern.

1

22

333

4444

55555

#include <stdio.h>

#define N 5

int main()

{

int i, j, k, count=5, c;

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

{

for (k=1;k<=count; k++)

printf(" ");

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

printf("%2d", i);

printf("\n");

C--;

}

return 0;

}

41. Write a program to print the multiplication table of n, where n is entered by the user.

#include <stdio.h>

int main()

{

int n, i;

printf("\n Enter any number: ");

scanf("%d", &n);

printf("\n Multiplication table of %d", n);

printf("\n ****************") ;

 for (i=0;i<=20;i++)

printf("\n %d X %d = %d", n, i, (n* i));

return 0;

}

Output

Enter any number: 2

Multiplication table of 2

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

2 X 0 = 0

2 X 1 = 2

………

2 X 20 = 40

42. Write a program using for loop to print all the numbers from m to n, thereby classifying them as even or odd

#include <stdio.h>

#include <conio.h>

int main()

{

int i, m, n;

clrscr();

printf("\n Enter the value of m: ");

scanf("%d", &m);

printf("\n Enter the value n: ");

scanf("%d", &n);

for (i=m; i<=n;i++)

if (i%2 = = 0)

{

printf("\n %d is even",i);

else

printf("\n %d is odd", i);

}

return 0;

}

Output

Enter the value of m: 5

Enter the value of n: 7

5 is odd

6 is even

7 is odd

43. Write a program using for loop to calculate the average of first n natural numbers.

#include <stdio.h>

#include <conio.h>

int main()

{

int n, i, sum =0;

float avg = 0.0;

clrscr();

printf("\n Enter the value of n: ");

scanf("%d", &n);

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

sum = sum + i;

avg = (float) sum/n;

printf("\n The sum of first n natural numbers %d", sum);

printf("\n The average of first n natural numbers %.2f", avg);

return 0;

}

Output

Enter the value of n: 10

The sum of first n natural numbers 55

The average of first n natural numbers = 5.50

44. Write a program using for loop to calculate factorial of a number.

#include <stdio.h>

#include <conio.h>

int main()

{

int fact = 1, num;

clrscr();

printf("\n Enter the number: ");

scanf("%d", &num);

if (num = = 0)

fact = 1;

else

{

for (int i=1; i<=num; i++)

fact fact* i;

}

printf("\n Factorial of %d is: %d ", num, fact);

return 0;

}

Output

Enter the number: 5

Factorial of 5 is: 120                  

45. Write a program to classify a given number as prime or composite.

#include <stdio.h>

#include <conio.h>

int main()

{

int flag = 0, i, num;

clrscr();

printf("\n\n Enter any number: ");

scanf("%d", &num);

for (i=2;i<=num/2; i++)

{

if (num%i==0)

{

flag = 1;

break;

}

}

if (flag = = 1)

printf("\n %d is a composite number", num);

else

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

return 0;

}

Output

Enter the number: 5

5 is a prime number

46. Write a program using do-while loop to read the numbers until -1 is encountered. Also count the number of prime numbers and composite numbers entered by the user

#include <stdio.h>

#include <conio.h>

int main()

{

int num, i;

int primes=0, composites=0, flag=0;

clrscr();

printf("\n Enter -1 to exit");

printf("\n\n Enter any number: ");

scanf("%d", &num);

do

{

for (i=2; i<num/2; i++)

{

if (num%i= =0)

{

flag =1;

break;

}

}

if (flag= =0)

primes++;

else

composites++;

flag=0;

printf("\n Enter any number: ");

scanf("%d", &num);

} while (num != -1);

printf("\n Count of prime numbers entered = %d", primes);

printf("\n Count of composite numbers entered = %d", composites);

return 0;

}

Output

Enter -1 to exit

Enter any number: 5

Enter any number: 10

Enter any number: 7

Enter any number: -1

Count of prime numbers entered = 2

Count of composite numbers entered = 1

47. Write a program to calculate pow(x,n) i.e., to calculate xn.

#include <stdio.h>

#include <conio.h>

#include <math.h>

int main()

{

int i, num, n;

long int result =1;

clrscr();

printf("\n Enter the number: ");

scanf("%d", &num);

printf("\n Till which power to calculate: ");

scanf("%d", &n);

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

result = result * num;

printf("\n pow(%d, %d) = %ld", num, n, result);

return 0;

}

Output

Enter the number: 123

Till which power to calculate: 5

pow (2, 5) = 32

48. Write a program to print the reverse of a number.

#include <stdio.h>

#include <conio.h>

int main()

{

int num, temp;

clrscr();

printf("\n Enter the number: ");

scanf("%d", &num);

printf("\n The reversed number is: ");

while (num != 0)

{

temp = num%10;

printf("%d", temp);

num = num/10;

}

return 0;

}

Output

Enter the number: 123

The reversed number is: 321

49. Write a program to enter a number and then calculate the sum of its digits.

#include <stdio.h>

#include <conio.h>

int main()

int num, temp, sumofdigits = 0;

clrscr();

printf("\n Enter the number: ");

scanf("%d", &num);

while (num != 0)

{

temp = num%10;

sumofdigits += temp;

num = num/10;

}

printf("\n The sum of digits = %d", sumofdigits);

return 0;

}

Output

Enter the number: 123

The sum of digits = 6

50. Write a program to enter a decimal number. Calculate and display the binary equivalent of this number.

#include <stdio.h>

#include <conio.h>

#include <math.h>

int main()

{

int decimal_num, remainder, binary_num = 0, i = 0;

clrscr();

printf("\n Enter the decimal number: ");

scanf("%d", &decimal_num);

while (decimal_num != 0)

{

remainder = decimal_num%2;

binary_num += remainder*pow (10, i);

decimal_num = decimal_num/2;

i++;

}

printf("\n The binary equivalent = %d", binary_num);

return 0;

}

Output

Enter the decimal number: 7

The binary equivalent = 111

51. Write a program to enter a decimal number. Calculate and display the octal equivalent of this number

#include <stdio.h>

#include <conio.h>

#include <math.h>

int main()

{

int decimal_num, remainder, octal_num = 0, i = 0;

clrscr();

printf("\n Enter the decimal number: ");

scanf("%d", &decimal_num);

while (decimal_num != 0)

{

remainder = decimal_num%8;

octal_num += remainder*pow (10, i);

decimal_num = decimal_num/8;

i++;

}

printf("\n The octal equivalent = %d", octal_num);

return 0;

}

Output

Enter the decimal number: 18

The octal equivalent = 22

52. Write a program to enter a binary number. Calculate and display the decimal equivalent of this number.

#include <stdio.h>

#include <conio.h>

#include <math.h>

int main()

{

int decimal_num = 0, remainder, binary_num, i = 0;

clrscr();

printf("\n Enter the binary number: ");

scanf("%d", &binary_num);

while (binary_num != 0)

{

remainder = binary_num%10;

decimal_num += remainder*pow (2, i);

binary_num = binary_num/10;

i++;

}

printf("\n The decimal equivalent = %d", decimal_num);

return 0;

}

Output

Enter the binary number : 111

The decimal equivalent = 7

53. Write a program to enter an octal number. Calculate and display the decimal equivalent of this number.

#include <stdio.h>

#include <conio.h>

#include <math.h>

int main()

{

int decimal_num =0, remainder, octal_num, i = 0;

clrscr();

printf("\n Enter the octal number: ");

scanf("%d", &octal_num);

while (octal_num != 0)

{

remainder = octal_num%10;

decimal_num += remainder*pow (8, i);

octal_num = octal_num/10;

i++;

}

printf("\n The decimal equivalent %d", = decimal_num);

return 0;

}

Output

Enter the octal number: 22

The decimal equivalent = 18

54. Write a program to enter a hexadecimal number.Calculate and display the decimal equivalent of this number.

#include <stdio.h>

#include <conio.h>

#include <math.h>

int main()

{

int decimal_num= 0, remainder, hex_num, i=0;

clrscr();

printf("\n Enter the hexadecimal number: ");

scanf("%d", &hex_num);

while (hex_num != 0)

{

remainder = hex_num%10;

decimal_num += remainder*pow (16, i);

hex_num = hex_num/10;

i++;

}

printf("\n The decimal equivalent = %d", decimal_num);

return 0;

}

Output

Enter the hexadecimal number: 39

The decimal equivalent = 57

 

55. Write a program to calculate GCD of two numbers.

#include <stdio.h>

#include <conio.h>

int main()

{

int num1, num2, temp;

int dividend, divisor, remainder;

clrscr();

printf("\n Enter the first number: ");

scanf("%d", &num1);

printf("\n Enter the second number: ");

scanf("%d", &num2);

if (num1>num2)

{

dividend = num1;

divisor = num2;

}

else

{

dividend = num2;

divisor = num1;

}

while (divisor)

{

remainder = dividend%divisor;

dividend = divisor;

divisor = remainder;

}

printf("\n GCD of %d and %d is = %d", num1,num2, dividend);

return 0;

}

Output

Enter the first number: 64

Enter the second number: 14

GCD of 64 and 14 is = 2

56. Write a program to sum the series 1+ 1/2 + 1/3 +……..+ 1/n.

#include <stdio.h>

#include <conio.h>

int main()

{

int n;

float sum=0.0, a, i;

clrscr();

printf("\n Enter the value of n: ");

scanf("%d", &n);

for (i=1.0;i<=n;i++)

{

a=1/i;

sum = sum +a;

}

printf("\n The sum of series 1/1 + 1/2 +………+ 1/%d == %f",n, sum);

 return 0;

}

Output

Enter the value of n: 5

The sum of series 1/1 + 1/2 +……….+ 1/5 = 2.2838

57. Write a program to sum the series 1/12 + 1/22 +……….+1/32.

#include <stdio.h>

#include <math.h>

#include <conio.h>

int main()

{

int n;

float sum=0.0, a,i;

clrscr();

printf("\n Enter the value of n: ");

scanf("%d", &n);

for (i=1.0;i<=n;i++)

{

a=1/pow(1, 2);

sum sum +a;

}

printf("\n The sum of series 1/12 + 1/ 22 + ……..1/%d2 = %f", n, sum);

return 0;

}

Output

Enter the value of n: 5

The sum of series 1/12 + 1/ 22 +………1/52 = 1.4636

58. Write a program to sum the series1/2 + 2/3 …….+ n/(n+1).

#include <stdio.h>

#include <conio.h>

int main()

{

int n;

float sum=0.0, a, i;

clrscr();

printf("\n Enter the value of n: ");

scanf("%d", &n);

for (i=1.0;i<=n;i++)

{

a= i/ (i+1);

sum = sum +a;

}

printf("\n The sum of series 1/2 + 2/3 +………. %d/%d = %f",n,n+1, sum);

return 0;

}

Output

Enter the value of n :5

The sum of series 1/2 + 2/3 + ....5/6 = 2.681+E

59. Write a program to sum the series 1/1 + 22/2 + 33/3 +……….

#include <stdio.h>

#include <conio.h>

#include <math.h>

int main()

{

int n, NUM;

float i, sum=0.0;

clrscr();

printf("\n Enter the value of n: ");

scanf("%d", &n);

for (i=1.0;i<=n;i++)

{

NUM = pow (i,i);

sum += (float) NUM/i;

}

printf("\n 1/1 + 4/2 + 27/3 +……….. = %f",sum);

return 0;

}

Output

Enter the value of n:5

1/1 + 4/2 + 27/3 + ………= 701.000

60. Write a program to calculate sum of cubes of first n numbers.

#include <stdio.h>

#include <conio.h>

#include <math.h>

int main()

Programming Tip: It is a logical error to skip the updating of loop control variable in the while/do-while loop. Without an update statement, the loop will become an infinite loop.

{

int i, n;

int term, sum = 0;

clrscr();

printf("\n Enter the value of n: ");

scanf("%d", &n);

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

{

term = pow(i,3);

sum += term;

}

printf("\n 13 + 23 + 33 +………= %d", sum);

return 0;

}

Output

Enter the value of n:5

13 + 23 + 33 +……. = 225

61. Write a program to calculate sum of squares of first n even numbers.

#include <stdio.h>

#include <conio.h>

#include <math.h>

int main()

int i, n;

int term, sum=0;

clrscr() ;

printf("\n Enter the value of n: ");

scanf("%d", &n);

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

{

if (i%2= =0)

{

term = pow(i, 2);

sum += term;

}

}

printf("\n 22 + 42 + 62 +………= %d", sum);

return 0;

}

Output

Enter the value of n: 5

22 + 42 + 62 +……..= 20

62. Write a program to find whether the given number is an armstrong number or not.

#include <stdio.h>

#include <conio.h>

#include <math.h>

int main()

{

int num, sum=0, r, n;

clrscr();

printf("\n Enter the number: ");

scanf("%d", &num);

n=num;

while (n>0)

r = n%10;

sum += pow (r,3);

n = n/10;

}

if (sum = = num)

printf("\n %d is an armstrong number", num);

else

printf("\n %d is not an armstrong number", num);

return 0;

}

Output

Enter the number : 432

432 is not an armstrong number

63. Write a program using for loop to calculate the value of an investment, given the initial value of investment and the annual interest. Calculate the value of investment over a period of time.

#include <stdio.h>

int main()

{

double initVal, futureVal, ROI;

int yrs, i;

printf("\n Enter the investment value: ");

scanf("%lf", &initVal);

printf("\n Enter the rate of interest: ");

scanf("%lf", &ROI);

printf("\n Enter the number of years for which investment has to be done: "); scanf("%d", &yrs);

futureVal=initVal;

printf("\n YEAR \t\t VALUE");

printf("\n _______________________");

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

{

futureVal = futureVal* (1 + ROI/100.0);

printf("\n %d \t %lf", i, futureVal);

}

return 0;

}

Output

Enter the investment value: 20000

Enter the rate of interest: 12

Enter the number of years for which investment has to be done: 5

YEAR  VALUE

_________________

1  22400.00

2  25088.00

3  28098.56

4  31470.38

5  35246.83

64. Write a program to generate calendar of a month given the start day of the week and the number of days in that month.

#include <stdio.h>

int main()

{

int i, j, startDay, num_of_days;

printf("\n Enter the starting day of the week (1 to 7): ");

scanf("%d", &startDay);

printf("\n ENter the number of days in that month: ");

scanf("%d", &num_of_days);

printf(" Sun Mon Tue Wed Thurs Fri Sat\n");

printf("\n________________________");

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

printf(" ");

for(j=1;j<=num_of_days; j++)

{

if (i>6)

{

printf("\n");

i=1;

}

else

i++;

printf("%2d ", j);

}

return 0;

}

Output

Enter the starting day of the week (1 to 7) : 5

Enter the number of days in that month : 31

Sun Mon Tue Wed Thurs Fri Sat

1  2  3  4  5  6  7

Programming in C: Unit I (c): Decision Control and Looping Statements : Tag: : with Example C Programs - Nested Loops