-
Notifications
You must be signed in to change notification settings - Fork 0
/
week 2 in C
75 lines (55 loc) · 1.66 KB
/
week 2 in C
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<1> Sum and Difference of Two Numbers
Task
Your task is to take two numbers of int data type, two numbers of float data type as input and output their sum:
Declare variables: two of type int and two of type float.
Read lines of input from stdin (according to the sequence given in the 'Input Format' section below) and initialize your variables.
Use the and operator to perform the following operations:
Print the sum and difference of two int variable on a new line.
Print the sum and difference of two float variable rounded to one decimal place on a new line.
Input Format
The first line contains two integers.
The second line contains two floating point numbers.
Print the sum and difference of both integers separated by a space on the first line, and the sum and difference of both float (scaled to decimal place) separated by a space on the second line.
Sample Input
10 4
4.0 2.0
Sample Output
14 6
6.0 2.0
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main()
{
int a,b,sum=0,sub=0;
float c,d,s=0,su=0;
scanf("%d%d",&a,&b);
sum= a+b;
sub=a-b;
printf("%d %d\n",sum,sub);
scanf("%f%f",&c,&d);
s=c+d;
su=c-d;
printf("%0.1f %0.1f",s,su);
return 0;
}
<2> Maximum Number using ternary operator
Write a program to find the maximum number out of two integer numbers by using ternary operator.
Input Format
10 15
Output Format
15
Sample Input 0
10 15
Sample Output 0
15
#include <stdio.h>
int main()
{
int n1 = 5, n2 = 10, max;
max = (n1 > n2) ? n1 : n2;
printf("Largest number between %d and %d is %d. ",
n1, n2, max);
return 0;
}