# 7.8 – loops – programs to practice Part 2

by on October 10, 2017

Specification: Accept an integer and print its factors

Show Logic

if n is divisible by i, then i is called the factor of n. Say for example 1, 2, 4 and 8 are the factors of 8.
If we want to find and print the factors of n then we generate 1 to n natural numbers using the loop

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

We check whether n is divisible by i for every iteration using the condition if(n%i==0). If the condition is true then i is printed otherwise skipped and continue for the next iteration.

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

Say for example if n is 4 then loop checks conditions as

```if(4%1==0)              //true
printf("\t%d",i);  //printed
if(4%2==0)              //true
printf("\t%d",i);  //printed
if(4%3==0)              //false
printf("\t%d",i);  //not printed
if(4%4==0)               //true
printf("\t%d",i);   //printed
```

Here conditions are true when i is 1 ,2 ,4 and false when i is 3. Hence 1, 2, 4 are printed as factors

Program:

```#include<stdio.h>
int main()
{
int n,i;
printf("Enter a number:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
if(n%i==0)
printf("%5d",i);
}
return 0;
}
```

Execution:
Enter a number:15
1    3    5   15

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

Show Logic

If the sum of all the factors other than the same number is equal to the given number then the number is called perfect number

Here we generate factors prior to the given number (i<n) using the loop

```for(i=i;i<n;i++)
if(n%i==0)
i
```

```for(sum=0,i=1;i<n;i++)
if(n%i==0)
sum=sum+i;
```

We check whether sum is equal to the given number or not. If equal then the given number is a perfect number otherwise not a perfect number.

```if(sum==n)
printf("Perfect number");
else
printf("Not a perfect number");
```

Program:

```#include<stdio.h>
int main()
{
int n,sum,i;
printf("Enter any integer:");
scanf("%d",&n);
for(sum=0,i=1;i<n;i++)  /* up to the last before */
{
if(n%i==0)
sum=sum+i;
}
if(sum==n)
printf("Perfect number");
else
printf("Not a perfect number");
return 0;
}
```

Execution 1:
Enter any integer:6
Perfect number

Execution 2:
Enter any integer:24
Not a perfect number

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

Show Logic

If any number has only two factors that are 1 and itself then the number is called a prime number
Here, we use a loop to find factors

```for(i=1;i<=n;i++)
if(n%i==0)
```

Now we need to count how many times do the condition if(n%i==0) gives true, which is the number of factors a given number has.
We take a variable count, which is initialized with 0 and incremented by 1 whenever the condition if(n%i==0) is true. So that, the count variable has number of factors.

```for(count=0,i=1;i<=n;i++)
if(n%i==0)
count++;
```

If the number of factors is 2 then the number is called prime number otherwise not called as a prime number

```if(count==2)
printf("Prime number");
else
printf("Not a prime number");
```

Program:

```#include<stdio.h>
int main()
{
int n,i,count;
printf("Enter any integer:");
scanf("%d",&n);
for(count=0,i=1;i<=n;i++)
{
if(n%i==0)
count++;
}
if(count==2)
printf("Prime number");
else
printf("Not a prime number");
return 0;
}
```

Execution 1:
Enter any integer:7
Prime number

Execution 2:
Enter any integer:25
Not a prime number

Specification: Accept any number and print the sum of all the digits.

Show Logic

The modulo division operator (%) return the last digit when we divide by 10;
n=n/10 removes the last digit. For example if n is 345, n/10 is 34.5 and assigning to integer variable “n” results 34 is stored into “n”.
It is only possible if “n” is of integer type.

Example 1:  if “n” is a three digit number.

Example 2:  if “n” is a four digit number.

Summery:
Extracting individual digits one by one using n%10, n=n/10 until “n” becomes 0

```while(n!=0)  /* loop runs as long as n is not zero */
{
n%10;
n=n/10;
}
```

Here, n%10 extracts digits one by one as the loop progresses, need to be added to the result variable sum in order to print the sum of individual digits.

```sum=0;
while(n!=0)
{
sum=sum+n%10;
n=n/10;
}
```

The same loop can be written using for iteration statement as

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

The same loop can be written using arithmetic assigning operators as

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

The same loop can be written using null statement as

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

Program:

```#include<stdio.h>
int main()
{
int n,sum;
printf("Enter any integer:");
scanf("%d",&n);
for(sum=0;n!=0;n=n/10)
sum=sum+n%10;
printf("Sum of all the digits %d",sum);
return 0;
}
```

Execution 1:
Enter any integer: 345
Sum of all the digits 12

Execution 2:
Enter any integer: 4536
Sum of all the digits 18

Specification: Accept a number and print whether the number is an Armstrong number or not.

Show Logic

if sum of cubes of individual digits is equal to the given number then the number is called an Armstrong number

Examples: 153, 370, 371, 407

We use the following loop to extract individual digits from the number. n would be zero on completion of loop. Hence we need to store n into a temporary variable to compare sum of cubes of individual digits with the given number.

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

We add cubes of individual digits to the result variable sum.

```for(temp=n,sum=0;n!=0;n=n/10)
sum=sum+pow(n%10,3);
```

We compare sum with the given number (temp), if both are equal then it is an Armstrong number

```if(sum==temp)
printf("Armstrong number");
else
printf("Not an Armstrong number");
```

Program:

```#include<stdio.h>
#include<stdio.h>
int main()
{
int n,sum,temp;
printf("Enter a number:");
scanf("%d",&n);
temp=n; /* preserving original number */
for(sum=0;n!=0;n=n/10)
sum=sum+pow(n%10,3);
if(sum==temp)
printf("Armstrong number");
else
printf("Not an armstrong number");
return 0;
}
```

Execution 1:
Enter a number:153
Armstrong number

Execution 2:
Enter a number:768
Not an Armstrong number

Specification: Accept a number and print it in reverse.

Show Logic

Individual digits can be extracted using the loop

```while(n!=0)
{
n%10
n=n/10;
}
```

Individual digits extracted by the loop must be appended to a result variable rev. rev=rev*10+n%10 appends digits to the variable rev.

```rev=0;
while(n!=0)
{
rev=rev*10+n%10;
n=n/10;
}
```

The same loop can be written using for iteration statement as

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

The same loop can be written using null statement as

```for(rev=0;n!=0;rev=rev*10+n%10,n/=10);
```

Program:

```#include<stdio.h>
int main()
{
int n,rev;
printf("Enter an integer:");
scanf("%d",&n);
for(rev=0;n!=0;n=n/10)
rev=rev*10+n%10;
printf("The reverse number %d",rev);
return 0;
}
```

Execution:
Enter an integer:5647
The reverse number 7465

Previous post: