Example 3.1
The program in Fig.3.1 shows the use of integer arithmetic to convert a given number of days
into months and days.
PROGRAM TO CONVERT DAYS TO MONTHS AND DAYS
Program
main ()
{
int
months, days ;
printf("Enter days\n") ;
scanf("%d", &days) ;
months = days / 30 ;
days
= days % 30 ;
printf("Months = %d
Days = %d", months, days) ;
}
Output
Enter days
265
Months = 8
Days = 25
Enter days
364
Months = 12
Days = 4
Enter days
45
Months = 1
Days = 15
_______________________________________________________________
Fig. 3.1
Illustration of integer arithmetic
Example 3.2
Program of Fig.3.2 prints a sequence of squares of numbers. Note the use of the shorthand
operator *= .
The program attempts to print a sequence of squares of numbers starting from 2.
The statement
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
a *= a;
which is identical to
a = a*a;
replaces the current value of
a
by its square.
When the value of
a
becomes equal or greater than
N
(=100) the
while
is terminated.
Note that the output contains only three values 2, 4 and 16.
USE OF SHORTHAND OPERATORS
Program
#define
N
100
#define
A
2
main()
{
int
a;
a
=
A;
while( a < N )
{
printf("%d\n", a);
a *= a;
}
}
Output
2
4
16
Fig. 3.2
Use of shorthand operator *=
Example 3.3
In Fig.3.3, the program employs different kinds of operators. The results of their evaluation are
This is the end of the preview.
Sign up
to
access the rest of the document.
 Spring '09
 SHANTA
 #define, 1 days, 4 days, 45 months, 25 Days

Click to edit the document details