Written in C Not C++ or C# 1. Write a function to test whether a given number is
ID: 3746382 • Letter: W
Question
Written in C
Not C++ or C#
1. Write a function to test whether a given number is a multiple of 8. A one (1) is returned if the given number is a multiple of 8. Otherwise, a zero (0) is returned. The number to be tested is an integer and is an argument to this function.
2. Write a switch statement that will examine the value of an integer variable yy and assign one of the following values to the variable dd, depending on the value of yy:
(a)15, if yy == 1
(b)25, if yy == 2
(c) 35, if yy == 3
(d)45, if yy == 4
(e) 55, if yy == 5
3. Write a loop to compute the sum of the squares of the first 100 odd integers.
Explanation / Answer
1)
int test8(int num) //function takes num as integer
{
if(num%8==0) //if 8 successfully divides the num with remainder 0
return 1; //return 1
return 0; //otherwise return 0
}
2)
switch(yy) //examin the yy
{
case 1: dd=15; //if yy=1 then dd=15
break;
case 2: dd=25; //if yy=2 then dd=25
break;
case 3: dd=35; //if yy=3 then dd=35
break;
case 4: dd=45; //if yy=4 then dd=45
break;
case 5: dd=55; //if yy=5 then dd=55
break;
}
3)
for(i=0;i<100;i++) //loop for count 100 numbers start from 0 to 99
{
//each time i is multiplied with 2 and add 1 to form odd number
//then each time this odd number is added to variable sum
sum+=2*i+1;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.