# 7.9 – loops – programs to practice part-3

by on October 11, 2017

### Binary, Octal to Decimal conversion:

Specification: Accept any binary number and print its equivalent decimal number

Show Logic

We use the logic n!=0, n=n/10, n%10 to extract individual bits from a binary number.
We use the logic i=0, i++ to calculate 2i
Here we use pow(2,i) function to calculate 2i

```i=0;
sum=0;
while(n!=0)
{
sum=sum+n%10*pow(2,i);
n=n/10;
i++;
}
```

The same loop can be written using for iteration statement as

```for(sum=0,i=0;n!=0;n=n/10,i++)
sum=sum+n%10*pow(2,i);
```

Program:

```#include<stdio.h>
#include<math.h>
int main()
{
long n;
int sum,i;
printf("Enter the binary number:");
scanf("%ld",&n);
for(sum=0,i=0;n!=0;n=n/10,i++)
sum=sum+n%10*pow(2,i);
printf("Decimal equal %d",sum);
return 0;
}
```

Execution:
Enter the binary number:10111
Decimal equal 23

Specification: Accept any octal number and print its equivalent decimal

Show Logic

Program:

```#include<stdio.h>
#include<math.h>
int main()
{
long n;
int sum,i;
printf("Enter the octal number:");
scanf("%ld",&n);
for(sum=0,i=0;n!=0;n=n/10,i++)
sum=sum+n%10*pow(8,i);
printf("Decimal equal %d",sum);
return 0;
}
```

Execution:
Enter the octal number:2435
Decimal equal 1309

### Decimal to binary and octal conversion:

Specification: Accept any decimal number and print its equivalent binary number

Show Logic

To convert a decimal number into binary equal, we need to keep on divide the number by 2 and record all the remainders. Writing all the remainders in reverse order results binary equal

Here, remainders are generated using the loop

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

These remainders are appended, that is arranged in reverse order using

```for(sum=0;n!=0;n=n/2)
sum=sum+(n%2)*pow(10,i);
```

Program:

```#include<stdio.h>
#include<math.h>
int main()
{
int n,i;
long sum;
printf("Enter any integer:");
scanf("%d",&n);
for(sum=0,i=0;n!=0;n=n/2,i++)
sum=sum+(n%2)*pow(10,i);
printf("Binary equal %ld",sum);
return 0;
}
```

Execution:
Enter any integer: 91
Binary equal 1011011

Specification: Accept any decimal and print its equivalent octal number

```#include<stdio.h>
#include<math.h>
int main()
{
int n,i,sum;
printf("Enter any integer:");
scanf("%d",&n);
for(sum=0,i=0;n!=0;n=n/8,i++)
sum=sum+(n%8)*pow(10,i);
printf("Octal equal %d",sum);
return 0;
}
```

Execution:
Enter any integer:1309
Octal equal 2435

Specification: Print the alphabets and their ASCII values.

Show Logic

Any character constant is stored in a form of binary in the computer memory, decimal equivalent to this binary is called ASCII value.
While printing char type, if we use %c as format specifier then equivalent character is printed, if we use %d as format specifier then equivalent ASCII value is printed.

```char ch='a';            /* Assigns with 01100001 */
printf("%c",ch);      /* a is printed   */
printf("\n%d",ch);   /* 97 (ASCII) is printed */
```

We use a loop for(ch=’A’;ch<=’Z’;ch++) to generate A to Z alphabets and for(ch=’a’;ch<=’z’;ch++) to generate a to z alphabets. We use %c to print alphabet and %d to print ASCII value. We put altogether as

```for(ch='A';ch<='Z';ch++)     /* prints A to Z with their ASCII values */
printf("\n%c\t%d",ch,ch);

for(ch='a';ch<='z';ch++)     /* prints a to z with their ASCII values  */
printf("\n%c\t%d",ch,ch);
```

Program:

```#include<stdio.h>
int main()
{
char ch;
printf("ASCII table of capital alphabets\n");
printf("-----------------------------------\n");
for(ch='A';ch<='Z';ch++)
printf("\n%c\t%d",ch,ch);
printf("ASCII table of small case alphabets\n");
printf("---------------------------------------\n");
for(ch='a';ch<='z';ch++)
printf("\n%c\t%d",ch,ch);
return 0;
}
```

Specification: Accept an integer and print how many digits does the given number has?

Show Logic

We have been using a common logic to extract individual digits from a number, that is

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

The loop iterates three times if the number is a three digit number, loop iterates four times if the number is a four digit number.
Hence, we take a counter variable and increment it by 1 for every iteration. It gives the number of digits the given number has

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

