Cin One Shot Part 1

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 45

C in One Shot

Part - 1
Flow - 1
1) Basic Printing, \n wali cheez, printing numbers also with + and -.
2) Variables, printing variables, int, float, and +,-,*,/ of integers.
3) Variables naming rules.
4) Comments
5) Taking Input
6) Modulus Operator
7) Float to int, int to float
8) Hierarchy
9) Char and ASCII
Basic program in C

#include<stdio.h>

int main(){

printf(“hello world”);
return 0;
}
How to move in next line?

Example :

printf(“Hello PW”);

printf(“Hello CW”);

Output will be :

Hello PWHello CW
Use of escape sequence ‘\n’

Example :

printf(“Hello PW”);

printf(“\n”);

printf(“Hello CW”);

Output will be :

Hello PW
Hello CW
Use of escape sequence ‘\n’
Output
Predict the output :

f

n n

main(){ •

printf("nn\n\nnn\n"); • n n

printf("nn/n/nnn/n");
• nn / n / /
nnn n

} # in .ua , . n,
.

int main C) {

return 0
;
3
Variables and their Declaration
Let us focus on int data type as of now. Variables Literals
t
1) Variables as containers : '
In
'

my
'
< station >
'
# include %d

"n" )
int main / I
printfl ;
4 ↳ text
"

( Hello )
"
printf ;

pnintfl "%d
"

intro ; - ,n );

n =3;

n = 5;

printfl "%d
"

w
,n )
Printing Variables in C & Updation of Variables
→ int n "
Output
@

I
int x = 5; n=S ;
5713-7
printf(“%d”,x); -

x = 7;
printf(“%d”,x);
x = x + 6;
printf(“%d”,x);
x = x - 20; → n = 13-20

-7
printf(“%d”,x); n =
Arithmetic operations on int data type
' '
' '

+
int x = 5;
-

% d int
int y = 2; →

2f§-
printf(“%d”,x+y);
5/2 = 2

printf(“%d”,x-y);

printf(“%d”,x*y); int n = 4
;
n

int 2
printf(“%d”,x/y); y =
;
w

int z =
n+y ;
y

u
:
5/2 2.5

;]

int n = 512

5/2 → 2
Increment - Decrement operators
int x = 5;
x/+;
printf(“%d”,x);
x/-;
printf(“%d”,x);
/+x;
printf(“%d”,x);
–-x;
printf(“%d”,x);
Float data type
→ Numbers
Real God → int
float x = 3.1;
%f → float
Arithmetic operations on float data type
float x = 5;

float y = 2; 512 2

integer →

printf(“%f”,x+y); → 7
float 2 =
5/2 ;
printf(“%f”,x-y); 3
float 2 = 2 ;

printf(“%f”,x*y); 10
5.0/2 → 2^5

printf(“%f”,x/y); 2. 5
512.0 → 2.5

float n
;
5
n=
; n
V
4-311-83
=

/3
* * *
✓ = 4 3.14 r
*
r r

int r = 5
±

4*3.14*5 /
*
s 5 3
float v =

ttw / c. w WAP to
display area
of circle with given
-

. .

radius .
Example : Calculating percentage of 5 subjects
float x1 = 90; // x1 can be physics
float x2 = 91; // x2 can be chemistry
float x3 = 92; // x3 can be maths
float x4 = 93; // x4 can be english
float x5 = 94; // ohh wait comments ke baare me to bataya hi nahi xD
float percent = (x1 + x2 + x3 + x4 + x5)/5;
printf(“%f”,percent);
// change the marks and run each time

4 where
t.IN
.
Print percentage of Subjects marks are out of 40 .
Example : Calculating Area of a Circle Flow .

float radius = 5; float r = s;

float pi = 3.1415;
int n int y int z

float area = pi*radius*radius; ,

printf(“%f”,area); int a
Variable Naming rules
1) Variables can start from an alphabet or underscore _ .
2) Special characters except _ are not allowed.
3) Some particular keywords are not allowed.
4) Commas or blanks are not allowed.
to
Auto double int break extern enum unsigned while
case sizeof for const static long continue float
else signed do short switch char volatile default
goto struct if union return void register typedef

