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

Document 1

The document contains a series of C++ programming exercises that cover various topics such as finding maximum numbers, checking divisibility, validating triangles, calculating gross salaries, character classification, palindrome checking, swapping numbers, reversing digits, error types in programming, debugging procedures, and the features of Integrated Development Environments (IDEs). Each question includes a code snippet demonstrating the solution. Additionally, it discusses the types of errors in C/C++ and how IDEs assist in program development.

Uploaded by

hsyedhassan9
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views17 pages

Document 1

The document contains a series of C++ programming exercises that cover various topics such as finding maximum numbers, checking divisibility, validating triangles, calculating gross salaries, character classification, palindrome checking, swapping numbers, reversing digits, error types in programming, debugging procedures, and the features of Integrated Development Environments (IDEs). Each question includes a code snippet demonstrating the solution. Additionally, it discusses the types of errors in C/C++ and how IDEs assist in program development.

Uploaded by

hsyedhassan9
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

Name: Anas Mehmood

Class: BS AI
Student ID: 0000947237
Submitted To: Mam Yusra
QUESTION 1
Write a C++ program to find maximum between two numbers using
switch case.

#include<iostream>
using namespace std;
int main()
{
int a,b;
cout<<"enter two numbers:";
cin>>a>>b;
switch (a>b) {
case 1:
cout<<"maximum number is:"<<a<<endl;
break;
case 0:
switch (b>a){
case 1:
cout<<"maximum number is"<<b<<endl;
break;
case 0:
cout<<"both numbers are
equal"<<endl;
break;
}
break;
}
return 0;

}
QUESTION 2
Write a C++ program to check whether a number is divisible by 5 and 11
or not.

#include<iostream>
using namespace std;
int main()
{
int num;
cout<<"enter the number:";
cin>>num;
if(num%5==0 && num%11==0){
cout<<num<<"is divisible by both 5 and 11"<<endl;
}else{
cout<<num<<"is not devisible by both 5 and 11"<<endl;
}
return 0;
}
QUESTION 3

Write a C++ program to input angles of a triangle and check whether


triangle is valid or not.

#include<iostream>
using namespace std;
int main()
{
float angle1,angle2,angle3;
cout<<"enter first angle of the triangle";
cin>>angle1;
cout<<"enter the second angle of the triangle";
cin>>angle2;
cout<<"enter the third angle of the triangle";
cin>>angle3;
if(( angle1+angle2+angle3==180)&& angle1>0 &&angle2>0
&&angle3>0) {
cout<<"the triangle is valid:"<<endl;
}else{
cout<<"the triangle is not valid"<<endl;
}
return 0;
}
QUESTION 4
Write a C++ program to input basic salary of an employee and calculate
its Gross salary according to following: using if -else statement
Basic Salary <= 10000 : HRA = 20%, DA = 80%
Basic Salary <= 20000 : HRA = 25%, DA = 90%
Basic Salary > 20000 : HRA = 30%, DA = 95%

#include <iostream>
using namespace std;

int main() {
float basicSalary, hra, da, grossSalary;

// Input the basic salary


cout << "Enter basic salary of the employee: ";
cin >> basicSalary;

// Calculate HRA and DA based on the salary range


if (basicSalary <= 10000) {
hra = basicSalary * 0.20;
da = basicSalary * 0.80;
}
else if (basicSalary <= 20000) {
hra = basicSalary * 0.25;
da = basicSalary * 0.90;
}
else {
hra = basicSalary * 0.30;
da = basicSalary * 0.95;
}

// Calculate gross salary


grossSalary = basicSalary + hra + da;

// Display the result


cout << "Gross Salary = " << grossSalary << endl;

return 0;
}
QUESTION 5
Write a program that allows the user to enter any character through the
keyboard and determines, whether it is a capital letter, small letter, a digit
number or a special symbol.

#include <iostream>
using namespace std;

int main() {
char ch;

// Ask the user to enter a character


cout << "Enter a character: ";
cin >> ch;

// Check the type of character


if (ch >= 'A' && ch <= 'Z') {
cout << "It is a capital letter." << endl;
}
else if (ch >= 'a' && ch <= 'z') {
cout << "It is a small letter." << endl;
}
else if (ch >= '0' && ch <= '9') {
cout << "It is a digit number." << endl;
}
else {
cout << "It is a special symbol." << endl;
}

return 0;
}
QUESTION 6
Write a C++ program to check whether a number is palindrome or not.
#include <iostream>
using namespace std;

int main() {
int num, originalNum, reversedNum = 0, remainder;

cout << "Enter an integer: ";


cin >> num;

originalNum = num;

// Reverse the number


while (num != 0) {
remainder = num % 10;
reversedNum = reversedNum * 10 + remainder;
num /= 10;
}

// Check if the original number is equal to the reversed number


if (originalNum == reversedNum) {
cout << originalNum << " is a palindrome." << endl;
} else {
cout << originalNum << " is not a palindrome." << endl;
}

return 0;
}
QUESTION 7
Write a program that inputs two numbers, swaps these values without
using third variable and display them

#include <iostream>
using namespace std;

int main() {
int a, b;

// Input two numbers


cout << "Enter first number (a): ";
cin >> a;
cout << "Enter second number (b): ";
cin >> b;

// Swapping without using third variable


a = a + b;
b = a - b;
a = a - b;

// Display the swapped values


cout << "After swapping:" << endl;
cout << "First number (a): " << a << endl;
cout << "Second number (b): " << b << endl;

return 0;
}
QUESTION 8
Write a program that inputs a five digit number as input and reverse the
number. For example if the user enter 92174, it displays 47129 .

