Loop Control Statements 137
6.21 Perform multiplication of two integers by using negative sign.
# include <conio.h>
# include <math.h>
mainO
{
in t a ,b ,c,d « 0 ;
c l r s c r () ;
printf ("tt Enter two numbers
scanf ("%d %d", &a,&b);
for (c=l;c<=b;c++)
d=(d)-(-a);
printf (" Multiplication of% d * %d :%d",a,b,d);
I
p.umnj
Enter two numbers : 5 5
Multiplication of 5 * 5 : 25
Explanation The multiplication is nothing but repetitive addition. For example multiplication of
5 x 5 is 25 and the result is obtained by adding 5,5 times. Here, the two numbers are entered and the
1st number ( ' a 7) is repeatedly added to ' b 7 (2nd number) times and result is stored in ' d 7. Instead
of using ' + 7 sign double -v e is used. Multiplication of two -v e signs is +ve (- * - = +).
6.22 Perform multiplication of two integers by using repetitive addition.
# include <conio.h>
# include <math.h>
mainO
{
int a ,b ,cs l,d = 0 ;
c l r s c r ();
printf ("n Enter two numbers
scanf ("%d %d”, &a,&b);
for (;;)
I
d=d+a;
if(c^ b )
goto stop;
C++;
stop:
printf ("Multiplication of% d * %d :%d",a,b,d);
I
138 Programming and Data Structures
OUTPUT:
Enter two numbers : 8 4
Multiplication of 8 * 4 : 32
Explanation Here also the two numbers are entered through the keyboard. The infinite fo r loop
is used and counter %c ' is initialized to 1 and 'd' to 0. Variable 'd ' is used for storing the result
after repetitive addition. After adding value of 'a ' in 'd ' each time the counter %c ' is tested with
' b ' . If there is no match, counter is increased and the program goes back in the loop. Again the
addition is performed. When 1 c ' equals to 'b ' (repetitive addition of the first number till the counter is
equal to second number), program terminates using goto statement and result of multiplication is
displayed.
6.23 Calculate sum & average of five subjects.
# in clu d e < s td io .h >
# in clu d e <conio.h >
mainO
{
in t a ,b ,c,d ,e,su m B 0 ,i;
flo a t avg;
c l r s c r ();
printf ("nEnter The Marks o f Five Subjects");
fo r (i=l;i<=5;i++)
printf (" [%d] Student: ",i);
if (scanf ("%d %d %d %d %d"/&a,&b,&xf&d/&e)==5)
{
sum=a+b+c+d+e;
avg=sum/5;
printf ("n Total Marks o f Student[%d] %d",i,sum);
printf ("n Average Marks o f Student[%d] %f ",i,avg);
I
else
I
clrscrO;
printf ("n Type Mismatch");
)
I
OUTPUT:
Enter The Marks of Five Subjects
Loop Control Statements 139
[1] Student: 58 52 52 56 78
Total Marks of Student[lJ 296
Average Marks of Student[l] 59.000000
[2] Student:
Explanation Here, the for loop is used for entering the marks of five students. The marks of five
subjects are entered through the keyboard. They are assigned to variables a , b, c , d, e. Their
sum and average are calculated. If by mistake the user enters marks other than integers the program
reports the error message "Type Mismatch". The scanf () is compared with the number of
arguments entered correctly. If it is less than the given argument the program displays "Type
Mismatch". The marks of 2nd student onwards can be entered and the results can be observed.
6.24 Write a program to enter a character and display its position in alphabetic.
# include <stdio.h>
# include <conio.h>
# include <ctype.h>
void main()
{
in t c=l;
char i , ch;
c l r s c r ();
printf ("Enter a character
scanf ("%c”Ach);
ch=toupper(ch);
for (i=65;i<=90;i++,c++)
if (ch ~ i)
printf("n'%c'is%d [st/ndlrd/th] Character in Alphabetic. ,i,c);
I
OUTPUT:
Enter a character : U
'U' is 21 [st//nd/rd/th] Character in Alphabetic.
Explanation In this program a character is entered. It is converted into capital. The ASCII values of
A to Z are from 65 to 90. The for loop is taken from 65 to 90 and these values are compared with the
entered character. Certainly there will be a match somewhere. For this, statement (ch==i) is used.
Till the match is achieved counter is increased. Once match occurs we get the position of the character
from the first alphabetic with the help of a counter.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.129.194.123