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

CFP Assignment 3A

This document contains summaries of 14 programs written in C++. The programs cover topics like input/output, if/else statements, switch statements, loops, functions, data types, operators, and conversions between binary and decimal numbers. Each program is preceded by the program number and includes the source code and brief description of what the program is demonstrating.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
96 views

CFP Assignment 3A

This document contains summaries of 14 programs written in C++. The programs cover topics like input/output, if/else statements, switch statements, loops, functions, data types, operators, and conversions between binary and decimal numbers. Each program is preceded by the program number and includes the source code and brief description of what the program is demonstrating.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

CIS-104 Computer Fundamentals and

Programming

Assignment 3(A)

Compiled by: Muhammad Muddassir


Instructor: Muhammad Tufail

BSEE 19-23
Sec-A

Date: 28th October,2019


Program no.1:

#include<iostream>
using namespace std;
int main()
{
float fuel,Total_Consumption;
int distance;
cout << "Enter the distance in km : ";
cin >> distance;
cout << "Enter the fuel consumption in one liter : ";
cin >> fuel;
Total_Consumption = distance * fuel;
cout << "Thus " << Total_Consumption << " Liters of fuel will be burned in travelling through "
<< distance << " km " << endl;

getchar();
return 0;
}
Program no.2:

#include<iostream>
using namespace std;
int main()
{
int x;
cout << "Enter the integer to check the range : ";
cin >> x;
if (x >= 0 && x <= 20)
cout << x << " is in range (0-20)" << endl;
else if (x > 20 && x <= 40)
cout << x << " is in range of (21-40)" << endl;
else if (x > 40 && x <= 60)
cout << x << " is in range of (41-60)" << endl;
else if (x > 60 && x <= 80)
cout << x << " is in range of(61-80)" << endl;
else if (x < 0 && x>80)
cout << "Error" << endl;
getchar();
return 0;
}
Program no.10:
#include <iostream>
using namespace std;
int main()
{
int num1, num2;
cout << "Enter two numbers to find maximum number: ";
cin >> num1;
cin >> num2;
switch (num1 > num2)
{
case 0: cout << num2 << " is Maximum number";
break;
case 1: cout << num1 << " is Maximum number";
break;
}
return 0;
}
Program no.11:

#include <iostream>
using namespace std;
int main()
{
int day;
cout << "Enter the day to find name : ";
cin >> day;
switch (day)
{
case 1:cout << "Day " << day << " is Monday";
break;
case 2:cout << "Day " << day << " is Tuesday";
break;
case 3:cout << "Day " << day << " is Wednesday";
break;
case 4:cout << "Day " << day << " is Thursday";
break;
case 5:cout << "Day " << day << " is Friday";
break;
case 6:cout << "Day " << day << " is Saturday";
break;
case 7:cout << "Day " << day << " is Sunday";
break;
default:
cout<<"The day number must be less than 8"<<endl;

}
getchar();
return 0;
}

Program no:6
Identify and correct the errors in each of the following:
i) if ( gender == 1 )
cout<< "Woman" ;
else;
cout<< "Man" ;
ii) if ( a= 1 )

cout<< "a is equal to 1" ;


else
cout<< "a is not equal to 1" ;
iii) if ( ch == 1 );

cout<< "Lab" ;
else
cout<< "Tutorial" ;

Corrections:

i) if ( gender == 1 )
cout<< "Woman" ;
else
cout<< "Man" ;
ii) if ( a== 1 )

cout<< "a is equal to 1" ;


else
cout<< "a is not equal to 1" ;
iii) if ( ch == 1 )

cout<< "Lab" ;
else
cout<< "Tutorial" ;
Program no 12:

#include <iostream>
using namespace std;
int main()
{
char alphabet;
cout << "Enter the alphabet to check whether it is consonant or a vowel : ";
cin >> alphabet;
switch (alphabet)
{
case 'A':cout << "A" << " is vowel";
break;
case 'E':cout << "alphabet " << alphabet << " is a Vowel";
break;
case 'I':cout << "alphabet " << alphabet << " is a Vowel";
break;
case 'O':cout << "alphabet " << alphabet << " is a Vowel";
break;
case 'U':cout << "alphabet " << alphabet << " is a Vowel";
break;
case 'a':cout << "alphabet " << alphabet << " is a Vowel";
break;
case 'e':cout << "alphabet " << alphabet << " is a Vowel";
break;
case 'i':cout << "alphabet " << alphabet << " is a Vowel";
break;
case 'o':cout << "alphabet " << alphabet << " is a Vowel";
break;
case 'u':cout << "alphabet " << alphabet << " is a Vowel";
break;
default: cout << "alphabet " << alphabet << " is a Consonant";
}
getchar();
return 0;
}

Program no.4:

x=x+1
++x
x++
x==1

are the four statements to add one to a variable x.

Program no.8:

#include <iostream>
using namespace std;
int main()
{
int c; // demonstrates post increment
c = 5; // assigns 5 to c
cout << c << endl; // prints 5
cout << c++ << endl; // prints 5 then post increment
cout << c << endl; // prints 6

cout << endl; // skips a line


// demonstrates pre increment
c = 5; // assigns 5 to c
cout << c << endl; // prints 5
cout << ++c << endl; // pre increment then prints 6
cout << c << endl; // prints 6
return 0;
}
Program no.13:

#include<iostream>
using namespace std;
int main() {
char grade;
double gpa = 0.0;
cout << "Enter your Grade= ";
cin >> grade;
switch (grade) {
case'A':
case'a':
gpa = 4.0;
cout << "your GPA is " << gpa;
break;
case'B':
case'b':
gpa = 3.0;
cout << "your GPA is " << gpa;
break;
case'C':
case'c':
gpa = 2.0;
cout << "your GPA is " << gpa;
break;
case'D':
case'd':
gpa = 1.0;
cout << "your GPA is " << gpa;
break;
case'F':
case'f':
gpa = 0.0;
cout << "your GPA is " << gpa;
break;
default:
cout << "invalid grade entered";
break;
}
return 0;
}
Program no.9(a)

#include <iostream>
using namespace std;
int main()
{
int x, y;
cout << "Enter the value of x and y :";
cin >> x >> y;
if (x < 10)
{
if (y > 10)
cout << "y is greater than 10 ";
else
cout << "y is less than or equal to 10 ";
}
else
cout << "x is greater than or equal to 10";
getchar();
getchar();
return 0;}
Program no.7

#include <iostream>
using namespace std;
int main()
{
long float act_num ;
float total_charge;
float begining_balance;
float new_balance;
float total_credits;
float credit_limit;
cout << "Enter account number: " << endl;
cin >> act_num;
cout << "Enter the Beginning Balance: " << endl;
cin >> begining_balance;
cout << "Enter the Total Charges: " << endl;
cin >> total_charge;
cout << "Enter the Total Credits: " << endl;
cin >> total_credits;
cout << "Enter Credit Limit: " << endl;
cin >> credit_limit;
new_balance = begining_balance + total_charge - total_credits;
cout << "New Balance " << new_balance << endl;
if (new_balance > credit_limit)
{
cout << "\tAccount: " << act_num << endl;
cout << "\tCredit Limit: " << credit_limit << endl;
cout << "\tBalance: " << new_balance << endl;
cout << "***** Credit limit Exceeded! *****" << endl;
}
else
cout << "Payment successful new balance is :" << new_balance << endl;
getchar();
getchar();
return 0;
}
Program no.5

#include <iostream>
#include <iomanip>
using namespace std;
int main()
{

float x, y, res;
cout << "Enter the value of x and y :" << endl;
cin >> x >> y;
res = x * y;
cout << "result " << res << endl;
cout << setprecision(3) << fixed;
cout << "Result upto 3 digits of precision: " << res << endl;

getchar();
getchar();
return 0;
}

Program no.3:

#include <iostream>
using namespace std;
int main()
{
int num;
int decimal_num, rem, i;
cout << "Enter the binary number to convert it to decimal number" << endl;
cin >> num;
decimal_num = 0;
i = 0;
while (num != 0)
{
rem = num % 10;
num = num / 10;
decimal_num += rem * pow(2,i);
++i;
}
cout << "Decimal eqiuvalent of binary number is : " << decimal_num << endl;

getchar();
getchar();
return 0;
}

You might also like