0% found this document useful (0 votes)
9 views8 pages

Programming Lab 3

The document is a report by Kinza Fatima for a Computer Programming Lab course, detailing six programming tasks. Each task includes a description, the corresponding C++ code, and the expected output. The tasks cover topics such as divisibility checks, quadratic equations, age categorization, palindrome checking, character classification, and case conversion.

Uploaded by

xiaxiaotian0987
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 views8 pages

Programming Lab 3

The document is a report by Kinza Fatima for a Computer Programming Lab course, detailing six programming tasks. Each task includes a description, the corresponding C++ code, and the expected output. The tasks cover topics such as divisibility checks, quadratic equations, age categorization, palindrome checking, character classification, and case conversion.

Uploaded by

xiaxiaotian0987
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

Computer programming lab

Report 3

Name : Kinza Fatima


Reg No : L1F25BSEE0014
Submitted to: Prof. Eman Nazir
Date : 24-03-26
Department : Department of Electrical Engineering (BSEE)
Subject : Computer Programming Lab
Code : EECS1211
Task 1:
Implement a program which decides if a number entered by the user
is divisible by 7 or not. Display the result accordingly.
Code:

#include <iostream>
using namespace std;
int main() {
int n;
cout<<"Enter a number: ";
cin>>n;
if (n%7==0) {
cout<<"The number is divisible by 7!";
}
else {
cout<<"The number is not divisible by 7!";
}
return 0;

Output:

Task 2:
Implement a program which determines the roots of a quadratic
equation. The program also determines if the roots of the equation are real or imaginary.
Display the roots if they are real. Print a message on the output screen about the nature of the
roots (real or imaginary).

Code:
#include <iostream>

#include <cmath>

using namespace std;

int main() {

int a, b, c, d, x1, x2;

cout<<"ax^2 + bx + c = 0"<<endl;

cout<<"Enter the value of a,b and c: ";

cin>>a>>b>>c;

d = pow(b,2) - 4*a*c;

if (d >= 0) {

cout<<"The roots of the eqaution are real! "<<endl;

x1 = (-b + sqrt(d)) / (2*a);

x2 = (-b - sqrt(d)) / (2*a);

cout<<"The first root: "<<x1<<endl;

cout<<"The second root: "<<x2<<endl;

else {

cout<<"The roots of equation are imaginary!"<<endl;

cout<<"So it does not have any real solution.";

return 0;

Output:
Task 3:
Consider the following age groups:
Infant – age <= 3 years
Child – 3 years < age <= 12 years Teenager – 12
years < age <= 19 years Young – 19 years < age
<= 30 years Mid age – 30 years < age <= 50
years Old – 50 years < age

Implement a code which takes the age of a person and decides which age group does
the person falls in. Display the age and age group to which the person belongs.
Code:

#include <iostream>
using namespace std;
int main() {
int age;
cout<<"Enter your age: ";
cin>>age;
if (age <= 3) {
cout<<"Age Group: Infant.";
}
else if (age <= 12) {
cout<<"Age Group: Child.";
}
else if (age <= 19) {
cout<<"Age Group: Teenager.";
}
else if (age <= 30) {
cout<<"Age Group: Young.";
}
else if (age <= 50) {
cout<<"Age Group: Middle-age.";
}
else {
cout<<"Age Group: Old.";
}
return 0;
}

Output:

Task 4:
A palindrome is a string of characters that reads the same forward as
backwards. For example, 15251, 37173, etc are palindromes. Implement a program which
takes a five digit number as input from the user and decides whether it is a palindrome or not
(Hint: use / and % operators to separate the digits of a number

Code:

#include <iostream>
using namespace std;
int main() {
int n;
cout<<"Enter a 5-digit number: ";
cin>>n;
int first = n / 10000;
int second = (n / 1000) % 10;
int fourth = (n / 10) % 10;
int last = n % 10;
if (first == last && second == fourth) {
cout<<"The number is a palindrome!";
}
else {
cout<<"The number is not a palindrome!";
}

return 0;
}

Output:

Task 5:
A character is entered from the keyboard. Determine if the character
entered is a capital letter, small letter, number or a special character. The ASCII character set
is summarized as follows:

Capital letters: A to Z – 65 to 90 Small


letters: a to z – 97 to 122 Numbers: 0 to 9 –
48 to 57
Special characters (like @, &, *, ”, etc) : 0 to 47, 58 to 64, 91 to 96, 123 to 127

Code:
#include <iostream>
using namespace std;
int main() {
char ch;
cout<<"Enter a character: ";
cin>>ch;
int ascii = int(ch);
if (ascii >= 65 && ascii <= 90) {
cout<<"The character belong to: Capital letters(A-Z)!";
}
else if (ascii >= 97 && ascii <= 122) {
cout<<"The character belong to: Small letters(a-z)!";
}
else if (ascii >= 48 && ascii <= 57) {
cout<<"The character belong to: Numbers(0-9)!";
}
else if (ascii <= 47 || ( ascii >= 58 && ascii <= 64) || ( ascii >= 91 && ascii <= 96) || (ascii >= 123 && ascii
<= 127)) {
cout<<"The character belong to: Special Characters(#,%,&,@)!";
}
return 0;
}

Output:

Task 6:
Write a program that takes a single character as input from the user. If the entered character is a small
letter (a to z), convert it to its capital letter and display the result. If the entered character is a capital letter (A to
Z), convert it to its small letter and display the result. If the entered character is neither a small nor a capital
letter, display an appropriate message.
Code:
#include <iostream>
using namespace std;
int main() {
char ch;
cout<<"Enter a character: ";
cin>>ch;
int ascii = int(ch);
if (ascii >= 65 && ascii <= 90) {
ascii = ascii + 32;
char a = char(ascii);
cout<<"Final result: "<<a;
}
else if (ascii >= 97 && ascii <= 122) {
ascii = ascii - 32;
char a = char(ascii);
cout<<"Final result: "<<a;
}
else {
cout<<"The character you entered is not appropriate. Enter an alphabet!";
}
return 0;
}

Output:

You might also like