# 7.14 – Nested loops – programs to practice part-3

by on October 21, 2017

Specification:  Accept any number and print whether the number is a strong number or not.

Show Logic

If the sum of factorials of individual digits is equal to the given number then the number is called a strong number.

Example:

We use the following loop to extract individual digits

```for( ;n!=0;n=n/10)
n%10
```

As n would be zero at the end of loop, we preserve the value of n into temp variable to compare with the sum of factorials later.

```temp=n;
for( ;n!=0;n=n/10)
n%10
```

factorials of individual digits are calculated and added to sum

```temp=n;
for(sum=0;n!=0;n=n/10)
{
for(fact=1,i=1;i<=n%10;i++)
fact=fact*i;
sum=sum+fact;
}
```

if the accepted value (temp) is equal to the sum of factorials of individual digits then it is called strong number

```if(sum==temp)
printf("Strong number");
else
printf("Not a strong number");
```

Program:

```#include<stdio.h>
int main()
{
int i,fact,sum,temp,n;
printf("Enter an integer:");
scanf("%d",&n);
temp=n;
for(sum=0;n!=0;n=n/10)
{
for(fact=1,i=1;i<=n%10;i++)
fact=fact*i;
sum=sum+fact;
}
if(sum==temp)
printf("Strong number");
else
printf("Not a strong number");
return 0;
}
```

Execution:
Enter an integer: 145
Strong number

Specification: Accept the limit and print the strong numbers from 1 to the given limit.

Program:

```#include<stdio.h>
int main()
{
int i,fact,sum,temp,n,x,limit;
printf("Enter the limit:");
scanf("%d",&limit);
for(x=1;x<=limit;x++)
{
n=x;
for(sum=0;n!=0;n=n/10)
{
for(fact=1,i=1;i<=n%10;i++)
fact=fact*i;
sum=sum+fact;
}
if(sum==x)
printf("%5d",x);
}
return 0;
}
```

Execution:
Enter the limit: 1000
1        2        145

Example explained:
Using outer loop we supply numbers from 1 to the specified limit one by one, if the supplied number (n) is a strong number then it would be printed otherwise will try for the next number

Specification: Print the Floyd’s triangle up to ‘n’ rows.

Show Logic

Printing the natural numbers from 1 as a number triangle is called Floyd’s triangle

To print the terms as a number triangle, we use the loop

```for(j=1;j<=n;j++)    /* to print n rows */
for(i=1;i<=j;i++)
---------
```

Rather printing i or j, we print natural numbers using another variable x. x must be started from 1 and must increment by 1 for every iteration.

```x=1;
for(j=1;j<=n;j++)
{
for(i=1;i<=j;i++)
printf("%5d",x++);  /* printing and then incrementing by 1 */
printf("\n\n");
}
```

Program:

```#include<stdio.h>
int main()
{
int i,j,x,n;
printf("How many rows?");
scanf("%d",&n);
x=1;
for(j=1;j<=n;j++)
{
for(i=1;i<=j;i++)
printf("%5d",x++);
printf("\n\n");
}
return 0;
}
```

Execution:
How many rows? 4

1

2     3

4     5    6

7     8    9     10

Specification: Print the following output using a C program

Show Logic

We need to print n rows using the loop

```for(r=1;r<=n;r++)
-------------------
```

Spaces must be printed using the formula n-r. Say for example for 1st row 4-1 that is 3 spaces must be printed, for 2nd row 4-2 that is 2 spaces must be printed and for 3rd row 4-3 that is a single space must be printed.

```for(r=1;r<=n;r++)
{
for(s=1;s<=n-r;s++)
printf("  "); /* 2 spaces */
-----------------
}
```

In each row r terms have to be printed that is 1 term has to print in 1st row, 2 terms have to print in 2nd row and 4 terms have to print in 4th row.
Arithmetic progression is printed with difference of 1 from 1 as terms ( 1  2  3  4  ……)

```x=1;
for(r=1;r<=n;r++)
{
for(s=1;s<=n-r;s++)       /* printing spaces */
printf("  ");       /* two spaces */
for(t=1;t<=r;t++)         /* printing row */
printf("%4d",x++);  /* printing terms */
printf("\n\n");
}
```

Program:

```#include<stdio.h>
int main()
{
int n,r,t,s,x;
printf("How many rows:");
scanf("%d",&n);
x=1;
for(r=1;r<=n;r++)
{
for(s=1;s<=n-r;s++)     /* printing spaces */
printf("  ");      /* two spaces */
for(t=1;t<=r;t++)       /* printing row */
printf("%4d",x++); /* printing terms */
printf("\n\n");
}
return 0;
}
```

Specification: Print Pascal triangle up to the required number of rows

Show Logic

Pascal’s triangle is a triangular array of the binomial coefficients. To build the triangle, start with “1” at the top and then continue placing numbers below it in a triangular pattern.
Each number is just the two numbers above it added together (except for the edges, which are all “1”).

Logic:
We need to print n rows that is from 0th row to n-1th row

```for(r=0;r<=n-1;r++)
------------------
```

we have to print (n-r)-1 spaces for every row. Say for example, for 0th row spaces should be (5-0)-1 that is 4 spaces, for 1st row spaces must be (5-1)-1 that is 3 spaces must be printed

```for(r=0;r<=n-1;r++)
{
for(s=1;s<=(n-r)-1;s++)
printf("  ");              /* 2 spaces */
----------
}
```

We need to print binomial coefficients for every row. Every row starts from coefficient 1 and other terms are calculated using (c*(r-t)/(t+1)) where c is the previous term, r is the row number and t is the term number

