0% found this document useful (0 votes)
19 views20 pages

Psa Notes

problem solving assesment 1st year btech notes
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views20 pages

Psa Notes

problem solving assesment 1st year btech notes
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 20

12:16 12-12-2023

FREQUENCY
start n=0
{
for (i=0,i<=n,i++)
{
print "PSIT" N+1
FOR(J=0, J<=N , J++) n^2 + 3n+2
Print "WORLD" n^2+2n+1
}
}
---------------------
2n^2+7n+6

---------------------

(2)
start
{
for(i=0,i<=n, i++) n+2
{
print"PSIT" n+1
for(j=1, j<m, j++) (n+1)m = nm+m
Print"WORLD" (n+1)(m-1) = nm+m-n-1
}
}
---------------------
2nm+2m+n+2

---------------------
big[O] = n^2 or m^2

(3)
start
{
for(i=1,i<n, i+3 )
NOTE: PROVIDED n IS DIVISBLE BY 3
print "PSIT"
}

(4)
start
Value for i is true = 0,03,6,9,12 so n=9
{
for(i=0,i<=n, i+3 ) n/3+2
print "PSIT" n/3+1
}
---------------------
2(n/3)+3
---------------------
(5)
start
NOTE: THIS IS INFINITE LOOP
{
for(i=0,i<=n, i*2 )
print "PSIT"
} ---------------------

---------------------

(6)
start
i=(1,2,)t, (4 )f ; n=2 i n
{
i=(1,2,4,)t ; (8)f ;n=4 2^1 2
for(i=1,i<=n, i*2 ) (k+1)+1
i=(1,2,4,8)t;(16)f ;n=8 2^2 4
print "PSIT"
. .
} ---------------------
(k+1)+1 . .
(log (base 2) n )+2
-------------------------------------------------------------
---------------------
2^k n n=2^k
BIG[O]=log (base 2) n
[ k= log (base 2) n]

(7)
start
{
for(i=1,i<n, i*2 ) log (base 2) n +1
print "PSIT" log (base 2) n
}
----------------------------
f(n)= 2log (base 2) n+1

----------------------------
BIG[O]=log (base 2) n

(8)
start
{
for(i=1,i<n, i*3 ) log (base 3) n +1
print "PSIT" log (base 2) n
}
---------------------------------------
f(n)= 2log (base 3) n+1

----------------------------------------
BIG[O]=log (base 3) n

Write a program to find the sum of 'n' natural number.

REVISION OF OPERATORS

1)Assignment
2)Arithmetic
i) +
ii) –
iii) *
iv) /
v) %(modulus operator )
3)Increment and decrement
i) ++
ii) --
4)Relational
a) <
b) >
c) >=
d)<=
e) ==
f) !=
5)Logical(gates)
a) &&(and)
b) ||(or)
c) !(not)
6)Bitwise
a) &(and)
b) |(or)
c) ^(xor)
d)~(not)
e) <<(left shift)
f) >>(right shift)
7)Other
a) ?(conditional operator)
b) Sizeof( )
c) ,(comma)…………………………………etc

How to solve
i. First solve pre
ii. Then expression
iii. Then post
Logical operator
&&truth table

A B A&&b
0 0 0
0 1 1
1 0 1
1 0 1

Ex

Main()

Int a= 37 b=52 c=23 d, e ,f

D= a&b;

E=a|c

F= a^b;

Printf(“d=%d, e=%d”,d ,e f);

}
<<LEFT SHIFT
Syntax: value<<position
Eg: int a 23<<2
Ans: a=92
0 0 0 0 0 0 0 0 0 0 0 0 10 11

1st msb ia a sign bit that is a zero


1st lsb is a one
>> RIGHT SHIFT

SYNTAX

VALUE>>Position

Eg: int a = 23>>2

Ans: a=5

0 0 0 0 0 0 0 0 0 0 0 1 0 1 1 1
Msb gives (-ve\+ve)

Date – 26/12/2023

~(BITWISE NOT)

 GIVE 1’S COMPLIMENT


 UNARY OPERATOR, TAKES ONE OPERAND
 EG: int a
A~23

A= - 24

FOR taking the bitwise not of eg -17 , the answer will


be considered the following pattern decrement with
positive sign

0 0 0 0 0 0 0 0 0 0 0 1 0 1 1 1
(For binary magnitude)

1 1 1 1 1 1 1 1 1 1 1 0 1 0 0 0

Take 15 bits 2‘s complement to know the normal number

111111111101000

1’s complement – 000000000010111 +1

2’s complement – 000000000011000

= 24

#REPRESENTATION OF NEGATIVE NUMBER

(X)1 BYTE – (16 BIT)


0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
(Y)[-1](2 BYTE ) -11111111111111111(2’s cpmplemnt)

2 (binary)- 0000000000000010

1‘s complement – 1111111111111101 +1

2’s complement - 111111111111110

Ques: (1111111100101101 )2 – (-211.)10

lsb is a sign bit

taking 15 bit except the sign bit : 111111100101101

1’s complement: 000000011010010 + 1

2’s complement : 0000000011010011

now converting the number into decimal number system ANS(-211)

QUES: main()

int a ,b ;

a~ =23

B=~-7810

printf(a=%d, b=%d”,a,b)}

ans:
a = -24 ; b= +7809

{+ve ho to ek no increase krke sign change and –ve ho to decrease karke sign change }

#Branching statement

 conditional branching

 looping

date- 29/12/2023

program- count 0

PSEUDO CODE :

1. START
2. INPUT:N
3. INTILIAZE COUNT FROM 0

#jump statement in c
1. Break
2. Continue
3. Go to
4. Return
# break statement
 Jumpstatement- used within switch or within
loop body
 Whenever break statement is encountered within
