Conditionals and Loops
Conditional Statements in C

#include <assert.h>
#include <limits.h>
#include <math.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char* readline();
int main()
{
char* n_endptr;
char* n_str = readline();
int n = strtol(n_str, &n_endptr, 10);
if (n_endptr == n_str || *n_endptr != '\0') { exit(EXIT_FAILURE); }
// Write Your Code Here: Start
if (1 <= n && n <= 9) {
if (n == 1) printf("one");
else if (n == 2) printf("two");
else if (n == 3) printf("three");
else if (n == 4) printf("four");
else if (n == 5) printf("five");
else if (n == 6) printf("six");
else if (n == 7) printf("seven");
else if (n == 8) printf("eight");
else if (n == 9) printf("nine");
} else {
printf("Greater than 9");
}
// Write Your Code Here: End
return 0;
}
char* readline() {
size_t alloc_length = 1024;
size_t data_length = 0;
char* data = malloc(alloc_length);
while (true) {
char* cursor = data + data_length;
char* line = fgets(cursor, alloc_length - data_length, stdin);
if (!line) { break; }
data_length += strlen(cursor);
if (data_length < alloc_length - 1 || data[data_length - 1] == '\n') { break; }
size_t new_length = alloc_length << 1;
data = realloc(data, new_length);
if (!data) { break; }
alloc_length = new_length;
}
if (data[data_length - 1] == '\n') {
data[data_length - 1] = '\0';
}
data = realloc(data, data_length);
return data;
}
For Loop in C

#include <stdio.h>
int main()
{
int a, b;
scanf("%d\n%d", &a, &b);
for (int n = a; n <= b; n++ ) {
if ( 1 <= n && n <= 9 ) {
if (n == 1) printf("one\n");
else if (n == 2) printf("two\n");
else if (n == 3) printf("three\n");
else if (n == 4) printf("four\n");
else if (n == 5) printf("five\n");
else if (n == 6) printf("six\n");
else if (n == 7) printf("seven\n");
else if (n == 8) printf("eight\n");
else if (n == 9) printf("nine\n");
}
else if ( n > 9) {
if ( n % 2 == 0 ) {
printf("even\n");
} else {
printf("odd\n");
}
}
}
return 0;
}
Sum of Digits of a Five Digit Number

#include <stdio.h>
int main() {
int n;
scanf("%d", &n);
int _, sum = 0;
for (_; _ < 5; _++) {
// Adding the last digit to sum
sum += n % 10;
// Removing the last digit
n /= 10;
}
printf("%d", sum);
return 0;
}
Bitwise Operators

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
//Complete the following function.
void calculate_the_maximum(int n, int k) {
int and_max = 0;
int or_max = 0;
int xor_max = 0;
for ( int a = 1; a <= n; a++) {
for (int b = a + 1; b <= n; b++ ) {
int a_and_b = a & b;
int a_or_b = a | b;
int a_xor_b = a ^ b;
if (a_and_b < k && a_and_b > and_max) and_max = a_and_b;
if (a_or_b < k && a_or_b > or_max) or_max = a_or_b;
if (a_xor_b < k && a_xor_b > xor_max) xor_max = a_xor_b;
}
}
printf("%d\n", and_max);
printf("%d\n", or_max);
printf("%d\n", xor_max);
}
int main() {
int n, k;
scanf("%d %d", &n, &k);
calculate_the_maximum(n, k);
return 0;
}
Printing Pattern Using Loops

#include <stdio.h>
void print_pattern(int n, int row) {
// Print decreasing part
for (int val = n; val > row; val--) {
printf("%d ", val);
}
// Print middle repeating part
int middle_count = (row - 1) * 2 + 1;
for (int j = 0; j < middle_count; j++) {
printf("%d ", row);
}
// Print increasing part
for (int val = row + 1; val <= n; val++) {
printf("%d ", val);
}
printf("\n");
}
int main() {
int n;
scanf("%d", &n);
// Print top half (and middle row)
for (int row = n; row >= 1; row--) {
print_pattern(n, row);
}
// Print bottom half
for (int row = 2; row <= n; row++) {
print_pattern(n, row);
}
return 0;
}
Last updated
Was this helpful?