Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
6 views

Programming Fundamentals Whole

Uploaded by

sajalay17
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Programming Fundamentals Whole

Uploaded by

sajalay17
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

PROGRAMMING FUNDAMENTALS

Structure programming theorem:


 Sequence…………. Arrangement
 Selection…………...decision (criteria)
 Repetition …………repeat, loop(multiple time occurrence)
Array: multiple values of same type in a variable
1D array: single row
2D array: row column
Function: Modules, (programs, of a single program, divided in small parts and solve separately)
Pseudo code:
 Pseudo code is a way to write programming ideas in a natural language style.
 It's not a real programming language, but rather a plan or outline.
 It uses simple words and structures to describe what a program should do.
 It's like a blueprint for a program, easy to read and understand.
Algorithm: way to solve a problem using set of instructions that will execute in a specified
manners.
Source code: human understandable,
showpoint : to show up the decimal part
setprecision(any number): control significance
floating display : significance of 6-digit
setw(any intgr): set width (formatting tool)
post increment : x++
ADD 1
pre increment : ++x

In equation :
Pre increment : first increament in value ) use
Post increment : first use ) increment

Bool flag:
Possible states two , 0 or 1, on or off, true or false
#include<iostream> # all the lines starting with # symbols are the preprocessed code in c++
#include<iomanip>
#include<string>
using namespace Include is the name of directive which will add,embed,include.the
int main() content of prewritten header file specified in the next
{
Int number;
Char grade=’A’;  iostream is the built in code from the c++ to input and output
cout<<”enter valid number”; data on console (CMD or DOS) or (it’s a path or pipe code and
cin>>number; code
cout<<”sizeof(int)”;  iomanip is also built in code from the c++ to format or
y=++number; manipulate the data
 string is built in code for string data input .
return 0;
} Cin/cout are input/output object. Console input/output
screen.
<< insertion operator >>exertion operator
; represent the end of line and it is necessary .
{} necessary for body

0: successful 1: unsuccessful

Return: inform about program


execution to OS

using namespace, int, return etc all


are the keywords and have some
special purpose and meaning and
cannot be used for other purpose
Escape Sequences in C++
Escape sequences are special character combinations in C++ that begin with a backslash
(\) and are used to represent various non-printable or control characters within string
literals and character constants. They play a crucial role in formatting output, including
newlines, tabs, and special characters
Non-printable Characters:
\a: Alert (bell) - Produces an audible or visual alert signal.
\b: Backspace - Moves the cursor one position back (without deleting a character).
\f: Form feed - Moves the cursor to the beginning of the next physical page (rarely used
nowadays).
\n: Newline - Moves the cursor to the beginning of the next line.
\r: Carriage return - Moves the cursor to the beginning of the current line.
\t: Horizontal tab - Moves the cursor to the next tab stop.
\v: Vertical tab - Moves the cursor to the next vertical tab stop
Special Characters within Strings:
\': Single quote (apostrophe) - To include a single quote within a string literal enclosed by single
quotes, use \'.
\": Double quote - To include a double quote within a string literal enclosed by double quotes, use\".
\\: Backslash - To include a backslash within a string literal, use \\.
Sequence
Solved sequence pdf:
Display sum of two numbers
INPUT num1, num2
sum = num1 + num2
PRINT sum
Input two numbers and add them
INPUT num1, num2
sum = num1 + num2
PRINT sum
Convert hours to minutes
INPUT hours
minutes = hours * 60
PRINT minutes
Convert weeks to days
INPUT weeks
days = weeks * 7
PRINT days
Convert yards to meters
INPUT yards
meters = yards * 0.9144
PRINT meters
Input two numbers and display sum, difference, product, and division
INPUT num1, num2
sum = num1 + num2
difference = num1 - num2
product = num1 * num2
division = num1 / num2
PRINT sum, difference, product, division
Convert Fahrenheit to Celsius
INPUT fahrenheit
celsius = (fahrenheit - 32) * 5/9
PRINT celsius
Initialize three variables and display z
x=5
y=3
z=x+y
PRINT z
Evaluate expression y = 3b
INPUT b
y=3*b
PRINT y
Input a number and display its square
INPUT x
y=x*x
PRINT y
Evaluate expression z = 3x + y
INPUT x, y
z=3*x+y
PRINT z

Evaluate expression z = (x + y) / 7
INPUT x, y
z = (x + y) / 7
PRINT z
Evaluate expression (3x + y) / (z + 2)
INPUT x, y, z
PRINT (3 * x + y) / (z + 2)
Evaluate expression (3x + z) / (y + 12)
INPUT x, y, z
PRINT (3 * x + z) / (y + 12)
Calculate and display Area of Rectangle
INPUT length, width
area = length * width
PRINT area
Input two variables, store sum in first and difference in second
INPUT num1, num2
num1 = num1 + num2
num2 = num1 - num2
PRINT num1, num2

Selection;
Solved selection pdf
1. High or Low Score:

if( score > 100 )


OUTPUT "High"
else
OUTPUT "Low"
end if
2. Negative or Non-Negative Number:
INPUT number
if( number < 0 )
OUTPUT "The number is negative"
else
OUTPUT "The number is non-negative"
end if
3. Division with Zero Check:
INPUT A, B
if (B = 0 )
OUTPUT "Division by Zero"
else
OUTPUT "C = " + (C - A / B)
end if
4. Valid Age for Driving License:
INPUT age
if (age >= 16 )
OUTPUT "Valid age for driving license"
else
OUTPUT "Age is not valid for driving license"
end if
5. Maximum of Two Positive Numbers:
INPUT num1, num2
if (num1 > num2 )
OUTPUT "Maximum: " + num1
else
OUTPUT "Maximum: " + num2
end if
6. Largest of Three Numbers:
INPUT A, B, C
if (A >= B && A >= C )
OUTPUT "Largest: " + A
elseif( B >= A && B >= C )
OUTPUT "Largest: " + B
else
OUTPUT "Largest: " + C
end if
7. Even or Odd Number:
INPUT number
if (number / 2 = 0 )
OUTPUT "Even"
else
OUTPUT "Odd"
end if
8. Multiple Check:
INPUT num1, num2
if (num2 % num1 = 0 )
OUTPUT "multiple"
else
OUTPUT "not"
end if

One-Way If Statements:
1. Absolute Value:
INPUT number
if ( number < 0 )
OUTPUT "Absolute value: ":
else
OUTPUT "Absolute value: "
end if
2. Number of Days in a Month
INPUT month
if (month = 1 OR month = 3 OR month = 5 OR month = 7 OR month = 8 OR month = 10 OR month = 12
)
OUTPUT "31 days"
elseif( month = 4 OR month = 6 OR month = 9 OR month = 11 )
OUTPUT "30 days"
else // February is not handled accurately here - Needs correction for leap year
OUTPUT "28 days"
end if
3. Single Digit to String:
INPUT digit
if (digit = 0 )
OUTPUT "zero"
elseif( (digit = 1 )
OUTPUT "one"
elseif( (digit = 2 )
OUTPUT "two" etc
end if
4. Point Location
INPUT x, y
if( x > 0 AND y > 0 )
OUTPUT "Point lies in First Quadrant"
elseif(( x < 0 AND y > 0 )
OUTPUT "Point lies in Second Quadrant"
elseif(( x < 0 AND y < 0 )
OUTPUT "Point lies in Third Quadrant"
elseif( (x > 0 AND y < 00)
OUTPUT "Point lies in Fourth Quadrant"
else
OUTPUT "Point lies on the origin"
end if

Conditional
1. In Range:
INPUT number
if (number >= 1 AND number <= 100 )
OUTPUT "In Range"
else
OUTPUT "Out of Range"
end if
2. Number of Digits:
INPUT number
if( number < 0 )
OUTPUT "Invalid Input: Enter a positive number"
elseif( (number < 10 )
OUTPUT "One digit"
elseif(( number < 100 )
OUTPUT "Two digits"
else
OUTPUT "Three digits"
end if
3. Temperature and Pressure Warning:
INPUT temperature, pressure
if( temperature >= 100 OR pressure >= 200)
OUTPUT "Warning"
else
OUTPUT "OK"
end if
4. Marks Grading:
INPUT marks
if( marks > 85)
OUTPUT "Excellent"
elseif( (marks >= 80 )
OUTPUT "Very Good"
elseif( (marks >= 75 )
OUTPUT "Good"
elseif( (marks >= 65 )
OUTPUT "Fair - You may not get the degree with such marks"
else
OUTPUT "Fail"
end if
Repetition;
Solved repetition pdf
Finding the sum of 10 numbers taken from the user:
sum = 0
count = 0
while count < 10:
num = ("Enter a number: ")
sum = sum + num
count = count + 1
print("Sum of 10 numbers is:", sum)
Finding the sum of n numbers taken from the user, where n is taken from the user as well
sum = 0
n = ("Enter the number of elements: ")
count = 0
while (count < n)
num = input number
sum = sum + num
count = count + 1
print("Sum of", n, "numbers is:", sum)
Calculate the factorial of a positive integer entered by the user:
factorial = 1
num = Enter a positive integer
while ( num > 0)
factorial = factorial * num
num = num - 1
print("Factorial is:", factorial)
Take two positive integers a and n from the user. Calculate and display a^n. Assume that
the power operator is not available.
result = 1
a = Enter a
n = Enter n
count = 0
while (count < n)
result = result * a
count = count + 1
print(a, "raised to the power", n, "is:", result)
Take three numbers from the user and determine the largest number. Do it using a loop.
Input max_num
count = 0
while (count < 3)
num = Enter a number
if (num > max_num)
max_num = num
count = count + 1
print("Largest number is:", max_num)
Take a positive integer from the user. Keep displaying an error message and prompting for
input again and again if the user enters invalid input (negative or zero).

num = Enter a positive integer


while (num <= 0)
print("Error: Please enter a positive integer.")
num = Enter a positive integer
Write an algorithm to determine the sum of a variable number of positive integers taken
from the user. The algorithm should keep prompting the user for more input till the user
enters the sentinel value -999.
sum = 0
while (True)
num = Enter a positive integer or -999 to quit:
if (num == -999)
break
elif (num > 0)
sum = sum + num
print("Sum of positive integers is:", sum)
Input a 2-digit number and find the absolute difference between its digits.
num = Enter a 2-digit number
abs_diff = abs((num / 10) - (num % 10))
print("Absolute difference between digits is:", abs_diff)
Input an integer (up to 4 digits), and store its reverse in another variable. Then display
both integers.
num = Enter an integer (up to 4 digits
reverse = 0
temp = num
while (temp > 0)
digit = temp % 10
reverse = reverse * 10 + digit
temp = temp / 10
print("Original number:", num)
print("Reversed number:", reverse)
Input numbers until the user inputs zero, and at the end, display the last non-zero number.
last_non_zero = None
while (True)
num = Enter a number (0 to stop
if (num != 0)
last_non_zero = num
else:
break
print("Last non-zero number entered:", last_non_zero)
Input numbers until the user inputs zero, and display the largest number.
max_num = None
while( True)
num = Enter a number (0 to stop)
if ( num == 0)
break
if ( num > max_num)
max_num = num
print("Largest number entered:", max_num)
Input numbers until the user inputs zero, and display the smallest number.
min_num = None
while (True)
num = Enter a number
if (num == 0)
break
if (num < min_num)
min_num = num
print("Smallest number entered:", min_num)
Input 10 numbers, and display the smallest number
min_num = None
count = 0
while( count < 10)
num = input("Enter a number: ")
if (num < min_num)
min_num = num
count = count + 1
print("Smallest number is:", min_num)
Input 10 numbers, and display the count of Even and Odd numbers, separately, at the end.
even_count = 0
odd_count = 0
count = 0
while (count < 10)
num = input("Enter a number: ")
if (num % 2 == 0)
even_count = even_count + 1
else:
odd_count = odd_count + 1
count = count + 1
print("Count of even numbers:", even_count)
print("Count of odd numbers:", odd_count)
Input SLimit and ELimit from the user, and display even numbers between the range, with
both limits included.
SLimit = input("Enter the start limit: ")
ELimit = input("Enter the end limit: ")
if (SLimit % 2 == 1)
SLimit = SLimit + 1
while (SLimit <= ELimit)
print(SLimit)
SLimit = SLimit + 2 # Increment by 2 to get to the next even number

You might also like