0% found this document useful (0 votes)
9 views

LAB REPORT (PF)

The document is a lab manual for a Programming Fundamentals course, detailing various programming concepts and providing multiple C++ programming exercises. It covers topics such as high-level and low-level languages, algorithms, and includes sample programs for tasks like basic arithmetic operations, temperature conversion, and salary calculations. The manual is submitted by a student and includes structured lab exercises demonstrating practical coding applications.

Uploaded by

noorfatima59467
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)
9 views

LAB REPORT (PF)

The document is a lab manual for a Programming Fundamentals course, detailing various programming concepts and providing multiple C++ programming exercises. It covers topics such as high-level and low-level languages, algorithms, and includes sample programs for tasks like basic arithmetic operations, temperature conversion, and salary calculations. The manual is submitted by a student and includes structured lab exercises demonstrating practical coding applications.

Uploaded by

noorfatima59467
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/ 62

Programming Fundamentals

Lab Manual

Submitted by:
Noor Fatima
BS-DS-24-
Submitted to:
Ms. Fiza Khalil
Programming Fundamentals

Program:

A program is a set of instructions given to a computer to perform a specific task. It’s like a
recipe that tells the computer what steps to follow to achieve a certain result, such as adding
numbers, displaying a webpage, or sending an email. Programs can be written in different
programming languages like Python, Java, or C++, and they help automate tasks or solve
problems using computers.

High level language:


A high-level language is a programming language that abstracts away the complexities of the
computer's hardware, making it easier for developers to write, read, and maintain code. These
languages often use syntax that resembles human languages or mathematical notation,
allowing for clearer and more concise expression of ideas. Examples include Python, Java, C++,
and Ruby. High-level languages typically handle memory management, offer built-in data types,
and come with extensive libraries, making them more user-friendly compared to low-level
languages like assembly or machine code.

Low level language:


A low-level language is a type of programming language that provides little abstraction from a
computer's hardware. These languages are closely related to machine code and assembly
language, which means they allow for fine control over hardware resources.

Low level language is further divided into two types:

Assembly language:
Assembly language is a low-level programming language that provides a symbolic
representation of a computer's machine code. It acts as a bridge between high-level languages
and machine code, allowing programmers to write instructions in a more readable format than
binary.

Binary language:
Binary language, or machine code, is the most fundamental level of programming language that
computers understand. It consists solely of binary digits (0s and 1s), representing the electrical
states (on and off) of a computer's hardware.
Algorithms:
An algorithm is a step-by-step procedure or formula for solving a specific problem or
performing a task. It consists of a finite sequence of well-defined instructions that, when
followed, lead to a desired outcome. Algorithms can be expressed in various forms, including
natural language, pseudocode , flowcharts, and programming languages.

LAB#1
Program:

Write a program of C++ that prints “Hello World”.

#include <iostream>

using namespace std;

int main() {

cout<<"Hello World";

return 0;

}
LAB#2
Program:

Write a program that prints sum, subtraction and multiplication of two numbers
in C++.

#include <iostream>

using namespace std;

int main() {

int a=3;

int b=4;

int sum ,sub,mul;

sum=a+b;
sub=a-b;

mul=a*b;

cout<<sum <<"sum of both values"<<endl;

cout<<sub<<"sub of both values"<<endl;

cout<<mul<<"mul of both values"<<endl;

return 0;

}
LAB#3
Program:

Write a program which accepts two numbers and print their sum and average.

#include <iostream>

using namespace std;

int main(){

int a,b,sum;

float avg;

cin>>a;

cin>>b;

sum=a+b;

avg=sum/2;

cout<<sum<<endl;

cout<<avg<<endl;

return 0;

}
Program:

Write a program to swap the values of two variables.

#include <iostream>

using namespace std;

