Assignment

Download as pdf or txt
Download as pdf or txt
You are on page 1of 30

Department of Information Technology

Faculty of Engineering and Technology


University of Sindh, Jamshoro.

Class: BSIT Part 1 (Morning)


Semester: First Semester
Course: Programming
Fundamentals Full Name: Abdul
Jabbar Roll No. 2K22/IT/5
WEEK 1
WEEK 2
Solution of Exercise 1:

#include<iostream>
using namespace std;
int main()
{
float volume, radius, height;
const float PI = 3.14159265;
cout<<"Enter the radius of cone: ";
cin>>radius;
cout<<"Enter the height of the cone: ";
cin>>height;
cout<<"The volume of the cone is: ";
volume = (PI * radius * radius * height ) / 3;
cout<<volume;
return 0;}

OUTPUT:

Solution of Exercise 2:
#include<iostream>
using namespace std;
int main()
{
float celsius, fahrenheit;
cout<<"Enter the temperature in Fahrenheit: ";
cin>>fahrenheit;
cout<<"The given temperature in Celsius is: ";
celsius=5*(fahrenheit-32)/9;
cout<<celsius;
return 0;}
OUTPUT:

WEEK 3
Solution of Exercise 1:

#include<iostream>
using namespace std;
int main(){
cout<<"\"Welcome All\" is a string constant \n"
<<"\'A\' is a character constant \n\\"
<<"t is for tab \n\\"
<<"n is for new line\n\n";
return 0;}

OUTPUT:
Solution of Exercise 2:

#include<iostream>
using namespace std;
int main()
{
int n1, n2;
cout<<"Enter First Number: ";
cin>>n1;
cout<<"Enter Second Number: ";
cin>>n2;
cout <<n1 <<" + " <<n2 <<" = " <<n1+n2 <<endl
<<n1 <<" - " <<n2 <<" = " <<n1-n2 <<endl
<<n1 <<" x " <<n2 <<" = " <<n1*n2 <<endl
<<n1 <<" / " <<n2 <<" = " <<n1/n2 <<endl
<<n1 <<" % " <<n2 <<" = " <<n1%n2 <<endl;
return 0;
}

OUTPUT:
WEEK 4
Solution of Exercise 1:

#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int a,b;
float area;
cout<<"Please enter the length of of two equal sides of the triangle: ";
cin>>a;
cout<<"Please enter the base of the triangle: ";
cin>>b;
area = 0.5 * b * sqrt( pow(a,2) - pow(b,2) / 4);
cout<<"The area of the triangle is: "
<<area;
return 0;
}

OUTPUT:

Solution of Exercise 2:
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int rad;
cout<<"Input the radius(1/2 of diameter) of a circle: ";
cin>>rad;
cout<<"The area of the circle is: " << M_PI*pow(rad,2) <<endl
<<"The circumference of the circle is: " <<2*M_PI*rad;
return 0;}
OUTPUT

WEEK 5
Solution of Exercise 1:

#include<iostream>
using namespace std;
int main()
{
long long a;
for (a=10;a<=1000000000;a*=10)
cout<<a <<" ";
return 0;
}

OUTPUT
Solution of Exercise 2:
#include<iostream>
using namespace std;
int main()
{
int first=0, second=1, ans,n;
cout<<"Enter n: ";
cin>>n;
cout<<first <<" , " <<second <<" , ";
for(int i=1;i<=(n-2);i++ ){
ans=first + second;
cout<<ans;
if (i<=(n-3))cout<<" , ";
first=second;
second=ans;}
}

OUTPUT:

Solution of Exercise 3:

#include<iostream>
using namespace std;
int main()
{
int n, sum;
cout<<"Enter N: ";
cin>>n;
for(int i=1; i<=n; i++){
sum=sum+i;
cout<<i;
if (i==n)cout<<" = " <<sum;
else cout<<" + ";}
}
OUTPUT:

Solution of Exercise 4:
#include<iostream>
using namespace std;
int main()
{
int table, start, end;
cout<<"Enter Table Number: ";
cin>>table;
cout<<"Enter Starting Value: ";
cin>>start;
cout<<"Enter Ending Value: ";
cin>>end;
for(int i=start; i<=end; i++)
{cout<<table <<" x " <<i <<" = " <<table*i <<endl;}
}

