Friday, October 18, 2013

Using modulus operator

Modulus operator returns the reminder after division. It can be used with integral data types (i.e. integer and char).
Program 21:  
#include <stdio.h>
#include <conio.h>
void main()
{
    char x=’z’,y=’a’;
    clrscr();
    printf("%d”,x%y);
   getch();
}

Output:
#-------------------------------------------------------------------------------#
  25
#-------------------------------------------------------------------------------#

Here the value of x is 122 (the ASCII value of z). And the value of y=97 (the ASCII value of a). So, x%y is equal to 122%97=25 which is the reminder after division.

Program 22: WAP using modulus operator to find whether the inputted number is odd or even.
/* Program using modulus operator. */
#include <stdio.h>
#include <conio.h>
void main()
{
    int number;
    clrscr();
    printf("Enter any number:”);
    scanf(“%d”,&number);
   if(number%2==0)
   {
          printf(“Number is even.”);
   }
   else
   {
 printf(“Number is odd.”); 
 }
   getch();
}

Output:
#-------------------------------------------------------------------------------#
  Enter any number:555
  Number is odd.
#-------------------------------------------------------------------------------#

You can write this program in more fancy way like this:
/* Program using modulus operator. */
#include <stdio.h>
#include <conio.h>
void main()
{
    int number;
    clrscr();
    printf("Enter any number:”);
    scanf(“%d”,&number);
   if(number%2)
   {
          printf(“Number is odd.”);
   }
   else
   {
        printf(“Number is even.”); 
   }
   getch();
}

The statement if(number%2) is similar to if(1) (i.e. if the condition is true). So, if we enter 555 as an input then the expression within if changes to 555%2, and after division it return 1 (the reminder after division. Note that even if the expression returns other than 1 then the output will be the same).

More programs using modulus operator

Program 23: WAP to find number of months, weeks and days, if number of days is input through the keyboard.

/* Program using modulus operator. */
#include <stdio.h>
#include <conio.h>
void main()
{
    int days,rdays,months,weeks;
    clrscr();
    printf("Enter number of days:”);
    scanf(“%d”,&days);
    /* Considering 30 day month.*/
    months=days/30;
    /* Remaining days after month calculation. */
    rdays=days%30;
    /* Calculate weeks. */
    weeks=rdays/7;
    /* Remaining days after week calculation. */
    rdays=rdays%7;
    printf(“\n Number of days entered=%d”,days);
    printf(“\n Number of months=%d”,months);
    printf(“\n Number of weeks=%d”,weeks);
    printf(“\n Remaining days=%d”,rdays);
    getch();
}

Output:
#-------------------------------------------------------------------------------#
  Enter number of days:250
  Number of days entered:250
  Number of months=8
  Number of weeks=1
  Remaining days=3  
#-------------------------------------------------------------------------------#

Program 24: WAP to find the sum of digits of an inputted four digit number.
#include <stdio.h>
#include <conio.h>
void main()
{
    int number,sum=0,p;
    clrscr();
    printf("Enter any four digit number:”);
    scanf(“%d”,&number);
   
    /* Considering inputted number is 1234.  Extract last number from 1234.*/
    p=number%10;
    sum=sum+p;
    number=number/10;
         
    /* Extract last digit from 123 and add the number to the last sum variable   
        sum.*/
    p=number%10;
    sum=sum+p;
    number=number/10;

    /* Extract last digit from 12 and add the number to the last sum variable   
        sum.*/
    p=number%10;
    sum=sum+p;
    number=number/10;

    /* Extract last digit from 1 and add the number to the last sum variable   
        sum.*/
    p=number%10;
    sum=sum+p;
    number=number/10;

    printf(“\nSum of digits=%d”,sum);
    getch();
}

Output:
#-------------------------------------------------------------------------------#
  Enter any four digit number:1234
  Sum of digits=10
#-------------------------------------------------------------------------------#

If you observe the program, you may noticed that, we use the following section of the code repeatedly in our program.
    p=number%10;
    sum=sum+p;
    number=number/10;

Consider, our inputted number is 1234. Then at the first pass the above section changes to:
1st pass: p=1234%10;       i.e. p=4
              sum=sum+p;      i.e. sum=0+4=4
                number=number/10; i.e. number=1234/10=123       

So, in the first pass the value of sum changes to 5 and the value of variable number reduced to 123.

2nd pass: p=123%10;       i.e. p=3
              sum=sum+p;      i.e. sum=4+3=7
                number=number/10; i.e. number=123/10=12           

So, in the second pass the value of sum changes to 7 and the value of variable number reduced to 12.

3rd pass: p=12%10;         i.e. p=2
              sum=sum+p;      i.e. sum=7+2=9
                number=number/10; i.e. number=12/10=1               

So, in the third pass the value of sum changes to 9 and the value of variable number reduced to 1.

4th pass: p=1%10;            i.e. p=1
              sum=sum+p;      i.e. sum=9+1=10
                number=number/10; i.e. number=1/10=0       

So, in the last pass the value of sum changes to 10 and the value of variable number reduced to 0. And in this way our program finds the sum of digits of a given four digit number.

Program 25: WAP to find the reverse of an inputted four digit number.
#include <stdio.h>
#include <conio.h>
void main()
{
    int number,rev=0,p;
    clrscr();
    printf("Enter any four digit number:”);
    scanf(“%d”,&number);
   
    p=number%10;
    rev=rev*10+p;
    number=number/10;
         
    p=number%10;
    rev=rev*10+p;
    number=number/10;

    p=number%10;
    rev=rev*10+p;
    number=number/10;

    p=number%10;
    rev=rev*10+p;
    number=number/10;


    printf(“\nReverse number=%d”,rev);
    getch();
}

Output:
#-------------------------------------------------------------------------------#
  Enter any four digit number:1234
  Reverse number=4321
#-------------------------------------------------------------------------------#

To understand how the program works, refer to the above program.

Program 26: WAP to find the sum of first and last digit of an inputted four digit number.
#include <stdio.h>
#include <conio.h>
void main()
{
    int first_number,last_number,number,sum=0;
    clrscr();
    printf("Enter any four digit number:”);
    scanf(“%d”,&number);
   
    /* Extracts last digit. */
    last_number=number%10;

    /* Extracts first digit. */
    first_number=number/1000;

    sum=first_number+last_number;
    printf(“\nSum of first and last digit=%d”,sum);
    getch();
}

Output:
#-------------------------------------------------------------------------------#
  Enter any four digit number:1234
  Sum of first and last digit=5
#-------------------------------------------------------------------------------#

Program 27: WAP to convert a sum of money in pence into the equivalent sum in pounds and pence.
#include <stdio.h>
#include <conio.h>
void main()
{
    int pence,pounds;
    clrscr();
    printf("Enter the amount in pence:”);
    scanf(“%d”,&pence);
    printf(“\n%d pence is:”,pence);
    pounds=pence/100;
    /* Remaining pence after pound conversion. */
    pence=pence%100;
    printf(“\n%d pounds %d pence.”, pounds, pence);
    getch();
}


Output:
#-------------------------------------------------------------------------------#
 Enter the amount in pence:625
 625 pence is:6 pounds 25 pence.
#-------------------------------------------------------------------------------#
Now, we are going to write one more interesting program using modulus operator.

Program 28: WAP which receives the number in the form 123.45 and find the sum of digits in such a fashion that the result will be 6.9.
#include <stdio.h>
#include <conio.h>
void main()
{
    int num1,num2,sum1,sum2,p1,p2;
    int orig_num1,orig_num2;
    char point;

    sum1=sum2=0;
    clrscr();
    printf(“Enter number (E.g. 123.45):”);
    scanf(“%3d%c%2d”,&num1,&point,&num2);

    orig_num1=num1;
    orig_num2=num2;
   
    /* Find sum of digits of num1 */
    p1=num1%10;
    sum1=sum1+p1;
    num1=num1/10;
               
    p1=num1%10;
    sum1=sum1+p1;
    num1=num1/10;

    p1=num1%10;
    sum1=sum1+p1;
    num1=num1/10;

    /* Find the sum of digits of num2 */
    p2=num2%10;
    sum2=sum2+p2;
    num2=num2/10;

    p2=num2%10;
    sum2=sum2+p2;
    num2=num2/10;
   
    printf(“Sum of digits of %d%c%d=%d%c%d”,orig_num1, point, orig_num2,
    sum1,point,sum2);
   
    getch();
}

#-------------------------------------------------------------------------------#
 Enter number (E.g. 123.45):123.45
 Sum of digits of 123.45=6.9
#-------------------------------------------------------------------------------#

The program output is pretty simple. The program receives the number in the form of xxx.xx say (123.45). The scanf() function divides that number in three parts. The part before decimal point i.e. xxx is stored in the variable num1. The decimal point is assigned to the character variable point ant the rest of the part i.e. xx is assigned to the variable num2. We never used scanf() like this before. Program stores the original value of num1 and num2 into corresponding integer variables orig_num1 and orig_num2, using the following statements:

orig_num1=num1;
orig_num2=num2;

Then it finds the sum of digits separately of variables num1 and num2 and stores their sum in the variable sum1 and sum2. And finally program prints the result. But, before moving to another topic, we will study about this new look of scanf().

No comments:

Post a Comment