#include <iostream>
using namespace std;

int main() {
int a, b;

// Input two numbers


cout << "Enter first number (a): ";
cin >> a;
cout << "Enter second number (b): ";
cin >> b;

// Swapping without using third variable


a = a + b;
b = a - b;
a = a - b;

// Display the swapped values


cout << "After swapping:" << endl;
cout << "First number (a): " << a << endl;
cout << "Second number (b): " << b << endl;

return 0;
}
QUESTION 9
What types of errors can occur while writing a computer program?? How
these errors can be removed in C/C++? Explain the debugging procedure
in C/C++.

When writing a computer program, especially in languages like C or C++,


several types of errors can occur. These errors generally fall into three
main categories: syntax errors, runtime errors, and logical errors.
Each of these errors has different causes and methods for removal.

🔹 1. Types of Errors in C/C++

a. Syntax Errors

Definition: Errors that violate the rules of the programming


language grammar.

Examples:

Missing semicolon: int x = 5

Misspelled keywords: pritnf("Hello");

Wrong function declaration: int main( {

How to fix:

Use a compiler (like GCC or Clang) to identify and correct


these mistakes.

Most IDEs highlight syntax errors.


b. Runtime Errors

Definition: Errors that occur during the execution of the program.

Examples

Division by zero: int x = 10 / 0;

Null pointer dereferencing.

File not found when opening a file.

How to fix:

Add input validation and error-checking code.

Use debugging tools to trace where the error occurs during


execution.

c. Logical Errors

Definition: The program compiles and runs, but the output is


incorrect due to an error in logic.

Examples:

Using + instead of * in a formula.

Incorrect loop conditions

How to fix:

Review the program's logic step-by-step.

Add print statements or use a debugger to trace variable


values.

🔹 2. Removing Errors in C/C++

Use of Compiler Warnings and Errors:


Compile the code using flags like -Wall in GCC: gcc -Wall
program.

This helps in catching potential issues early.

Code Review and Testing:

Review code manually or with peers.

Write and run unit tests to check logic.

Use of Tools:

Static analyzers (e.g., cppcheck, Clang Static Analyzer).

Memory checkers (e.g., Valgrind for detecting memory leaks and invalid
access).

🔹 3. Debugging Procedure in C/C++

Debugging is the process of identifying, analyzing, and fixing bugs in a


program. Here's how it's done in C/C++:

Step-by-Step Debugging Procedure:

Compile with Debug Information:

Use -g flag to include debug symbols:

bash

CopyEdit
gcc -g program.c -o program

1.

Use a Debugger (e.g., gdb):

Start debugger:

bash
CopyEdit
gdb ./program

Common gdb commands:

break main – Set a breakpoint at main()

run – Start the program

next – Execute next line

print var – Print the value of a variable

backtrace – Show function call stacK

continue – Continue execution until next breakpoint

2.

Set Breakpoints:

Pause execution at key locations to inspect program behavior.

Step Through the Code:

Use step (s) and next (n) to execute line by line and examine
logic.

Watch Variables:

Monitor variable values to identify where they go wrong.

Analyze Stack Traces:

If a crash occurs, inspect the call stack to find the faulty


function.
QUESTION 10

How an integrated Development Environment (IDE) helps a programmer


to edit, compile and run a program? Discuss the features of the IDE you
are using for program development in this course.

An Integrated Development Environment (IDE) is a software


application that provides comprehensive facilities to programmers for
software development. It integrates multiple tools into a single interface
to streamline the process of writing, compiling, debugging, and running
programs.

✅ How an IDE Helps in Program Development

1. Code Editing

Offers a rich text editor with features like:

Syntax highlighting

Auto-completion

Code formatting

Bracket matching

Error highlighting

2. Compilation

Allows one-click compilation using built-in compiler integration


(e.g., GCC for C/C++).

Shows compiler errors and warnings directly in the interface


with line numbers and suggestions.

3. Running Programs

Provides a "Run" button to execute the compiled program.

Displays output in a terminal or output window.


Some IDEs also support input redirection or interactive I/O.

4. Debugging Tools

Integrated debugger with features like:

Breakpoints

Step-by-step execution

Watch expressions

Call stack inspection

5. Project Management

Helps organize files into projects or solutions.

Supports multiple source files and libraries.

6. Other Tools

Version control integration (Git)

Code refactoring tools

Build automation

Plugin and extension support

🔹 10) Features of the IDE Used in This Course

Since you didn’t specify the IDE you're using, I’ll describe
Code::Blocks and Visual Studio Code (VS Code) — two
common IDEs used in C/C++ courses. Let me know if you're
using a different one.

🔹 A. Code::Blocks (for C/C++)

Key Features:

Simple and lightweight C/C++ IDE.


Supports multiple compilers (GCC, MinGW, MSVC).

Built-in debugger (uses GDB).

Project management and workspace handling.

Syntax highlighting and code folding.

Integrated build system (auto builds on save or run).

Customizable UI and keyboard shortcuts.

How it helps:

Easy for beginners.

All-in-one setup for editing, compiling, running, and debugging


C/C++ programs.

🔹 B. Visual Studio Code (VS Code)

Key Features:

Lightweight but powerful text editor with IDE-like capabilities.

Requires extensions like:

C/C++ Extension by Microsoft

Code Runner or build tasks

Intellisense (auto-complete and suggestions).

Integrated terminal for manual gcc or g++ commands.

Git integration and source control tools.

Debugging support with configuration (launch.json).

Custom tasks for building projects.

How it helps:
Highly customizable.

Supports many languages in addition to C/C++.

Useful for both beginners and advanced developers.

You might also like