Test your knowledge for the course Logic Building with Computer Programming (CSU1128) under the guidance by Dr. Pankaj Vaidya.
Program 1
/* Miscellaneous Program by dmj.one
* DO NOT COPY DO NOT REPRODUCE DO NOT MODIFY
*
* (c) 2023, Divya Mohan for dmj.one. All rights reserved.
*/
#include <stdio.h>
int main() {
int i = 3;
printf("%d %d %d\n", i--, i--, i);
return 0;
}
Output:
2 3 1 / 2 1 0 / 3 2 1 / something else? You decide!!Program 2
/* Miscellaneous Program by dmj.one
* DO NOT COPY DO NOT REPRODUCE DO NOT MODIFY
*
* (c) 2023, Divya Mohan for dmj.one. All rights reserved.
*/
#include <stdio.h>
int main() {
int i = 3;
printf("%d\n", i = i++);
return 0;
}
Output:
3Program 3
/* Miscellaneous Program by dmj.one
* DO NOT COPY DO NOT REPRODUCE DO NOT MODIFY
*
* (c) 2023, Divya Mohan for dmj.one. All rights reserved.
*/
#include <stdio.h>
void func(int *p) {
*p += 2;
p += 2;
}
int main() {
int i = 4;
int *p = &i;
func(p);
printf("%d\n", i);
return 0;
}
Output:
6Program 4
/* Miscellaneous Program by dmj.one
* DO NOT COPY DO NOT REPRODUCE DO NOT MODIFY
*
* (c) 2023, Divya Mohan for dmj.one. All rights reserved.
*/
#include <stdio.h>
int main() {
int i = 5;
i = i + i++ * i++;
printf("%d\n", i);
return 0;
}
Output:
27 / 37 / something else / something else? You decide!!Program 5
/* Miscellaneous Program by dmj.one
* DO NOT COPY DO NOT REPRODUCE DO NOT MODIFY
*
* (c) 2023, Divya Mohan for dmj.one. All rights reserved.
*/
#include <stdio.h>
int main() {
int i = 1;
i = i + ++i;
printf("%d\n", i);
return 0;
}
Output:
4Program 6
/* Miscellaneous Program by dmj.one
* DO NOT COPY DO NOT REPRODUCE DO NOT MODIFY
*
* (c) 2023, Divya Mohan for dmj.one. All rights reserved.
*/
#include <stdio.h>
void func(int a[], int n) {
int i;
for (i = 0; i < n; i++) {
a[i] = a[i] + 2;
a++;
}
}
int main() {
int a[] = {1, 2, 3, 4};
int n = sizeof(a) / sizeof(int);
func(a, n);
int i;
for (i = 0; i < n; i++) {
printf("%d ", a[i]);
}
return 0;
}
Output:
3 5 7 9 / 3 2 5 4 / something else? You decide!!Program 7
/* Miscellaneous Program by dmj.one
* DO NOT COPY DO NOT REPRODUCE DO NOT MODIFY
*
* (c) 2023, Divya Mohan for dmj.one. All rights reserved.
*/
#include <stdio.h>
int main() {
int i = 5, j = 8;
i = i++ + ++j;
printf("%d %d\n", i, j);
return 0;
}
Output:
14 9Program 8
/* Miscellaneous Program by dmj.one
* DO NOT COPY DO NOT REPRODUCE DO NOT MODIFY
*
* (c) 2023, Divya Mohan for dmj.one. All rights reserved.
*/
#include <stdio.h>
int main() {
int i = 5, j = 3;
i = i & j | i ^ j;
printf("%d\n", i);
return 0;
}
Output:
1 / 7 / something else? You decide!!Program 9
/* Miscellaneous Program by dmj.one
* DO NOT COPY DO NOT REPRODUCE DO NOT MODIFY
*
* (c) 2023, Divya Mohan for dmj.one. All rights reserved.
*/
#include <stdio.h>
int main() {
int i = 5, j = 3;
i += (j = 3);
printf("%d\n", i);
return 0;
}
Output:
8Program 10
/* Miscellaneous Program by dmj.one
* DO NOT COPY DO NOT REPRODUCE DO NOT MODIFY
*
* (c) 2023, Divya Mohan for dmj.one. All rights reserved.
*/
#include <stdio.h>
int main() {
int a = 3, b = 4;
if (a = b) {
printf("Equal\n");
} else {
printf("Not Equal\n");
}
return 0;
}
Output:
EqualProgram 11
/* Miscellaneous Program by dmj.one
* DO NOT COPY DO NOT REPRODUCE DO NOT MODIFY
*
* (c) 2023, Divya Mohan for dmj.one. All rights reserved.
*/
#include <stdio.h>
int main() {
int i = 0;
for (i = 0; i < 10; i++) {
if (i == 5) {
continue;
} else if (i == 8) {
break;
}
printf("%d ", i);
}
return 0;
}
Output:
0 1 2 3 4 6 7Program 12
/* Miscellaneous Program by dmj.one
* DO NOT COPY DO NOT REPRODUCE DO NOT MODIFY
*
* (c) 2023, Divya Mohan for dmj.one. All rights reserved.
*/
#include <stdio.h>
int main() {
int i = 0, j = 0;
i = i + j++;
i = i + ++j;
printf("i = %d j = %d\n", i, j);
return 0;
}
Output:
i = 2 j = 2 / i = 3 j = 2 / something else? You decide!!Program 13
/* Miscellaneous Program by dmj.one
* DO NOT COPY DO NOT REPRODUCE DO NOT MODIFY
*
* (c) 2023, Divya Mohan for dmj.one. All rights reserved.
*/
#include <stdio.h>
int main() {
int a = 0, b = 0, c = 0;
a = b = c = 5;
printf("a = %d b = %d c = %d\n", a, b, c);
return 0;
}
Output:
a = 5 b = 5 c = 5Program 14
/* Miscellaneous Program by dmj.one
* DO NOT COPY DO NOT REPRODUCE DO NOT MODIFY
*
* (c) 2023, Divya Mohan for dmj.one. All rights reserved.
*/
#include <stdio.h>
int main() {
int i = 5, j = 3;
i += j += i += j;
printf("i = %d j = %d\n", i, j);
return 0;
}
Output:
i = 16 j = 8 / i = 19 j = 11 / something else? You decide!!Program 15
/* Miscellaneous Program by dmj.one
* DO NOT COPY DO NOT REPRODUCE DO NOT MODIFY
*
* (c) 2023, Divya Mohan for dmj.one. All rights reserved.
*/
#include <stdio.h>
int main() {
int a = 0, b = 0;
a = (b = 5) + (b = 3);
printf("a = %d b = %d\n", a, b);
return 0;
}
Output:
a = 8 b = 3 / a = 6 b = 3 / something else? You decide!!Program 16
/* Miscellaneous Program by dmj.one
* DO NOT COPY DO NOT REPRODUCE DO NOT MODIFY
*
* (c) 2023, Divya Mohan for dmj.one. All rights reserved.
*/
#include <stdio.h>
int main() {
int i = 0, j = 0;
i += ++j;
i += j++;
printf("i = %d j = %d\n", i, j);
return 0;
}
Output:
i = 2 j = 2Program 17
/* Miscellaneous Program by dmj.one
* DO NOT COPY DO NOT REPRODUCE DO NOT MODIFY
*
* (c) 2023, Divya Mohan for dmj.one. All rights reserved.
*/
#include <stdio.h>
int main() {
int i = 0, j = 0;
i = i++ + j++;
i = i + j;
printf("i = %d j = %d\n", i, j);
return 0;
}
Output:
i = 2 j = 2 / i = 1 j = 1 / i = 3 j = 1 / something else? You decide!!Program 18
/* Miscellaneous Program by dmj.one
* DO NOT COPY DO NOT REPRODUCE DO NOT MODIFY
*
* (c) 2023, Divya Mohan for dmj.one. All rights reserved.
*/
#include <stdio.h>
void fun(int x)
{
x = x + 1;
printf("x = %d\n", x);
}
int main()
{
int y = 5;
fun(y++);
printf("y = %d\n", y);
return 0;
}
Output:
x = 6 y = 6Program 19
/* Miscellaneous Program by dmj.one
* DO NOT COPY DO NOT REPRODUCE DO NOT MODIFY
*
* (c) 2023, Divya Mohan for dmj.one. All rights reserved.
*/
#include <stdio.h>
int main()
{
int a = 5;
a = (a++) + (++a);
printf("a = %d\n", a);
return 0;
}
Output:
a = 7 / a = 12 / something else? You decide!!Program 20
/* Miscellaneous Program by dmj.one
* DO NOT COPY DO NOT REPRODUCE DO NOT MODIFY
*
* (c) 2023, Divya Mohan for dmj.one. All rights reserved.
*/
#include <stdio.h>
#include <stdlib.h>
int main() {
int a[] = {1, 2, 3, 4, 5};
int *p = (int*) malloc(5 * sizeof(int));
memcpy(p, a, 5 * sizeof(int));
int i;
for (i = 0; i < 5; i++) {
printf("%d ", p[i]);
}
free(p);
return 0;
}
Output:
1 2 3 4 5Program 21
/* Miscellaneous Program by dmj.one
* DO NOT COPY DO NOT REPRODUCE DO NOT MODIFY
*
* (c) 2023, Divya Mohan for dmj.one. All rights reserved.
*/
#include <stdio.h>
int main()
{
int a = 5;
a = (a--) + (--a);
printf("a = %d\n", a);
return 0;
}
Output:
a = 3 / a = 7 / a = 8 / something else? You decide!!Program 22
/* Miscellaneous Program by dmj.one
* DO NOT COPY DO NOT REPRODUCE DO NOT MODIFY
*
* (c) 2023, Divya Mohan for dmj.one. All rights reserved.
*/
#include <stdio.h>
#include <string.h>
int main() {
char s1[] = "Hello";
char s2[] = "World";
int i = strcmp(s1, s2);
if (i == 0) {
printf("Equal\n");
} else {
printf("Not Equal\n");
}
return 0;
}
Output:
Not EqualProgram 23
/* Miscellaneous Program by dmj.one
* DO NOT COPY DO NOT REPRODUCE DO NOT MODIFY
*
* (c) 2023, Divya Mohan for dmj.one. All rights reserved.
*/
#include <stdio.h>
#include <time.h>
int main() {
time_t t;
srand((unsigned) time(&t));
int r = rand() % 100;
printf("%d\n", r);
return 0;
}
Output:
(random number between 0-99) 79Program 24
/* Miscellaneous Program by dmj.one
* DO NOT COPY DO NOT REPRODUCE DO NOT MODIFY
*
* (c) 2023, Divya Mohan for dmj.one. All rights reserved.
*/
#include <stdio.h>
int main()
{
int a = 5;
a = (++a) + (a++);
printf("a = %d\n", a);
return 0;
}
Output:
a = 11 / a = 13 / something else? You decide!!Program 25
/* Miscellaneous Program by dmj.one
* DO NOT COPY DO NOT REPRODUCE DO NOT MODIFY
*
* (c) 2023, Divya Mohan for dmj.one. All rights reserved.
*/
#include <stdio.h>
int main()
{
int a = 5;
a = (--a) + (a--);
printf("a = %d\n", a);
return 0;
}
Output:
a = 7 / a = 3 / something else? You decide!!Program 26
/* Miscellaneous Program by dmj.one
* DO NOT COPY DO NOT REPRODUCE DO NOT MODIFY
*
* (c) 2023, Divya Mohan for dmj.one. All rights reserved.
*/
#include <stdio.h>
#include <ctype.h>
int main() {
char c = 'a';
printf("%c\n", toupper(c));
return 0;
}
Output:
AProgram 27
/* Miscellaneous Program by dmj.one
* DO NOT COPY DO NOT REPRODUCE DO NOT MODIFY
*
* (c) 2023, Divya Mohan for dmj.one. All rights reserved.
*/
#include <stdio.h>
void func(int a[]) {
a[0] = a[1] + a[2];
a[1] = a[2] - a[0];
a[2] = a[0] * a[1];
}
int main() {
int a[] = {2, 3, 4};
func(a);
printf("%d %d %d\n", a[0], a[1], a[2]);
return 0;
}
Output:
a = 7 -3 -21 / a = 4 -1 -4 / a = 3 -1 -14 / something else? You decide!!Program 28
/* Miscellaneous Program by dmj.one
* DO NOT COPY DO NOT REPRODUCE DO NOT MODIFY
*
* (c) 2023, Divya Mohan for dmj.one. All rights reserved.
*/
#include <stdio.h>
#include <stdlib.h>
int main() {
int i = atoi("123");
printf("%d\n", i);
return 0;
}
Output:
123Program 29
/* Miscellaneous Program by dmj.one
* DO NOT COPY DO NOT REPRODUCE DO NOT MODIFY
*
* (c) 2023, Divya Mohan for dmj.one. All rights reserved.
*/
#include <stdio.h>
int main() {
int x = 0;
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
if (i == j) {
x++;
continue;
}
if (i < j) {
x += 2;
}
if (i > j) {
x -= 2;
}
}
}
printf("%d\n", x);
return 0;
}
Output:
0 / 4 / something else? You decide!!Program 30
/* Miscellaneous Program by dmj.one
* DO NOT COPY DO NOT REPRODUCE DO NOT MODIFY
*
* (c) 2023, Divya Mohan for dmj.one. All rights reserved.
*/
#include <stdio.h>
int main() {
int x = 5;
x = x << 2 + 1;
printf("%d\n", x);
return 0;
}
Output:
2Program 31
/* Miscellaneous Program by dmj.one
* DO NOT COPY DO NOT REPRODUCE DO NOT MODIFY
*
* (c) 2023, Divya Mohan for dmj.one. All rights reserved.
*/
#include <stdio.h>
int main() {
int x = 5;
int y = x++ + ++x + x--;
printf("%d\n", y);
return 0;
}
Output:
13 / 19 / something else? You decide!!Program 32
/* Miscellaneous Program by dmj.one
* DO NOT COPY DO NOT REPRODUCE DO NOT MODIFY
*
* (c) 2023, Divya Mohan for dmj.one. All rights reserved.
*/
#include <stdio.h>
int main() {
int x = 5;
int y = 2;
int z = x / y * y + x % y;
printf("%d\n", z);
return 0;
}
Output:
5Program 33
/* Miscellaneous Program by dmj.one
* DO NOT COPY DO NOT REPRODUCE DO NOT MODIFY
*
* (c) 2023, Divya Mohan for dmj.one. All rights reserved.
*/
#include <stdio.h>
int main() {
int x = 5;
x = x << 2 + 1;
printf("%d\n", x);
return 0;
}
Output:
21 / 40 / something else? You decide!!Program 34
/* Miscellaneous Program by dmj.one
* DO NOT COPY DO NOT REPRODUCE DO NOT MODIFY
*
* (c) 2023, Divya Mohan for dmj.one. All rights reserved.
*/
#include <stdio.h>
#include <stdlib.h>
int main() {
int x = 10;
int y = 20;
int z = (x > y) ? x : y;
printf("%d\n", z);
return 0;
}
Output:
20Program 35
/* Miscellaneous Program by dmj.one
* DO NOT COPY DO NOT REPRODUCE DO NOT MODIFY
*
* (c) 2023, Divya Mohan for dmj.one. All rights reserved.
*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
int a[100];
srand(time(NULL));
for (int i = 0; i < 100; i++) {
a[i] = rand() % 1000;
}
int max = a[0];
for (int i = 0; i < 100; i++) {
if (a[i] > max) {
max = a[i];
}
}
printf("%d\n", max);
return 0;
}
Output:
(random number between 0-999) 334Program 36
/* Miscellaneous Program by dmj.one
* DO NOT COPY DO NOT REPRODUCE DO NOT MODIFY
*
* (c) 2023, Divya Mohan for dmj.one. All rights reserved.
*/
#include <stdio.h>
int main() {
int x = 5;
int y = 2;
int z = x + y - (x = y);
printf("%d\n", z);
return 0;
}
Output:
3 / 5 / something else? You decide!!Program 37
/* Miscellaneous Program by dmj.one
* DO NOT COPY DO NOT REPRODUCE DO NOT MODIFY
*
* (c) 2023, Divya Mohan for dmj.one. All rights reserved.
*/
#include <stdio.h>
int main() {
int x = 5;
int y = 2;
int *p1 = &x;
int *p2 = &y;
int z = *p1 + *p2 - (*p1 = *p2);
printf("%d\n", z);
return 0;
}
Output:
5Program 38
/* Miscellaneous Program by dmj.one
* DO NOT COPY DO NOT REPRODUCE DO NOT MODIFY
*
* (c) 2023, Divya Mohan for dmj.one. All rights reserved.
*/
#include <stdio.h>
int main() {
int x = 5;
int y = 2;
int z = x++ + y - (x = y);
printf("%d\n", z);
return 0;
}