OUTPUT:
WEEK 6
Solution of Exercise 1:
#include<iostream>
using namespace std;
int main()
{int day;
cout<<"Input day number(1-7): ";
cin>>day;

switch (day){
case(1): {cout<<"The day is Monday"; break;}
case(2) : {cout<<"The day is Tuesday"; break;}
case(3) : {cout<<"The day is Wednesday"; break;}
case(4) : {cout<<"The day is Thursday"; break;}
case(5) : {cout<<"The day is Friday"; break;}
case(6) : {cout<<"The day is Saturday"; break;}
case(7) : {cout<<"The day is Sunday"; break;}
default : cout<<"You must enter number from 1 to 7";}
return 0; }

OUTPUT:

Solution of Exercise 2:
#include<iostream>
using namespace std;
int main()
{ int month;
cout<<"Input month number (1-12): ";
cin>>month;
switch (month){
case (1) : case (3) : case (5) : case (7) : case (8) : case (10) : case (12) :
{cout <<"Total number of days = 31"; break;}
case (2) : {cout<<"Total number of days = 28"; break;}
case (4) : case (6) : case (9) : case (11) : {cout<<"Total number of days = 30"; break;}
default : cout<<"Must enter number from 1 - 12";}
return 0;}
OUTPUT:

Solution of Exercise 3: (Switch statement)


#include<iostream>
using namespace std;
int main()
{
char ch3;
cout<<"Input Character: ";
cin>>ch3;

switch (ch3){
case ('a') : case ('e') : case ('i') : case ('o') : case ('u') :
case ('A') : case ('E') : case ('I') : case ('O') : case ('U') :
{cout<<"\'" <<ch3 <<"\' is a vowel"; break;}
default : {cout<<"\'" <<ch3 <<"\' is a consonant\n\n"; break;} }
return 0;}

OUTPUT:
Solution of Exercise 3: (if-else statement)
#include<iostream>
using namespace std;
int main()
{
char ch;
cout<<"Input Character: ";
cin>>ch;
if (ch=='a' || ch=='e' || ch=='i' || ch=='o' || ch=='u'
|| ch=='A' || ch=='E' || ch=='I' || ch=='O' || ch=='U')
{cout<<"\'" <<ch <<"\' is a vowel\n\n";}
else {cout<<"\'" <<ch <<"\' is a consonant\n\n";}
return 0;}

OUTPUT:

Solution of Exercise 4:

#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
int counter4;
for (counter4=129 ; counter4<=255 ; counter4++)
{
char ch4 = counter4;
if (counter4%8)
cout<<setw(7)<<counter4 <<setw(2)<<ch4;
else cout<<setw(7)<<counter4 <<setw(2)<<ch4 <<endl;
}
return 0;}
OUTPUT:

WEEK 7
Solution of Exercise 1:
#include<iostream>
#include<conio.h>
using namespace std;
int main(){
char ch;
do{
cout<<"Input character: ";
ch=getche();
cout<<endl;
if (ch>=48 && ch<=57){cout<<"'"<<ch<<"'"<<" is a number\n";}
else if ((ch>=65 && ch<=90) || (ch>=97 && ch<=122))
{cout<<"'"<<ch<<"'"<<" a alphabet\n";}
else if (ch!='\r') {cout<<"'"<<ch<<"'" <<" is a special character\n";}}
while (ch!= '\r');
cout<<"Exit";return 0;}

OUTPUT:
Solution of Exercise 2:
#include<iostream>
using namespace std;
int main(){
for (int i=1; i<10;i++){
for(int m=1; m<=i; m++)
cout<<"<";
cout<<" - " ;
for (int j=10-i; j>=1;j--) cout<<">";
cout<<endl;}
return 0;}

OUTPUT:

Solution of Exercise 3 of week 7:


#include<iostream>
using namespace std;
int main(){
const short a = 11;
for (int i=1; i<=a; i++){
for(int j=1; j<=a ;j++){
if (j<=(a-i)) cout<<" ";
else if (i%2==0) continue;
else cout<<" *";}
cout<<endl;}
return 0;}