float Macbook ;
int Mac
;
Variable Naming rules - Examples
Q. Which of the following are invalid variable names and why?

BASICSALARY _basic basic-hra

#MEAN group. 422

population in 2006 over time mindovermatter

FLOAT hELLO queue.

team’svictory Plot#3 2015_DDay


Example : Calculating Simple Interest
float p,r,t,si;
,R
SI = P
p = 100;
r = 10;

1524601¥ 3% 30-4
t = 2;
=

si = (p*r*t)/100;
printf(“%f”,si);
Taking input // Let us take a simple example
from User .

int x;

printf(”Enter a number\n”);

scanf(“%d”,&x); // user will give ‘x’ a value.

int y = x*x;

printf(”square of number that you gave is %d”,y);


Run

10

circle 314 IS
The area
of is :
-

y
DryRu pitta
f
radius
µ The area
of circle is : 314.1s

pi area
Output :

Enter Principal : 100

Enter Rate : 20

Enter Time : 3

60
simple interest is :
Your

"

scanfl "%d ,
& a)

③ ④
principal rate time Si
int n = 5; float y = 6.1 ;

El y

4ns address of n

int n

scan fl "%d
;
"

,
en ) ;
µ
Taking input // SUM of 2 given numbers Homeworld :

int x; by output

[
printf(”Enter first number\n”); "" " ""

scanf(“%d”,&x); // user will give ‘x’ a value. En -


- -

int y;
printf(”Enter second number\n”);
scanf(“%d”,&y); // user will give ‘y’ a value.
int sum = x+y;
printf(”sum of the numbers that you gave is %d”,sum);

au
sum
Taking Input
Output
③ & ""
¥
"""
Predict the output :
"

30 40
main(){ 2
p 30 40
q=
=

int p,q;
printf("Enter values of p and q");
scanf(" %d %d ",&p,&q);
printf ("p = %d q =%d", p, q);
}
'
Run
'

Dry
Example : Take two integers input, a and b : a>b, and find the
remainder when a is divided by b. & print the remainder .

main C) E
int a
,
b
; 713% 7×5+4 39
pnintfl Enter 1st
"
" No ) =

3⑧
.
;
"

f (
scan
"
%d ,
& a) ;
2ⁿᵈ No )
"

pñntf / Enter
" . ;

" "
& b) i int a = 39 ;
scant ( Yod ,

int b = 7;
int
q % ;
=

int
q = a / b;
Hq = 5
int r = a -
( b*q ) ;

Dinis or ✗ Quotient + Rem = Dividend


return 0;
Remainder Dividend -
Divisor ✗ Quotient
3
=

8 = a -
( bxql
0b¥ :

+
,
-
,
/ ,
*
%
3- = 0

%
MMr→
38%8=6
r= a % b
a=2 ;

b = 6;
y

813T
]
[
3%
2
2 % n = 2 n >

www.tab-a [a <
b)
float to int and int to float :

Ques : Take integer as input and print half of the


number.
a positive

Ques : Take float input and print the fractional part


of the real number. 123 2 (n] = -

1, 2, 3, 4, 5, 6
13.14" 0.14

& I. F. 15.7' = St0.7


0.7
->
(nT 22 = 7./

C2 ]= =
=
is line No.
float a;
out
W
-

I
scant ("%of", On); 07

int
;

y
=
ch;
W
floats-as;
a
float bint W
z

Homework:
-
Code it yourself
~
Hierarchy of operators BOGMAS ->

B,0,01M, Als
int i = 2 * 3 / 4 + 4 / 4 + 8 - 2 + 5 / 8 ;
A
printf(“%d”,i);
I' s ↳ I a MATHS
12 43) 1x 619: 1
= >

PROG
=

(2x33K =
619= 105
*
<2 3) 1= 614 = 2
2x131) =
2 x 0.73= 1.5

& "(3 ) = 2*0 =


0

1 1
=
+ 1 +8-2 + 0 =
8


Try This!

I
W

Predict the output : EB WR

West
int main(){

No
too.
- int i = 2, j = 3, k, l;
2 o.000000
float a, b; a

k =*
aO 1
i /213)
j * j ; 30*3 =
=
O
⑧ l = Cj
⑧ / iI * i; B/2) *2=
=
12 = 2

28 O
a = (i / jS * j ; (213) 3= 0
*
3 = O
db = Cj / i I * i; = 2
0.000
printf("%d %d %f %f", k, l, a, b);
-
}
Mea

&
A

Try This! Output

I
Groc
a 5.000000
o
=

-S
Predict the output : b = 2.000000 X

int main(){
float a = 5, b = 2;
W
a

int c;
2X
c = a % b;
printf("%d", c);
}
out =>
+, -, *, 1,%
float ++, -, ,
char data type int, float
G -

C finteger
-> % a real number/decimal
char ch = ‘a’;
- variable d
lot

⑭ dod ->

format
a
specifies
An
charch= '#'
char a=
'a";
->

" bi's ... 23 W


Ch
Cz"
A, B','i, . . .
ASCII values ->
Important topic
-
good questions
char ch = ‘a’;

*65 ⑳
az # - 33

B b 98 ! 33

S
-> 6 f = -

c -> 67 c = 99 f f

- 68
* ·

d = 100
E-69 -

F
- 7O

G + 7 I 2 = 12] ⑧ - OU

* - 42

= 90 I
z
-
3 36
*
Else

MCQ Time !
MCQ 1
character ->
single digit
a b"It'
charch='a+;
Which of the following is NOT a character constant
(1) ‘Thank You’ WRONG

(2) ‘Enter values of P, N, R’


(3) ‘23.56E-03’
I I

-
(4) All the above
MCQ 2
In (b = 6.6 / a + 2 * n ;Iwhich operation will be performed first?
(1) 6.6 / a
-
(2) a + 2
(3) 2 * n
(4) Depends upon compiler
MCQ 3
Which of the following statements is false
-
(1) Each new C instruction has to be written on a separate line

(2) Usually all C statements are entered in small case letters

(3) Blank spaces may be inserted between two words in a C statement

(4) Blank spaces cannot be inserted within a variable name

int n = 4;

22 = 1 + 7 ;
MCQ 4
IfO
a is an integer variable, a = 5 / 2 ; will return a value
(1) 2.5

(2) 3
int
a =
5/2;

-
(3) 2

(4) 0
MCQ 5 O,DM AS
3

The expression, a = 7 / 22 * ( 3.14 + 2 ) * 3 / 5 ; evaluates to


G
(1) 8.28

(2) 6.28
2 122 5.14 *3/S

(3) 3.14 8 *S.14 *3/S

-
(4) 0 ⑧*3/S
8 IS


8
MCQ 6
int

The expression, a = 30 * 1000 + 2768 ; evaluates to
-
(1) 32768 30000 + 2768

(2) -32768 a = 32768

(3) 113040

(4) 0 Short a 32768;


=


-
3276 8
G
2 2102
=

Types
*
1824 > 64
-
Chhote dabba
But vs Short
=

r
iut z = 3,
d
float y =
3.M;

Charch: 'Al
-
mom
3ES7, 8 to

short int short bytes 16 bits


2= ->
->
->

long n =
32768; 8 Bits-I byte
bits 2"numbers
long long n -
z =

16 bits + 2 numbers store

b R6 Mb Gb Th d
I6
v a v 2 = 85536
1000 1000 1000 1000
+32 bits
int data type -><
bytes
->
232 numbers
d
long> 8 bytes -> 64 bits

23" numbers

short -z' to 23-I


int -
23 to 23-I

long long - 263 to 23-I

char ->< byte -> 8 bits - 28 numbers 256 numbers


=
*
BODMAS
MCQ 7 ->% I, +,

The expression x = 4 + 2 % - 8 evaluates to


(1) -6 m
=> = 4 + 2 = 6
50 2

-
(2) 6 2% 8= 2 ↳
2
W
(3) 4 2T5

(4) None of the above


↳ TE
I
8 I

2 0-8 = 208 = 2
MCQ 8
What will be the value of d if d is a float after the operation
d = 2 / 7.0?
float d =
217.0 j
(1) 0

stressin
(2) 0.2857

(3) Cannot be determined

-
(4) None of the above
~

4 O

3
o
u

You might also like