complete lab02
This commit is contained in:
2 files changed
+81
No files matched your search
@@ -0,0 +1,45 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Course: COMP 2510
|
||||||
|
* Assignment: Assignment 2
|
||||||
|
* Name: Braeden Sowinski
|
||||||
|
* Student ID: A01385066
|
||||||
|
* Date: 2025-10-29
|
||||||
|
* Description: Checks if both the second
|
||||||
|
* and fourth bit of a given number are
|
||||||
|
* on or not.
|
||||||
|
*/
|
||||||
|
void printBinary(int n)
|
||||||
|
{
|
||||||
|
printf("Binary: ");
|
||||||
|
|
||||||
|
// sizeof(int) * 2 - 1 = number of bits. i.e. 7 bits, range 7 to 0 inclusive
|
||||||
|
for (int i = sizeof(int) * 2 - 1; i >= 0; i--)
|
||||||
|
{
|
||||||
|
// shift bit 'i' to bit index 0, use '&' to check if bit is 1 or 0
|
||||||
|
printf("%d", (n >> i) & 1);
|
||||||
|
}
|
||||||
|
printf("\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
int areSecondAndFourthBitsOn(int n)
|
||||||
|
{
|
||||||
|
int mask = 0b00010100;
|
||||||
|
|
||||||
|
return (n & mask) == mask;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
int n;
|
||||||
|
|
||||||
|
printf("Enter an integer: ");
|
||||||
|
scanf("%d", &n);
|
||||||
|
|
||||||
|
printBinary(n);
|
||||||
|
areSecondAndFourthBitsOn(n) == 1 ? printf("Second and fourth bits are ON.\n") :
|
||||||
|
printf("Second and fourth bits are NOT both ON.\n");
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,36 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Course: COMP 2510
|
||||||
|
* Assignment: Assignment 2
|
||||||
|
* Name: Braeden Sowinski
|
||||||
|
* Student ID: A01385066
|
||||||
|
* Date: 2025-10-29
|
||||||
|
* Description: Sum of digits uses recursion to
|
||||||
|
* calculate the sum of digits of a number.
|
||||||
|
* e.g. 12345 -> 1 + 2 + 3 + 4 + 5 = 15
|
||||||
|
*/
|
||||||
|
int sumOfDigits(int n)
|
||||||
|
{
|
||||||
|
if (n == 0)
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
return (n % 10) + sumOfDigits(n / 10);
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
int n;
|
||||||
|
int sum;
|
||||||
|
|
||||||
|
printf("Enter a number: ");
|
||||||
|
scanf("%d", &n);
|
||||||
|
|
||||||
|
sum = sumOfDigits(n);
|
||||||
|
|
||||||
|
printf("Sum of digits = %d\n", sum);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
Reference in new issue
Block a user