int main(){

int a=5,b=7,temp;

cout<<"before swapping"<<endl;

cout<<a<<endl;

cout<<b<<endl;

temp =a;

a=b;

b=temp;

cout<<"after swapping"<<endl;
cout<<a<<endl;

cout<<b<<endl;

return 0;

Program:

Write a program to calculate area of a circle.

#include <iostream>

using namespace std;

int main(){

float radius,area;

cout<<"enter the radius";


cin>>radius;

area= 3.14*radius*radius;

cout<<"area of circle with radius";

cout<<area;

return 0;

Program:

Write a program which accepts temperature in Fahrenheit and print it in


centigrade.

#include <iostream>

using namespace std;

int main(){

float centigrade;
float fahrenheit;

cout<<"Enter temp. in Fahrenheit: ";

cin>>fahrenheit;

cout<<endl;

centigrade = (fahrenheit - 32) * 5/9;

cout<<"Temp. in Celsius: "<<centigrade;

return 0;

Program:
Write a program which accepts a character and display its ASCII value.

#include <iostream>

using namespace std;

int main(){

char a;

cout << "Enter a character ";

cin >> a;

cout << "ASCII value " << a << int(a);

return 0;

Program:
Write a program which accepts days as integers and display total number of years
,months and days in it.

#include <iostream>

using namespace std;

int main(){

int days;

int years, months;

cin >> days;

years = days / 365;

months = (days % 365) / 30;

cout << years << " years " << months << " months " ;

return 0;

}
Program:
1.Write a program that reads and prints whether it is negative, zero or positive.

#include <iostream>

using namespace std;

int main(){

int num;

cout<<"enter a num";

cin>>num;

if(num>0)

cout<<"the num is positive";

else if (num<0)

cout<<"the num is negative";


else

cout<<"zero";

return 0;

2. Write a program to check whether the given number is even or odd.

#include<iostream>

using namespace std;

int main(){

int num;

cin>>num;

if(num%2)

cout<<"even";
else

cout<<"odd";

return 0;

3. Write a program to calculate the total expenses. Quantity and price per item and input by
the user and discount of 10% is offered if the expense is more then 5000.

#include<iostream>

using namespace std;

int main(){

int q,p,t,d;

cout<<"enter quantity";

cin>>q;
cout<<"enter price";

cin>>p;

t=p*q;

cout<<t;

if(t>5000){

d=(t*0.1);

t=t-d;

cout<<"total expense"<<endl;

return 0;

Program to calculate division of a number:…….


#include <iostream>

using namespace std;

int main(){

int sub1,sub2,sub3,sub4,sub5,sum;

float p;

cin>>sub1;

cin>>sub2;

cin>>sub3;

cin>>sub4;

cin>>sub5;

sum= sub1+sub2+sub3+sub4+sub5;

p= (float(sum)/500)*100;

cout<<sum<<"sum"<<endl;

cout<<"p"<<p<<endl;

if (p>=90){

cout<<"first division";

}else if (p<=89 && p>=80){

cout<<"second division";

}else if (p<=79 && p>= 70){

cout<<"third division";

}else if (p<=69 && p>=60){

cout<<"fouth division";

}else {

cout<<"fail";
}

return 0;}

C++ program that reads the temperature value and the unit (C for Celsius or F for
Fahrenheit), and then prints whether water is in a solid, liquid, or gas state at the
given temperature.

In this program:

 Water freezes at 0°C or 32°F (solid state).


 Water boils at 100°C or 212°F (gas state).
 Between these temperatures, water is in liquid state.

#include <iostream>
using namespace std;

int main() {
float temp;
char unit;

cout << "Enter the temperature value: ";


cin >> temp;
cout << "Enter the unit (C for Celsius, F for Fahrenheit): ";
cin >> unit;
if (unit == 'C' ) {
if (temp <= 0) {
cout << "Water is (Ice)" << endl;
} else if (temp >= 100) {
cout << "Water is (Steam)" << endl;
} else {
cout << "Water is liquid" << endl;
}
} else if (unit == 'F' ) {

if (temp <= 32) {


cout << "Water is solid (Ice)" << endl;
} else if (temp >= 212) {
cout << "Water is gas (Steam)" << endl;
} else {
cout << "Water is liquid" << endl;
}
} else {
cout << "Invalid unit entered. Please use 'C' for Celsius or 'F' for Fahrenheit." << endl;
}

return 0;
Program, the employes's salary is input find gross salary . if his basic salary is less then 15000
then hrs=10%of basics salary and daily allaowance=90% of basic salary . if his salary is equal
or above15000 then hrs=rs 500and da=98% of basic salary

#include <iostream>

using namespace std;

int main(){

float basicsalary,da,hra,grosssalary;

cout<<"basic salary of employe"<<endl;

cin>>basicsalary;

if(basicsalary<15000){

hra=0.10*basicsalary;

da=0.90*basicsalary;

}else{

hra=500;

da=0.98*basicsalary;

grosssalary=basicsalary+hra+da;

cout<<"gross-salary="<<grosssalary;

return 0;}
Write a program that reads amount and Check if amount is less than 1000. If not, then
print "program ends because amount is larger than 1000" but if yes, then Print" amount is
smaller than 1000 so please go into thisif/else" and inside this if else, check the followings:
Is amount less than 500 too, then print "stay in this if and go to its nested if and again
check, is amount less than 300 too, then print "oh! Ok, it's a small amount entered else, go
into one more if inside else part and check if amount is not equal to 1000, then print ""oh!
Ok, it's silently larger amount entered".

#include <iostream>

using namespace std;

int main()

int amount;

cout <<"Enter the amount: ";

cin >> amount;

if (amount < 1000)

{
cout <<"Amount is smaller than 1000 so please go into this if/else"<< endl;

if (amount < 500)

cout <<"Stay in this if"<< endl;

if (amount < 300)

cout <<"Oh! Ok, it's a small amount entered"<< endl;

else if (amount != 1000)

cout <<"Oh! Ok, it's a larger amount entered"<< endl;

else

cout <<"Program ends because amount is larger than 1000"<< endl;

return 0;

}
Task #1

Q 1 Write sum of number by for and while loop:

using namespace std;

int main(){

int sum=0;

for(int i=1;i<=10;i++){

sum+=i;

cout<<"sum"<<sum<<endl;
return 0;

Using While loop:

using namespace std;

int main(){

int sum=0,i=1;

while(i<=10){

sum+=i;

i++;

cout<<"sum"<<sum<<endl;

return 0;
}

Q 2 Write a program to find factorial of numbers given

using namespace std;

int main(){

int factorial=1,i=1,n;

cout<<"enter a number";

cin>>n;

while(i<=n){

factorial*=i;

i++;

cout<<"factorial"<<factorial<<endl;
return 0;

Using for loop:

using namespace std;

int main(){

int factorial=1,n;

cout<<"enter a number";

cin>>n;

for(int i=1;i<=n;i++){

factorial*=i;

cout<<"factorial"<<factorial<<endl;
return 0;

Q3 Write a program to find the value the value of one number raised
to power of another

using namespace std;

int main(){

int exponent ,base,result=1;

cout<<"enter base and exponont"<<endl;

cin>>base>>exponent;

while(exponent!=0){

result*=base;

--exponent;

cout<<result;

return 0;
}
 Initially: result = 1
 Iteration 1: result = 1 * 2 = 2, exponent = 2
 Iteration 2: result = 2 * 2 = 4, exponent = 1
 Iteration 3: result = 4 * 2 = 8, exponent = 0

Q4 Reverse of number:

using namespace std;

int main(){

int n, reversed =0,remainder;

cout<<"enter a number";

cin>>n;

for(int i=0;i<=n;i++){
remainder =n% 10;

reversed=reversed*10+remainder;

n/=10;

cout<< reversed<<"reversed number"<<endl;

return 0;

n/=10; is used as it removes last digit of n by dividing it from 10.


Q5 write a program to count even and odd numbers from a series of
numbers

using namespace std;

int main(){

int n, number, evennumber=0,oddnumber=0;

cout<<"enter the n";

cin>>n;

cout<<"enter the number";

for (int i=0;i<n;i++){

cin>>number;

if(number%2==0){

evennumber++;

}else{

oddnumber++;

}
cout<<"even number"<<evennumber<<endl;

cout<<"odd number"<<oddnumber<<endl;

return 0;

Q6 Write a program to find positive negative and zero in series of numbers


entered

#include <iostream>

using namespace std;

int main(){

int n,positive=0,negative=0,number;

cout<<"enter a number";

cin>>n;
cout<<"enter a number";

for(int i=0;i<n;i++){

cin>>number;

if(number >0){

positive++;

}else if

(number<0){

negative++;

}else{

cout<<"zero"<<endl;

cout<<positive<<"positive"<<endl;

cout<<negative<<"negative"<<endl;

return 0;

}
Write a programe to find armbstong of 1st three digits
int main() {

int number, originalNumber, remainder, result = 0;

cout << "Enter a number: ";

cin >> number;

originalNumber = number;

while (originalNumber != 0) {

remainder = originalNumber % 10; // Get the last digit

result += remainder * remainder * remainder; // Add the cube of the digit

originalNumber /= 10; // Remove the last digit


}

if (result == number)

cout << number << " is an Armstrong number." << endl;

else

cout << number << " is not an Armstrong number." << endl;

return 0;

Q 7 Write ambstrong from 1 to 500


#include<iostream>

#include<cmath>
using namespace std;

int main() {

int start=1,end=500;

for(int number=start;number<=end;number++){

int remainder,orignalnumber=number,result=0;

while (orignalnumber!=0){

remainder=orignalnumber%10;

result+=pow(remainder,3);

orignalnumber/=10;

if(result== number){

cout<<number<<endl;

cout<<endl;

return 0;

}
Task #2

:Q 8 Write a program that finds the smallest of several integers.


Assume that the first value specifies the number of values remaining
and that the first number is not one of the integers to compare.

using namespace std;

int main(){

int n,number,smallest;

cout<<"the numbers that are compared"<<endl;


cin>>n;

cout<<"enter first integer"<<endl;//the integer which we are comparing with

cin>>smallest;

for(int i=1;i<n;i++){

cout<<"enter the integer"<<endl;

cin>>number;

if(number<smallest){

smallest=number;

cout<<"smallest number is"<<smallest<<endl;

return 0;
}

:Q9 Write a program that sums a sequence of integers. Assume that


the first integer read specifies the number of values remaining to be
entered. Your program should read only one value per input
statement. A typical input sequence might be:

using namespace std;

int main(){

int n,number,sum =0;

cout<<"the number of digits that are summed"<<endl;


cin>>n;

for(int i=0;i<n;i++){

cout<<"enter the integer"<<endl;

cin>>number;

sum+=number;

cout<<"sum of numbers"<<sum<<endl;

return 0;

}
Q 10 write a program that calculates and prints the average of several
integers. The integers are terminated by the sentinel value 9999, which
should not be included in the calculation.

using namespace std;

int main(){

int number,sum =0,count=0;

float average;

cout<<"enter integers"<<endl;

while(true){
cin>>number;

if(number==999){

break;

sum+=number;

count++;}

average=sum/count;

cout << "The average of the entered numbers is: " << average << endl;

return 0;

}
Q 11 To meet the requirements of printing all numbers from 0 to 6
except 3 and 6 using a continue statement, here’s the program:

#include<iostream>

using namespace std;

int main(){

int n;

cout<<"enter integer";

cin>>n;

for(int i=0;i<n;i++){

if(i==3||i==6)

{continue; }

cout<<i<<"";

cout<<endl;

return 0;
}
Task#3

Q# 13 To implement the task of creating a histogram by printing


asterisks for five numbers (each between 1 and 30),

#include<iostream>

using namespace std;

int main(){

int number;

cout<<"enter integer"<<endl;

for(int i =0;i<5;i++){

cin>>number;

if(number<1 || number>30){

cout<<" the number is out of range out of 1 to 30"<<endl;


i--;

continue;

for(int j=0;j<number;j++){

cout<<"*";

cout<<endl;

return 0;

Q 14 Here's a program that generates the multiplication table for a


number N as described:
#include<iostream>

using namespace std;

int main(){

int n;

cout<<"number that needs to be multiplied";

cin>>n;

for(int i=1;i<=10;i++){

cout<<n<<"x"<<i<<"="<<i*n<<endl;

return 0;

Q 15 Write multiplication table of a number in backward


#include<iostream>

using namespace std;


int main(){

int n;

cout<<"number that needs to be multiplied";

cin>>n;

for(int i=10;i>=1;i--){

cout<<n<<"x"<<i<<"="<<i*n<<endl;

return 0;

}
Program:1

Output:

Program:2
Output:
Program:3
Output:

Program:6
Output:
Program # 7
Program:8
Output:

Program:9
Output:

Program:10
Output:

Program # 11
Program:13

Output:

Program # 14
Output:

Program:
Output:

programe
Output:

You might also like