OUTPUT:
Solution of Exercise 4:
#include<iostream>
using namespace std;
int main(){
int number, tens,ones;
do{ cout<<"\nEnter any number (from 1-99): ";
cin>>number;
tens = number/10;
ones = number%10;
if (number<20 && number>9){
switch (number){
case (10) : {cout<<"Ten"; break;}
case (11) : {cout<<"Eleven"; break;}
case (12) : {cout<<"Twelve"; break;}
case (13) : {cout<<"Thirteen"; break;}
case (14) : {cout<<"Fourteen"; break;}
case (15) : {cout<<"Fifteen"; break;}
case (16) : {cout<<"Sixteen"; break;}
case (17) : {cout<<"Seventeen"; break;}
case (18) : {cout<<"Eighteen"; break;}
case (19) : {cout<<"Ninteen"; break;}}}
else{ switch (tens){
case (2) : {cout<<"Twenty"; break;}
case (3) : {cout<<"Thirty"; break;}
case (4) : {cout<<"Forty"; break;}
case (5) : {cout<<"Fifty"; break;}
case (6) : {cout<<"Sixty"; break;}
case (7) : {cout<<"Seventy"; break;}
case (8) : {cout<<"Eighty"; break;}
case (9) : {cout<<"Ninety"; break;}}
switch (ones){
case (1) : {cout<<" One"; break;}
case (2) : {cout<<" Two"; break;}
case (3) : {cout<<" Three"; break;}
case (4) : {cout<<" Four"; break;}
case (5) : {cout<<" Five"; break;}
case (6) : {cout<<" Six"; break;}
case (7) : {cout<<" Seven"; break;}
case (8) : {cout<<" Eight"; break;}
case (9) : {cout<<" Nine"; break;}}}}
while(number!=0);
cout<<"Exit";
return 0;}

OUTPUT:

WEEK 8

Solution of Exercise 1:
#include<iostream>
using namespace std;
int main(){
int a[5], sum=0;
float avg;
for (int i=0; i<5; i++){
cout<<"Enter element no. " <<i+1 <<": ";
cin>>a[i];
sum+=a[i];}
avg=(float)sum/5;
cout<<"The sum of all numbers is: " <<sum <<endl <<"The average of all numbers is: " <<avg;
return 0;}

OUTPUT:
Solution of Exercise 2:
#include<iostream>
using namespace std;
int main(){

const int n=10;


int arr[n],small;
for (int i=0; i<n; i++){
cout<<"Enter element no. " <<i+1 <<" : ";
cin>>arr[i];}
small=arr[0];
for (int i=0; i<n; i++){
if(small>arr[i]) small=arr[i];}
cout<<"Smallest number is: " <<small;
return 0;}

OUTPUT:
Solution of Exercise 3:
#include<iostream>
using namespace std;
int main(){
int arr1[3][3], arr2[3][3], sum[3][3];
cout<<"Input the element of square matrix (3x3):\n\nInput the elements of 1st matrix:\n";
for(int i=0; i<3; i++){
for(int j=0; j<3; j++){
cout<<"["<<i<<"]"<<"["<<j<<"] : ";
cin>>arr1[i][j];}}
cout<<"\nInput the elements of 2nd matrix:\n";
for(int i=0; i<3; i++){
for(int j=0; j<3; j++){
cout<<"["<<i<<"]"<<"["<<j<<"] : ";
cin>>arr2[i][j];}}
cout<<"\nThe First matrix is: \n";
for(int i=0; i<3;i++){
for(int j=0; j<3; j++){
cout<<arr1[i][j] <<" ";}
cout<<endl;}
cout<<"\nThe Second matrix is: \n";
for(int i=0; i<3;i++){
for(int j=0; j<3; j++){
cout<<arr2[i][j] <<" ";}
cout<<endl;}
cout<<"\nThe Addition of two matrices is: \n";
for(int i=0; i<3;i++){
for(int j=0; j<3; j++){
sum[i][j] = arr1[i][j] + arr2[i][j];
cout<<sum[i][j] <<" ";}
cout<<endl;}
return 0;}
OUTPUT:
WEEK 9
Solution of Exercise 1:
#include<iostream>
#include<conio.h>
#include<process.h>
#include<ctype.h>
using namespace std;
int main()
{
bool a=0,b=0;
while (a==0 || b==0){
cout<<"Enter car registration number: ";
char arr[6];
for(int i=0;i<6;i++){
arr[i]=getche();
}

for(int i=0; i<3;i++)


if (!(a=isalpha(arr[i])))
{cout<<"\nSorry Enter again\n";
break;}
for(int i=3; i<6;i++)
if (!(b=isdigit(arr[i])) && a==1)
{cout<<"\nSorry Enter again\n";
break;}
}
cout<<"\nRegistration Number Verified";

return 0;}

OUTPUT:
Solution of Exercise 2:
#include<iostream>
#include<string>
using namespace std;
int main(){
string str;
int size;
cout<<"Enter URL: ";
cin>>str;
str.erase(0,4);
size=str.size();
str.erase((size-4),size);
cout<<"Name of site: " <<str;

return 0;}

OUTPUT:

WEEK 10
Solution of Exercise 1:

#include<iostream>
using namespace std;
float conv (float);