loop body will be terminated to out at that line
will be transferred to out of the nearest and
closing loop.
 Syntax-
While(……)
{
1. ….
2. …..
3. …
4. If(condition)
break
5. …
6. …

IT IS JUMPM STATEMENT

Syntax –
While(…)

1. {
2. ..
3. .
4. .
5. .
6. .

8.

9.

10…

#go to statement

 Jump statement
 Goto statement jump the control to a specified label
 Label is an identifier (name) which specifies the phase
where jump is to be done

Syntax:

1. __________________
2. _____________________ [backwardjump]
3. __________________
4. ___________
5. If(condition)
6. Goto label ;
7. ____________
8. ____________

1. ___________________
2. ______________
3. ______________
4. If(condition)

Ques: find factorial if a no using go to function.

Int main()

Int n,I,fact;

Printf(“enter the no.”);

Scanf(“%d,&n);

I=1;
Fact=1;

X:

Fact=fact*1;

I++;

If(i<=n)

Goto x ;

Printf(“%d, fact),

Return 0;

9/01/2024

#perfect number

If sum of positive divisor of the given number (excluding


itself)= original number

Eg: num=6; +ve divisor of 6 = 1,2,3,6*

= 1+2+3=6

So, 6 is a perfect number,

Ques: write the pseudo code and flowchart whether no. is


perfect number or not ?

ans:
1. start
2. Use for loop
If i<n; i++
If(n%i==0)
Sum=sum+I;
Print that the number entered is entered is a
perfect number

Date :

1. FUNCTION: It is a self-contained independent block of


code which perform specific task when it is called.
If required it may receive some input (actual
parameter/data) process that data or perform some
logic and then function may or may not return some
resulting value to it calling function.

Int main()

{ CALLING FUNCTION
Int n= 5, a;
Formal
a=factorial(n);
Called parameter
printf(“%d,a);
function
return 0;
Int factorial (int n )

Int I , f=1;

for (i=1; i<=n; i++;

f=j*I;

Return 0;
FUNCTION

User defined
Library or switching function
function

 These are declared in respective header file(prototype)


 They are defined (body) in all files (dynamic link and librarian)
Ex: printf(), scanf()…………..

ADVANTAGES OF USER DEFINED FUNCTION

1) Provide modular programming approach


2) Easy debugging
3) Code- reuse ability
4) Enhance the readability of code
5) Easy scalable(upgrade)

TERMINOLOGY OF USER DEFINED FUNCTION

1) Function declaration(prototype)
2) Function definition(body or logic)
3) Function – call(use- point)

 FUNCTION DECLARATION
 Function header
i) Return type
ii) Function name
iii) Argument
 Semi-colon (;)

Return type

Ex- int fun(int n); argument


Function name

 FUNCTION DEFINITION
Int function name( type, arg 1, type, )
{
Int a , b, sum = 0
.
.
.
. Body
.
Return 0 ;
}

 Function call
Syntax
o Write a program to divide a number into sum of two prime number print all the possibilities……

#include<stdio.h>
#include<math.h>
Int prime(int n)
{
Int i;
For(i=2; i<=sqrt(n); i++)
{
If (n%i==0)
Return 0;
}
Return1;

Int main()
{
Int a ,b,i;
Printf(enter the range );

Home assignment

o Draw a pascal’s triangle


STRONG NUMBER
If sum of factorial of each digit of the given number = original number

Eg: 145 = sum


1!+2!+3!=145 Hence it is a strong number

Pseudo code
1 Start
2 Input: num
3 Copy n num
4 Initialize sum 0
5 While (num!=0)

Do

D num%10
F last factorial d)
Sum sum =!f
Num sum/10

Endloop
6 Check if (sum==n)

Print (“strong no”)

7 Else
Print(“not a strong no”);

RECURSION
IT is a top down approach. It is based on a divide and conquer
approach in which a big problem will be broken into similar type
of problem but in smaller size until and unless we reach the base
case. Once we reaches the base case then solution are getting
combined and then back to the previous stage and the finally we
got the solution of our original problems. Every recursive solution
consist of two cases:

CASE1: base case: it is the terminating condition of recursion i.e.


recursion will stop at this stage. More than one base case are
possible in a recursive solution. If there is no base case then it
became infinite recursion.

CASE 2: in this case function calls by passing some reduced data


every time.
NOTE: RECURSION IS COSTLY PROCESS I.E. RECURSIVE SOLUTION REQUIRES MORE
TIME AND SPACE THAN ITERATIVE ,IN INSPITE OF THESE LIMTATIONS BUT WE OFTEN
USE RECURSIVE SOLUTION BECAUSE IT IS VERY SIMPLE AND EASY THEN ITERATIVE
SOLUTION . SPECIFICALLY IN SOME COMPLEX PROBLEM.
FACTORIAL

Types of recursion
1 Direct recursion: when a function calls itself within its body
2 Indirect recursion: when function calls itself again through
another function.
3 Non- tail recursion: if there are some pending task which will be
evaluated after the return of recursive call of the function.
4 Tail recursive: if there is no pending task, after the return of
recursive call of the function.
1. Direct recursion.
Eg int fun (int n)
{
If(n==0)
Return 1;
Else
{
Int a = function(n/2);
Int s = a+n;
Return s;
}
}

INDIRECT FUNCTION
 C – MEMORY LAYOUT
Date – 16 February 2024

Searching technique
1. Linear search
2. Binary search
 Linear search
Start
Input n
Input array : arr
Input : key
Initialize found
While ( i<n)
If (arr [i]== key)
Print: ”key found at index =”i
found 1
break the loop
I i+1
If(found==0)
Print: ”key is not found”
End

Time complexity
1. Best case

You might also like