C Programming Exercises
10th Class Computer Science – Unit 02: User Interactions
Exercise 1
Arithmetic Operations and Operator Precedence
#include <stdio.h>
void main()
{
int x = 2, y = 3, z = 6;
int ans1, ans2, ans3;
ans1 = x / z * y;
ans2 = y + z / y * 2;
ans3 = z / x + x * y;
printf(“%d%d%d”, ans1, ans2, ans3);
}
Output:
079
Exercise 2
Escape Sequences and Special Characters
#include <stdio.h>
void main()
{
printf(” nn \\n\\n nnn \\nn \\nt \\t”);
printf(“nn /n/n nn /n\\n”);
}
Output:
nn
nnn
n
t nn/n/n nn /n
nnn
n
t nn/n/n nn /n
Exercise 3
Type Conversion (Float to Integer)
#include <stdio.h>
void main()
{
int a = 4, b;
float c = 2.3;
b = c * a;
printf(“%d”, b);
}
Output:
9
Exercise 4
Complex Arithmetic Expression
#include <stdio.h>
void main()
{
int a = 4 * 3 / (5 + 1) + 7 % 4;
printf(“%d”, a);
}
Output:
5
Exercise 5
Logical Operations (AND, OR)
#include <stdio.h>
void main()
{
printf(“%d”, ((((5 > 3) && (4 > 6)) || (7 > 3))));
}
Output:
1
Source: www.pakstudynotes.com