```for(r=0;r<=n-1;r++)
{
for(s=1;s<=(n-r)-1;s++)
printf("  ");              /* 2 spaces */
c=1;
for(t=0;t<=r;t++)         /* printing r+1 terms */
{
printf("%3d",c);
c=(c*(r-t)/(t+1));
}
printf("\n\n");
}
```

Program:

```#include<stdio.h>
int main()
{
int n,r,t,s,c;
printf("Enter the limit ");
scanf("%d",&n);
printf("\n");
for(r=0;r<=n-1;r++)
{
for(s=1;s<=(n-r)-1;s++)
printf("  ");              /* 2 spaces */
c=1;
for(t=0;t<=r;t++)         /* printing r+1 terms */
{
printf("%4d",c);
c=(c*(r-t)/(t+1));
}
printf("\n\n");
}
return 0;
}
```

Specification: Accept any number and print the prime factors.

Show Logic

Prime factors are the factors of a number which are of prime type.

Say for example, factors of 6 are 1   2    3    6, Out of these factors 2 and 3 are prime numbers. Hence, prime factors of 6 are 2 and 3

Logic to find factors is

```for(x=1;x<=n;x++)
if(n%x==0)
{
---------
}
```

Logic to find prime or not is

```for(count=0,i=1;i<=x;i++)
if(x%i==0)
count++;
if(count==2)
------------
```

Putting both together

Program:

```#include<stdio.h>
int main()
{
int n,x,count,i;
printf("Enter an integer:");
scanf("%d",&n);
for(x=1;x<=n;x++)
if(n%x==0)             /* if x is the factor of n */
{
for(count=0,i=1;i<=x;i++)
if(x%i==0)                /* counting the factors of x */
count++;
if(count==2)
printf("\t%d",x);
}
return 0;
}
```

Execution 1:
Enter an integer:6
2     3

Execution 2:
Enter an integer:60
2       3       5

Specification: Accept the beginning, common difference and number of terms and print the Arithmetic progression and arithmetic series

Show Logic

In a series of numbers, if the difference among any two consecutive numbers is the same then it is called arithmetic progression and the sum of arithmetic progression is called arithmetic series.

5        10      15      20

Here in the list the difference among any two consecutive numbers is 5

Logic:
Let us take the first term as ‘a’, difference as ‘d’ and the number of terms as ‘n’
The first term in the series must be started from a.

```t=a;
```

The value of term must be incremented by d for every iteration

```t=t+d;
```

Loop must run for n number of times to generate n terms

```t=a;
for(i=1;i<=n;i++)
{
printf("%5d",t);
t=t+d;
}
```

To get arithmetic series, t must be added to another variable sum for every iteration

```sum=0;
for(i=1;i<=n;i++)
{
printf("%5d",t);
sum=sum+t;
t=t+d;
}
```

The same loop can be written as

```for(sum=0,t=a,i=1;i<=n;i++,sum+=t,t+=d)
printf("%5d",t);
```

Program:

```#include<stdio.h>
int main()
{
int a,d,n,t,sum,i;
printf("Enter the first term:");
scanf("%d",&a);
printf("Enter the difference:");
scanf("%d",&d);
printf("Enter the number of terms:");
scanf("%d",&n);
printf("Arithmetic progression:\n");
sum=0;
t=a;
for(i=1;i<=n;i++)
{
printf("%5d",t);
sum=sum+t;
t=t+d;
}
printf("\nArithmetic series  %d",sum);
return 0;
}
```

Execution:
Enter the first term:2
Enter the difference:3
Enter the number of terms:5
Arithmetic progression:
2    5    8   11   14
Arithmetic series  40

Specification: Accept the beginning, common difference and number of terms and print the Geometric progression and Geometric series

Show Logic

In a series of numbers, if the common ratio of any two consecutive numbers is same then it is called geometric progression. The sum of all the terms of geometric progression is called geometric series.

2        4        8        16      32, …

In the above progression the common ration among any two consecutive terms is 2

10      5       2.5    1.25, …

In the above progression the common ratio among any two consecutive terms is 0.5

Logic:

In arithmetic progression, we have incremented t by common difference d for every iteration. Where as here we have to multiply t by common ratio r for every iteration

```sum=0;
t=a;
for(i=1;i<=n;i++)
{
printf("%5d",t);
sum=sum+t;
t=t*r;
}
```

Program:

```#include<stdio.h>
int main()
{
int a,r,n,t,sum,i;
printf("Enter the first term:");
scanf("%d",&a);
printf("Enter the common ratio:");
scanf("%d",&r);
printf("Enter the number of terms:");
scanf("%d",&n);
printf("Geometric progression:\n");
sum=0;
t=a;
for(i=1;i<=n;i++)
{
printf("%5d",t);
sum=sum+t;
t=t*r;
}
printf("\nGeometric series  %d",sum);
return 0;
}
```

Execution:
Enter the first term:2
Enter the common ratio:2
Enter the number of terms:5
Geometric progression:
2    4    8   16   32
Geometric series  62

Specification: Calculate and print the sum of following series

Program:

```#include<stdio.h>
int main()
{
int n,x,i;
long int fact;
double sum;
printf("How many terms?");
scanf("%d",&n);
sum=0;
for(x=1;x<=n;x++)            /* generating terms (x) from 1 to n  */
{
for(fact=1,i=1;i<=x;i++)              /* calculating x!    */
fact=fact*i;
sum=sum+(double)x/fact;        /*   summing terms x/x!  */
}
printf("Value of series %Lf",sum);
return 0;
}
```

Execution 1:
How many terms? 2
Value of series 2.000000

Execution 2:
How many terms? 5
Value of series 2.708333

Previous post:

Next post: