0% found this document useful (0 votes)
67 views17 pages

PF Assignment No. 1,2,3

The document contains assignments submitted by Abdul Muizz for his BS in Robotics program. Assignment 1 contains 6 questions requiring the use of basic C++ programming concepts like conditional statements, loops, functions etc. to solve problems related to grading systems, determining oldest/youngest among three people, and calculating area of a circle. Assignment 2 contains 8 questions focusing on the use of for/while loops to print patterns, find sums, and display ASCII characters with their values. The assignments aim to develop fundamental C++ programming skills.

Uploaded by

Abdul Muiz
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)
67 views17 pages

PF Assignment No. 1,2,3

The document contains assignments submitted by Abdul Muizz for his BS in Robotics program. Assignment 1 contains 6 questions requiring the use of basic C++ programming concepts like conditional statements, loops, functions etc. to solve problems related to grading systems, determining oldest/youngest among three people, and calculating area of a circle. Assignment 2 contains 8 questions focusing on the use of for/while loops to print patterns, find sums, and display ASCII characters with their values. The assignments aim to develop fundamental C++ programming skills.

Uploaded by

Abdul Muiz
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/ 17

SUPERIOR UNIVERSITY

ASSIGNMENT NO. 1,2,3


PROGRAMING FUNDANETAL

Academic Year: 2022 – 2026


BS ROBOTICS

Abdul Muizz | Enrollment No.


PF / SIR AHMED BILAL
Date of Submission: March 15, 20XX
Assignment 1

Question #1: Write a C++ program that ask user to enter his age then using following rules print their place of
service. 1. if employee age is in between 20 to 40 then he may work in anywhere 2. if employee age is in between
40 t0 60 then he will work in urban areas only. 3. And any other input of age should print "ERROR".

Code:

#include <iostream>

using namespace std;

int main() {

int age; // declare an integer variable age to store the user's age

cout << "Enter your age: ";

cin >> age;

// check if the age is between 20 and 40, inclusive

if (age >= 20 && age <= 40)

{ cout << "You may work anywhere" << endl; }

// check if the age is between 40 and 60, inclusive

else if (age > 40 && age <= 60)

{ cout << "You can only work in urban areas" << endl; }

else

{ cout << "ERROR" << endl; }

return 0; }

Screenshot (Question #1):


Question #2: Write a C++ program that ask user to enter marks and print the corresponding grade. THE SUPERIOR
UNIVERSITY LAHORE (Faisalabad Campus) Assignment_01 (BSSE/BSIT) Faculty of Computer Science and
Information Technology Subject: Programming Fundamentals Instructor: Mr. Ch. Ahmad Bilal Total marks: 30
Instructor: Muhammad Hassan Total Marks: 50 Using following rules for grading system? a. Below 25 - F b. 25 to
45 - E c. 45 to 50 - D d. 50 to 60 - C e. 60 to 80 - B f. Above 80 - A Ask user to enter marks and print the
corresponding grade.

Code:

#include <iostream>

using namespace std;

int main() {

int marks;

cout << "Enter your marks: ";

cin >> marks;

// check if the marks fall within a certain range

if (marks < 25)

{ cout << "Grade: F" << endl; }

else if (marks >= 25 && marks < 45)

{ cout << "Grade: E" << endl; }

else if (marks >= 45 && marks <= 50)

{ cout << "Grade: D" << endl; }

else if (marks > 50 && marks <= 60)

{ cout << "Grade: C" << endl; }

else if (marks > 60 && marks <= 80)

{ cout << "Grade: B" << endl; }

else if (marks > 80)

{ cout << "Grade: A" << endl; }

else

{ cout << "Invalid marks entered." << endl; }

return 0;

}
Screenshot (Question #2):
Question #3: Write a C++ program that input the age of three peoples and determine oldest and youngest among
them?

Code:

#include <iostream>

using namespace std;

int main() {

int age1, age2, age3;

// Prompt the user to enter the age of each person.

cout << "Enter the age of first person: ";

cin >> age1;

cout << "Enter the age of second person: ";

cin >> age2;

cout << "Enter the age of third person: ";

cin >> age3;

// Determine the oldest person.

if (age1 >= age2 && age1 >= age3)

{ cout << "The oldest person is " << age1 << " years old." << endl; }

else if (age2 >= age1 && age2 >= age3)

{ cout << "The oldest person is " << age2 << " years old." << endl; }

else

{ cout << "The oldest person is " << age3 << " years old." << endl; }

// Determine the youngest person.

if (age1 <= age2 && age1 <= age3)

{ cout << "The youngest person is " << age1 << " years old." << endl; }

else if (age2 <= age1 && age2 <= age3)

{ cout << "The youngest person is " << age2 << " years old." << endl; }

else

{ cout << "The youngest person is " << age3 << " years old." << endl; }

return 0;

}
Screenshot (Question #3):
Question #4: Write a C++ program to calculate area of the circle if the radius is greater than 0 using goto
statement.?

Code:

#include <iostream>

#include <cmath>

using namespace std;

int main()

{ double radius, area;

input:

cout << "Enter the radius of the circle: "; // Prompt the user to enter the radius

cin >> radius; // Read the radius entered by the user

if(radius <= 0) // Check if the radius is valid

{ cout << "Radius should be greater than 0." << endl;

goto input; }

area = M_PI * pow(radius, 2); // Calculate the area of the circle using the formula

cout << "The area of the circle is " << area << endl;

return 0; }

Screenshot (Question #4):


Question #5: Write a C++ program in which a user enters a number, and the program will find the Even and odd
number. Your program should only accept integers of length 2 or higher and should ask the user to re-enter a
value if the value containing total numbers of integers less than 2. The program with goto statement?

Code:

#include <iostream>

using namespace std;

int main()

{ int num;

input:

cout << "Enter a number (must be at least 2 digits long): ";

cin >> num;

if(num < 10)

// Check if the number entered by the user has less than 2 digits

{ cout << "Number must be at least 2 digits long." << endl;

// Output an error message if number is less than 2 digits

goto input; }

if(num % 2 == 0)

// Check if the number is even

{ cout << "The number " << num << " is even." << endl; }

else // If the number is not even, it must be odd

{ cout << "The number " << num << " is odd." << endl; }

return 0; }
Screenshot (Question #5):

.
Question #6: Write a C++ program to find the roots of a quadratic equation ax2 + bx + c = 0?

Code:

#include <iostream>

#include <cmath>

using namespace std;

int main() {

float a, b, c, determinant, root1, root2, real, imag;

cout << "Enter coefficients a, b and c of quadratic equation \n";

cin >> a >> b >> c;

/* Calculate determinant */

determinant = b*b - 4*a*c;

if(determinant >= 0)

{ root1= (-b + sqrt(determinant))/(2 * a);

root2= (-b - sqrt(determinant))/(2 * a);

cout << "Square roots are " << root1 <<

" " << root2; }

else {

real= -b/(2*a);

imag = sqrt(-determinant)/(2 * a);

cout << "Square roots are " << real << "+" << imag << "i , "

<< real << "-" << imag << "i"; }

return 0;

}
Screenshot (Question #6):
Assignment 2
Q1: Write a program in C++ to find the first 10 natural numbers using loop?
(The natural numbers are:1 2 3 4 5 6 7 8 9 10).

Code:

#include <iostream>

using namespace std;

int main()

{ // Loop from 1 to 10 and print the numbers

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

{ cout<<i<<" "; }

return 0; }

Screenshot (Question #1):

Q2: Write a program in C++ to find the sum of first 10 natural numbers?

Code:

#include <iostream>

using namespace std;

int main()

int sum=0;

// Loop from 1 to 10 and calculate the sum of numbers

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

sum += i;

// Print the sum of first 10 natural numbers

cout<<"The sum of first 10 natural numbers is: "<<sum<<endl;

return 0;

Screenshot (Question #2):

Q3: Write an infinite loop.? Hint: A infinite loop never ends. Condition is always true.

Code:

#include <iostream>
using namespace std;

int main()

// Create an infinite loop using a while statement

while(true)

cout<<"This is an infinite loop."<<endl;

return 0;

Screenshot (Question #3):

Q4: Write a program in C++ that will display following output using loop?

#####
####
###
##
#

Code:

#include <iostream>

using namespace std;

int main()

// Loop to print the pyramid pattern of '#' symbols

for(int i=5; i>=1; i--)

for(int j=1; j<=i; j++)

cout<<"# "; }

cout<<endl; }

return 0; }

Screenshot (Question #4):


Q5: Write a program in C++ that will display following output using loop?
1
11
111
1111
11111

Code:

#include <iostream>

using namespace std;

int main()

// Loop to print the pattern of '1's

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

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

cout<<"1";

cout<<endl;

return 0; }

Screenshot (Question #5):

Q6: Write a program in C++ that will display following output using loop?
5
54
543
5432
54321

Code:

#include <iostream>

using namespace std;

int main()

// Loop to print the pattern of decreasing numbers

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

for(int j=5; j>=i; j--) {

cout<<j;
}

cout<<endl;

return 0;

Screenshot (Question #6):

Q7: Write a program in C++ that will display following output using loop?
12345
2345
345
45
5

Code:

#include <iostream>

using namespace std;

int main()

// Loop to print the pattern of decreasing numbers

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

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

cout<<j;

cout<<endl;

return0; }

Screenshot (Question #7):

Q8: Write a program in C++ to print all ASCII character with their values?
Sample Output:
65 --> A
66 --> B
67 --> C
68 --> D
69 --> E and so on

Code:

#include <iostream>

using namespace std;


int main() {

// Loop to print all ASCII characters with their values

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

cout<<i<<" --> "<<(char)i<<endl;

return 0;

Screenshot (Question #8):

You might also like