I need help with these following questions. 1.Write a program that accepts 3 int
ID: 3724968 • Letter: I
Question
I need help with these following questions. 1.Write a program that accepts 3 integers, a, b, and c, calculates and prints the followings i) a "bitwise or" with "complement of" b ii) (b "inclusive or with" c) “or" a ii) a "bitwise and" with "complement of" a 2) Write a program to print the values between 1 and 200 that are divisible by 2 and 3 3) Write a program to accept an unsigned integer and calculates the number of I's in its binary representation. 4) We can calculate the distance traveled by an object using distance-velocity x time. Calculate the total distance traveled for speeds (in units of speed/unit of time) in the interval [40,60] in steps of 2 for each time (in unit of time) in the interval [10, 50] in steps of 5) What is the output of the following program and WHY? #include int mainO int x = 2; switch (x) case 1: printf("Choice is 1n"); case 2: printf("Choice is 21n"); case 3: printf("Choice is 3ln"); case 4: printf"Choice is 4ln"); break break printf("After Switch"); default: printf Choice other than 1, 2, 3 and 4In"); return 0 6) lfa-98 (decimal), b=0x1a83 (hexadecimal), and c-v73401 (octal), calculate the following i) a & b ii) blc ii (alc)b iv) (c)&a;Explanation / Answer
1) C program : Here, '|' = bitwise inclusive or, '~' = complement, '||' = OR, '&' = bitwise and.
say a = 2, b = 3, c = 5
a = 0000 0000 0000 0000 0000 0000 0000 0010 (in binary, assuming int has 4 bytes or 32 bits)
b = 0000 0000 0000 0000 0000 0000 0000 0011
~b = 1111 1111 1111 1111 1111 1111 1111 1100 (change all 0's to 1's)
a|(~b) = 1111 1111 1111 1111 1111 1111 1111 1110 = (-2 in two's complement form)
OR (||) operator evaluates to true iff atleast one of its operands is non-zero or true. So, 5 OR 7 = 1 because both operands are non-zero. Similarly, 1 OR 0 = 1 because one of the operands is 1. However, 0 OR 0 = 0.
#include <stdio.h>
#include <stdlib.h>
int main()
{
printf("Enter three integers ");
int a, b, c;
scanf("%d %d %d", &a, &b, &c);
printf("1. a "bitwise or" with"complement of" b : %d ", (a | (~b)));
printf("2. (b "inclusive or with" c) "or" a : %d ", ((b | c) || a));
printf("3. a "bitwise and" with "complement of" a : %d ", (a & (~a)));
return 0;
}
2) C program : A number that is divisible by 2 and 3 is divisible by 6 because LCM of 2 and 3 = 6.
#include <stdio.h>
#include <stdlib.h>
int main()
{
printf("All numbers between 1 and 200 that are divisible by 2 and 3 are: ");
int i;
for(i = 1; i <= 200; i++)
{
if(i % 6 == 0) //check if num 'i' is divisible by 2 and 3 -> check if i is divisible by 6
printf("%d ", i);
}
printf(" ");
return 0;
}
3) C program : In order to find out the number of 1's in a number's binary reporesentation, we count the times the loop executes before n becomes 0. We update 'n' in each iteration to n&(n-1).
Ex: Initialize c = 0.
n = 9 (1001) n >0
n-1 = 8 (1000)
n&(n-1) = 1000 = 8. c = 1;
Now, n = 8 > 0. (1000). (n-1) = 7 (0111)
n&(n-1) = 8 & 7 = 0000 = 0. c = 2.
Since n is now 0, we break out of the loop. return c which will hold the count of 1's.
#include <stdio.h>
#include <stdlib.h>
int main()
{
printf("Enter an unsigned integer: ");
unsigned int a;
scanf("%u", &a);
int c = 0; //will hold the count of number of 1's in the binary rep
while(a){ // check if a > 0
a = a&(a-1);
c++;
}
printf("Number of 1's in the binary representation of %u is %d ", a, c);
return 0;
}
4) C program: we will use two loops, one for velocity, the other for time. For every value in velocity, each value of time is used to calculate the distance travelled which is stored in long long int dist.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i, j;
long long dist = 0;
for(i = 40; i <= 60; i=i+2){
for(j = 10; j <= 50; j=j+5){
printf("Velocity = %d, Time = %d ", i, j);
dist += (i*j);
}
}
printf("Total distance travelled is %lld ", dist); //lld for long long integer
return 0;
}
5) The output will be :
Choice is 2
Choice is 3
Choice is 4
After Switch
Since, x = 2, we will compare 2 with all switch cases. We find a match at 'case 2', so we print "Choice is 2". Now, since there is no 'break' statement, the execution will fall through all cases until it reads a 'break' statement. That is, "Choice is 3", "Choice is 4" will be printed. In case 4, after the printf statement, there is a 'break' statement, therefore, the control passes to the first statement after switch. This will print "After Switch".
6) a = 98(decimal) = 0000 0000 0110 0010
b = 0xfa83 = 1111 1010 1000 0011
1) a&b = 0000 0000 0000 0010 (1 only where both inputs or operands are 1)
2) c = o73401 = 111 011 100 000 001 (Add 0's in the beginning (left) to make the number of bits in both, equal. This is called padding.)
So, b = 1111 1010 1000 0011
c = 0111 0111 0000 0001
b|c = 1111 1111 1000 0011
3) a = 0000 0000 0110 0010
c = 0111 0111 0000 0001
a|c = 0111 0111 0110 0011
b = 1111 1010 1000 0011
(a|c)^b = 1000 1101 1110 0000 (x^y = 1 only when either x=1 or y=1 but not both ^- is XOR)
4) c = 0111 0111 0000 0001
~c = 1000 1000 1111 1110 //all 0's become 1's. All 1's become 0's.
a = 0000 0000 0110 0010
(~c)&a = 0000 0000 0110 0010