The same loop can be written using null statement as

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

Program:

```#include<stdio.h>
int main()
{
long n;
int count;
printf("Enter any integer:");
scanf("%ld",&n);
for(count=0;n!=0;n=n/10)
count++;
printf("Number has %d digits",count);
return 0;
}
```

Execution 1:
Enter any integer:76487
Number has 5 digits

Execution 2:
Enter any integer:-234
Number has 3 digits

Specification: Accept numbers until 0 and print the biggest number (without using array)

Show Logic

We assume that the first number is the biggest number

```scanf("%d",&n);
big=n;
```

We accept numbers one by one until 0 using a loop

```while(n!=0)
{
scanf("%d",&n);

}
```

We compare the new number with biggest number every time a new number is accepted. If the new number is greater than the biggest number then we assign the new number to the biggest number otherwise continue for the next iteration

```while(n!=0)
{
scanf("%d",&n);
if(n>big)
big=n;
}
```

At the end of loop, the big variable will have the biggest number accepted so far.

Program:

```#include<stdio.h>
int main()
{
int n,big;
printf("Enter integers, give zero to stop..\n");
scanf("%d",&n);
big=n;        /* assuming that the first one is the biggest */
while(n!=0)  /* as long as n is not zero */
{
scanf("%d",&n);
if(n>big)
big=n;
}
printf("Biggest number  %d",big);
return 0;
}
```

Execution:
Enter integers, give zero to stop..
-23
45
22
12
0
Biggest number  45

Specification: Accept two numbers and print L.C.M (least common multiple)

Show Logic

Least common multiple is the least positive integer that is divisible by both the numbers. Let us see an example

What would be the Least Common Multiple of 2 and 5?

Multiples of 2 are
2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24…..

Multiples of 5 are
5, 10, 15, 20, 25….

Common multiples to both 2 and 5 are
10, 20 ……..

Least Common Multiple (LCM) is 10

Here we will start generating the natural numbers from biggest number of accepted numbers

```big=(a>=b)?a:b;  /*storing biggest number into big */
for(i=big;;i++)     /* starting loop from biggest number */
```

We check whether i is divisible by both numbers a, b for every iteration, if so we declare i as L.C.M and come out of the loop

```big=(a>=b)?a:b;
for(i=big;;i++)
if(i%a==0&&i%b==0)
{
lcm=i;
break;
}
```

Program:

```#include<stdio.h>
int main()
{
int a,b,big,lcm,i;
printf("Enter two integers:");
scanf("%d%d",&a,&b);
big=(a>=b)?a:b;
for(i=big;;i++)         /* infinite loop with no condition */
if(i%a==0&&i%b==0)    /* found LCM */
{
lcm=i;
break;
}
printf("LCM of  two integers is  %d",lcm);
return 0;
}
```

Execution 1:
Enter two integers:3    5
LCM of  two integers is  15

Execution 2:
Enter two integers:20   50
LCM of  two integers is  100

Specification: Accept two numbers and print G.C.D or H.C.F (Greatest Common Divisor or Highest Common factor)

Show Logic

Greatest Common Divisor is also called Highest Common Factor.
GCD is a maximum positive integer which can divide both the numbers with no remainder.

What would be the GCD of 15 and 12?

Divisors of 15
1, 3, 5, 15

Divisors of 12
1, 2, 3, 6, 12

Common Divisors
1, 3

Greatest Common Divisor
3

Here we will start generating the natural numbers from smallest number of accepted numbers towards 1.

```small=(a<=b)?a:b;
for(i=small;;i--)
```

We check whether both a and b are divisible by i for every iteration, if so we declare i as GCD and come out of the loop

```small=(a<=b)?a:b;  /*storing smallest number into small */
for(i=small;;i--)
if(a%i==0&&b%i==0)
{
gcd=i;
break;
}
```

Program:

```#include<stdio.h>
int main()
{
int a,b,small,gcm,i;
printf("Enter two integers:");
scanf("%d%d",&a,&b);
small=(a<=b)?a:b;
for(i=small;;i--)             /* infinite loop with no condition */
if(a%i==0&&b%i==0)           /* found GCM*/
{
gcm=i;
break;
}
printf("GCM of  two integers is  %d",gcm);
return 0;
}
```

Execution 1:
Enter two integers:15   12
GCM of  two integers is  3

Execution 2:
Enter two integers:54   24
GCM of  two integers is  6

{ 0 comments… add one now }

Previous post:

Next post: