PF Assignment No. 1,2,3
PF Assignment No. 1,2,3
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>
int main() {
int age; // declare an integer variable age to store the user's age
{ cout << "You can only work in urban areas" << endl; }
else
return 0; }
Code:
#include <iostream>
int main() {
int marks;
else
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>
int main() {
{ cout << "The oldest person is " << age1 << " years old." << endl; }
{ cout << "The oldest person is " << age2 << " years old." << endl; }
else
{ cout << "The oldest person is " << age3 << " years old." << endl; }
{ cout << "The youngest person is " << age1 << " years old." << endl; }
{ 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>
int main()
input:
cout << "Enter the radius of the circle: "; // Prompt the user to enter the radius
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; }
Code:
#include <iostream>
int main()
{ int num;
input:
// Check if the number entered by the user has less than 2 digits
goto input; }
if(num % 2 == 0)
{ cout << "The number " << num << " is even." << endl; }
{ 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>
int main() {
/* Calculate determinant */
if(determinant >= 0)
else {
real= -b/(2*a);
cout << "Square roots are " << 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>
int main()
{ cout<<i<<" "; }
return 0; }
Q2: Write a program in C++ to find the sum of first 10 natural numbers?
Code:
#include <iostream>
int main()
int sum=0;
sum += i;
return 0;
Q3: Write an infinite loop.? Hint: A infinite loop never ends. Condition is always true.
Code:
#include <iostream>
using namespace std;
int main()
while(true)
return 0;
Q4: Write a program in C++ that will display following output using loop?
#####
####
###
##
#
Code:
#include <iostream>
int main()
cout<<"# "; }
cout<<endl; }
return 0; }
Code:
#include <iostream>
int main()
cout<<"1";
cout<<endl;
return 0; }
Q6: Write a program in C++ that will display following output using loop?
5
54
543
5432
54321
Code:
#include <iostream>
int main()
cout<<j;
}
cout<<endl;
return 0;
Q7: Write a program in C++ that will display following output using loop?
12345
2345
345
45
5
Code:
#include <iostream>
int main()
cout<<j;
cout<<endl;
return0; }
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>
return 0;