112
112
NIM: 2702282982
Yes it is, but it has slightly different behavior when scanning strings or array of char.
scanf when scanning %s, as described in the documenta on
So, one specifier variable will stop being scanned a er finding a whitespace character,
such as space, newline, carriage return, etc.
So, if we input “Good Morning” using scanf, the output will be “Good”
gets on the other hand reads all the character from the standard input.
So, if we input “Good Morning” using gets, the output will be “Good ”
4. If all variables are integer, then state the value of A if:
a. A = B && E;
Answer: This is an AND condi onal operator, so value other than 0 is equal to 1 or true.
So A will be false because of E = 0;
b. A = B & C;
Answer: This is an AND bitwise operator, so the value of A will be
B: 1101
C: 1011
------------------ &
A: 1001
c. A = C || D;
Answer: This is an OR condi onal operator, so value other than 0 is equal to 1 or true. So
A will be 1 or true;
d. A = B | D;
Answer: This is an OR bitwise operator, so the value of A will be
B: 0000 1101
D: 0010 1010
------------------ &
A: 0010 1111
e. A = B > 2;
Answer: This is an greater than condi onal operator, because B = 13 is greater than 2,
then A is true or 1.
f. A = B >> 2;
Answer: This is a bitshi operator, the data bits of B will be shi ed right 2 mes
B: 0000 1101
B >> 1: 0000 0110
(B >> 1) >> 1: 0000 0011
g. A = C < 3;
Answer: This is an less than condi onal operator, because C = 11 and not less than 3,
then A is false or 0.
h. A = C << 3;
Answer: This is a bitshi operator, the data bits of C will be shi ed le 3 mes
C: 0000 1011
C << 3: 0101 1000
i. A = B = C;
Answer: B will be equal to C, then A will be equal to C
j. A = B == C;
Answer: This is an equality condi onal operator, because B = 13 is and C = 11, therefore
B is not equal to C, so A will be false or 0.
5. Given the following decimal : 15870, convert it to :
a. Binary
b. Octal
c. Hexadecimal
Answer:
void toBinary(int d) {
int temp = d;
int bin[100];
int i;
bin[i] = temp % 2;
temp /= 2;
printf("binary: ");
printf("\n");
}
void toOctal(int d) {
int temp = d;
int bin[100];
int i;
bin[i] = temp % 8;
temp /= 8;
printf("octal: ");
printf("\n");
void toHex(int d) {
int temp = d;
int bin[100];
int i;
temp /= 16;
printf("hex: ");
printf("\n");
}
int main(int argc, char *argv[]) {
while (1) {
int input;
scanf("%d", &input);
toBinary(input);
toOctal(input);
toHex(input);
return 0;
Output
Answer:
The block (a) checks the variable mark twice with 2 different condi ons where both condi ons
can be met.
The block (b) checks the variable mark once with 2 op on condi ons where only one of the
op on condi on can be met.
So, given the mark 90, the grade for block (a) will be ‘B’, and the grade for block (b) will be ‘A’
8. What is Nested if?
Nested if is a selec on program where the selec on is performed a er the the previous
selec on’s condi on is met.
example:
If (boolean_expression 1) {
/* Executes when the boolean expression 1 is true */
if(boolean_expression 2) {
/* Executes when the boolean expression 2 is true */
}
}
9. What is the main difference between do-while statements and while statements in C?
Answer:
a. do-while
Execute the statements inside the DO – WHILE scope at least once, and repeat if the
condi on is met
b. while
Execute the statements inside the WHILE – DO scope while the condi on is met
10. Create the following program : N = 10
Answer:
int main(int argc, char *argv[]) {
int len = 10;
int wid = 10;
int i;
for (i = 0; i < len; i++) {
int j;
for (j = 0; j < wid; j++) {
int pos = (i * j) % (len - 1);
if (i == 0 || i == len - 1 || j == 0 || j ==
wid - 1) printf("*");
else printf(" ");
}
printf("\n");
}
return 0;
}
11. How do we validate length of a string is no more than 30 and not less than 5?
Answer:
int check_str_len(const char* str, int min_len, int max_len) {
int len = strlen(str);
return (len >= min_len && len <= max_len);
}
int main(int argc, char *argv[]) {
const char* str = "some string";
printf("%d", check_str_len(str, 5, 30));
return 0;
}
12. Create a program to convert all first le ers of every word to be uppercase, while the rest is
remain unchanged
Answer:
char input[100];
gets(input);
int i = 0;
token = input[i];
i++;
if (len % 2) return 0;
int i;
head = str[i];
tail = str[len-i];
return 1;
while (1) {
char input[100];
scanf("%s", input);
input, isPalindrome(input));
return 0;
14. Create a program to do mul plica on of these two matrices, using array 2D!
Answer:
void matrixMultiplication(void) {
int A[4][4] = {
{5, 7, 8, 2},
{3, 6, 4, 5},
{2, 7, 1, 4},
{1, 3, 9, 6}
};
int B[4][4] = {
{3, 5, 4, 8},
{2, 5, 2, 3},
{1, 1, 2, 7},
{3, 7, 6, 1},
};
int X[4][4] = {
{0, 0, 0, 0},
{0, 0, 0, 0},
{0, 0, 0, 0},
{0, 0, 0, 0},
};
int i, j, k;
printf("\n");