int main(){
float fah, cel;
cout<<"Enter temperature in Fahrenheit: ";
cin>>fah;
cout<<"Temperature in Celsius is: " <<conv(fah);
return 0;}
float conv(float feh)
{return (feh-32)*5/9;}

OUTPUT:

Solution of Exercise 2:
#include<iostream>
using namespace std;

long hms_to_secs ( int h,int m,int s){


long ans;
h=h*3600;
m=m*60;
ans=h+m+s;
cout << "The value in second is: " <<ans <<"\nExiting Program";
return ans;}

int main(){
int hour, min, sec;
do{
cout<<"Enter hours: ";
cin>>hour;
if (!(hour>0 && hour<=24)) cout<<"op";
cout<<"Enter Minutes: ";
cin>>min;
cout<<"Enter Seconds: ";
cin>>sec;
if (!(hour>=0 && hour<=24 && min>=0 && min<=59 && sec>=0 && sec<=59 )) cout <<"\n\nWrong value
renter\n\n";
}while (!(hour>=0 && hour<=24 && min>=0 && min<=59 && sec>=0 && sec<=59 ));
hms_to_secs ( hour, min, sec);

return 0;}

OUTPUT:
Solution of Exercise 3:
#include <iostream>
#include <conio.h>
#include <string>
using namespace std;

void reverseit (char ch1[])


{
int k=0;
for (int i=0; ch1[i]!='\0';i++) k++;
for (int j=(k-1);j>=0;j--)
cout<<ch1[j];
}

int main()
{

int i,k=0;
char ch[25];
cout<<"Enter any String: ";
cin.get(ch,25);
reverseit (ch);

return 0;
}

OUTPUT:
WEEK 11
Solution of Exercise 1:
#include <iostream>
using namespace std;
int functtime=0;
void funct() {
functtime++;
cout << "I have been called " << functtime << " times" << endl;
}
int main() {
for (int i=0; i<10; i++)
funct();}

OUTPUT:
Solution of Exercise 2:
#include<iostream>
#include<string>
using namespace std;

int sum(int a, int b)


{cout<<a <<" + " <<b <<" = ";
return a+b;}

double sum(double x, double y)


{cout<<x <<" + " <<y <<" = ";
return x+y;}

string sum(string s1, string s2)


{cout<<s1 <<" + " <<s2 <<" = ";
return s1+s2;}

int main(){
int a=10, b=15;
cout<<sum(a,b) <<endl;
double x=3.174, y=138.348;
cout<<sum(x,y) <<endl;
string s1="Hello ", s2="World";
cout<<sum(s1,s2);

return 0;}

OUTPUT:
WEEK 12
Solution of Exercise 1:

#include <iostream>
#include <string>
using namespace std;
struct Employee {
string name;
short id;
int age;
double wage;
};
int main() {
Employee joe;
joe.name = "Joe";
joe.id = 14;
joe.age = 32;
joe.wage = 24.15;
Employee frank = { "Frank",15, 28, 18.27 };
cout<<"NAME\t" <<"ID\t" <<"AGE\t" <<"WAGE\t\n"
<<joe.name <<'\t' <<joe.id <<'\t' <<joe.age <<'\t' <<joe.wage <<endl
<<frank.name <<'\t' <<frank.id <<'\t' <<frank.age <<'\t' <<frank.wage;
return 0;
}

OUTPUT:
Solution of Exercise 2:
#include <iostream>
using namespace std;
struct Distance{
int feets;
double inches;
};
struct cube{
Distance height;
Distance width;
Distance depth;
Distance volume;
};
Distance addeng (Distance , Distance);
void engldisp (Distance);
int main()
{
cube c1;
c1.height.feets = 6;
c1.height.inches = 1;
c1.width.feets = 1;
c1.width.inches = 11;
c1.depth.feets = 9;
c1.depth.inches = 4;
cout<< "Height: "
<<c1.height.feets<<"\'-"
<< c1.height.inches<<"\""
<< endl << "Width: "
<< c1.width.feets<<"\'-"
<< c1.width.inches<<"\"" <<endl
<< "Depth: "
<< c1.depth.feets<<"\'-"
<< c1.depth.inches<<"\"" <<endl;
c1.volume.feets = c1.height.feets*c1.width.feets *c1.depth.feets;
c1.volume.inches= c1.height.inches*c1.width.inches *c1.depth.inches;
while(c1.volume.inches>=12){
c1.volume.inches-=12;
c1.volume.feets++;
}
cout<< "Volume: ";
cout
<< c1.volume.feets<<"\'-"
<< c1.volume.inches<<"\"";
}

OUTPUT:

You might also like