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

Assignment #1: Programming Fundamentals

The document discusses programming fundamentals through a series of questions and answers. It covers topics such as: 1) The role of escape sequences in representing special characters in strings. 2) The differences between various selection structures like if-else, elseif for controlling program flow based on boolean conditions. 3) Examples of programming problems involving arithmetic calculations, input/output, and conditional logic. 4) The importance of data types in specifying the type of data a variable can store and the memory allocated.

Uploaded by

Muzzamil Hussain
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

Assignment #1: Programming Fundamentals

The document discusses programming fundamentals through a series of questions and answers. It covers topics such as: 1) The role of escape sequences in representing special characters in strings. 2) The differences between various selection structures like if-else, elseif for controlling program flow based on boolean conditions. 3) Examples of programming problems involving arithmetic calculations, input/output, and conditional logic. 4) The importance of data types in specifying the type of data a variable can store and the memory allocated.

Uploaded by

Muzzamil Hussain
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Programming Fundamentals

Assignment #1

Class and Section:


B.Sc. (SE) PEC-Batch-02
Submitted By:
Muzzamil Hussain
Roll No:
F-201041
Submitted To:
Mr. Qamar Abbas
Dated: June 6, 2020
Q1: Discuss the role and requirement of escape sequences in programming languages

Character blends comprising of a backslash (\) trailed by a letter or by a mix of digits are
designated "get away from successions.". Getaway arrangements are commonly used to
determine activities, for example, carriage profits and tab developments for terminals and
printers. They are likewise used to give exacting portrayals of nonprinting characters and
characters that typically have uncommon implications, for example, the double quotation
mark ("). Many programming dialects use get away from arrangements in strings to speak to
unique characters. Getaway arrangements are groupings of at least two characters
beginning with a backslash (\).

Some escape sequences along with their representation is given below

Escape Sequence Represents

\a Bell (alert)

\b Backspace

\f Form feed

\n New line

\r Carriage return

\t Horizontal tab

\v Vertical tab

Q2: Elaborate the difference between various selection structures available in


programming languages.

Also known as a contingent structure, a selection structure is a programming highlight that


performs various procedures dependent on whether a boolean condition is valid or invalid.
Selection structures utilize social administrators to test conditions. There are various kinds
of selection structures that can be utilized to accomplish various results.

if-end
if-end selection structures are used when only one boolean condition is necessary. In if-end
structures, a process will be only be performed if the boolean condition is true.

If-else

if-else selection structures are used when only one boolean condition is necessary. In if-else
structures, a specific action will be performed if the boolean condition is true and another
action, if the condition is false.

Elseif

elseif structures are a way to combine multiple boolean conditions into a single selection
structure.

Q3: Read two positive numbers N1 and N2. Test whether the larger number is exactly
divisible by the smaller one.

#include<iostream>

using namespace std;

int main()

int a;

int b;

cout<<"Enter two numbers: \n";

cin>>a>>b;

if(a%b==0)

cout << "It is divisible \n";

else

cout << "Not divisible \n";

system ("pause");

return 0;

}
Q4: Read a number from the user (within range 1-100). Then check whether the number is
greater than, less than or equal to 50

#include<iostream>

using namespace std;

int main()

int a;

cout << "Enter a number between 1 to 100"<<endl;

cin>>a;

if(a>50 && a <=100)

cout << "Number is greater than 50"<<endl;

else if (a==50)

cout << "Number is equal to 50"<<endl;

else if(a>0 && a<50)

cout << "Number is less than 50"<<endl;

else

cout << "Entered Number is invalid"<<endl;


}

system ("pause");

return 0;

Q5: Take the lengths of the two legs of a right triangle as input and calculates area and
hypotenuse.

Area = ½ * leg1 * leg2

Hypotenuse = √ leg12+leg 22

#include <iostream>

#include <math.h>

using namespace std;

int main()

int leg_1, leg_2;

float area, hyp;

cout <<"The length of leg 1 is = "<<endl;

cin >> leg_1;

cout << "The length of leg 2 is = "<< endl;

cin >> leg_2;

area=(0.5) * (leg_1) * (leg_2);

cout << "The area of triangle is = " << area << endl;

hyp =sqrt ((leg_1*leg_1)+ (leg_2*leg_2));

cout << "The hypotenuse of triangle is = "<< hyp <<endl;

system ("pause");

return 0;
}

Q6: Read two numbers N1, N2 and a character OP (+,-,*,/), then perform calculation
depending upon the type of character entered by user and display result in format:

N1 op N2 = result

#include<iostream>

using namespace std;

int main()

char op;

float num1;

float num2;

cout << "Enter character"<<endl;

cin>>op;

cout<<"Enter one number"<<endl;

cin>>num1;

cout<<"Enter second number"<<endl;

cin>>num2;

switch(op)

case '+':

cout <<num1<<"+"<<num2<<"="<< num1 + num2 <<endl;

break;

case '-':

cout<<num1<<"-"<<num2<<"="<< num1-num2 <<endl;

break;
case '*':

cout<<num1<<"*"<<num2<<"="<< num1*num2 <<endl;

break;

case '/' :

cout<<num1<<"/"<<num2<<"="<< num1/num2 <<endl;

break;

default:

cout<< "Operator is not correct"<<endl;

break;

system ("pause");

return 0;

Q7: Find maximum and minimum of three numbers. Use proper string messages to make
your program readable.

#include<iostream>

using namespace std;

int main()

float n1;

float n2;

float n3;

cout << "Enter three numbers"<<endl;

cin>>n1>>n2>>n3;

if(n1>n2 && n1>n3)


{

cout <<"Largest number is "<<n1<<endl;

else if(n2>n1 && n2 >n3)

cout<<"Largest number is "<<n2<<endl;

else

cout<<"Largest number is "<<n3<<endl;

if(n1<n2 && n1<n3)

cout<<"Smallest number is "<<n1<<endl;

else if(n2<n1 && n2<n3)

cout<<"Smallest number is "<<n2<<endl;

else

cout<<"Smallest number is "<<n3<<endl;

system ("pause");

return 0;

}
Q8: Discuss the role and requirement of data types in programming languages.

All variables use data-type during statement to limit the type of data to be put away.
Subsequently, we can say that data types are utilized to tell the variables the type of data it
can store. At whatever point a variable is characterized in C++, the compiler allots some
memory for that variable dependent on the data-type with which it is proclaimed. Each data
type requires an alternate amount of memory. Data types help the related variables
understand the type of data they can store into them.

THE END

You might also like