0% found this document useful (0 votes)
18 views29 pages

Week 1: Bdul QUDOOS

Uploaded by

qudoosmemon300
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)
18 views29 pages

Week 1: Bdul QUDOOS

Uploaded by

qudoosmemon300
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/ 29

Department of Software Engineering

Faculty of Engineering and Technology


University of Sindh

Class: BSSW Part 1 (Morning)


Semester: First Semester
Course: Programming Fundamentals
Full Name: Abdul QUDOOS
Roll No. 2K24/SWE/11

WEEK 1
WEEK 2

EXERCISE 1:

#include <iostream>
using namespace std;
int main(){
int r;
cout<<"Enter the radius of the sphere: ";
cin>>r;
r=r*r*r;
float pi=3.14159;
float volume = 4 * pi * r/3;
cout<<"The volume of sphere is: "<<volume;
return 0;
}

OUTPUT:

EXERCISE 2:
#include<iostream>
using namespace std;
int main(){
int k;
cout<<"Enter temprature in Kelvin: ";
cin>>k;
float f=(k - 273.15) / 5 * 9 + 32;
cout<<"Temprature in Fahrenheit: "<<f;
return 0;
}
OUTPUT:

EXERCISE 3:
#include<iostream>
using namespace std;
int main(){
int sec;
cout<<"Enter the time in seconds: ";
cin>>sec;
int hours=sec/3600;
sec=sec-(3600*hours);
int min=sec/60;
sec=sec%60;
cout<<"Hours : "<<hours<<endl;
cout<<"Minutes : "<<min<<endl;
cout<<"Seconds : "<<sec;
return 0;
}
OUTPUT:

WEEK 3
EXERCISE 1:
#include <iostream>
using namespace std;
int main(){
cout<<"\"Welcome\" is a String Constant \n"
<<"\'A\' is character constant\n"
<<"\\t is for new tab \n"
<<"\\n is for new line"; return 0;
}

OUTPUT:

EXERCISE 2 :

#include <iostream>
using namespace std;
int main(){
int num1, num2;
cout<<"Enter first number: ";
cin>>num1;
cout<<"Enter second number: ";
cin>>num2;
cout<<num1<<" + "<<num2<<" = "<<num1+num2<<endl;
cout<<num1<<" - "<<num2<<" = "<<num1-num2<<endl;
cout<<num1<<" x "<<num2<<" = "<<num1*num2<<endl;
cout<<num1<<" / "<<num2<<" = "<<(float)num1/num2<<endl;
cout<<num1<<" % "<<num2<<" = "<<num1%num2<<endl;
return 0;
}

OUTPUT:
EXERCISE 3 :

#include <iostream>
using namespace std;
int main(){
int num1,num2,num3,sum;
cout<<"Enter three number: ";
cin>>num1;
cin>>num2;
cin>>num3;
sum=num1+num2+num3;
cout<<"Sum = "<<sum<<"\n Average = "<<sum/3;
return 0;
}

OUTPUT :

EXERCISE 4 :

#include<iostream>
using namespace std;
int main(){
int initialSpeed;
int acc;
int time;
cout<<"Enter the initial speed of the car (m/s): ";
cin>>initialSpeed;
cout<<"Enter the acceleration of the car (m/s\xFD): ";
cin>>acc;
cout<<"Enter the time traveled (s): ";
cin>>time;
int timeSquare=time*time;
float distance = initialSpeed * time + 0.5 * acc * timeSquare;
cout<<"TOTAL DISTANCE COVERED BY CAR: "<<distance;
return 0;
}
OUTPUT :

WEEK 4

EXERICE 1 :

#include<iostream>
#include<cmath>
using namespace std;
int main(){
float a;
float b;
cout<<"----------------------------------------------\n";
cout<<"Please do not enter Zero or any negative value.\n";
cout<<"----------------------------------------------\n";
cout<<"Enter first value : ";
cin>>a;
cout<<"Enter second value: ";
cin>>b;
// formula to calculate area
double area= 0.5 * b * (sqrt( pow(a,2) - pow(b,2) / 4));
cout<<"Area : "<<area;
return 0;
}
OUTPUT :

EXERCISE 2 :

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

OUTPUT:

WEEK 5

EXERCISE 1 :
#include<iostream>
#include<cmath>
using namespace std;
int main(){
for(int i=0;i<10;i++)
cout<<pow(7,i)<<" ";
return 0;
}

OUTPUT :

EXERCISE 2 :

#include <iostream>
using namespace std;
int main(){
// temp variable for storing num value, as it will be equal to zero in the end.
int num, sum, temp;
cout<<"Enter a number: ";
cin>>num;
temp=num;
while(num!=0){
sum+=num%10;
num/=10;
}
cout<<"The sum of digits of "<<temp<<" is "<<sum;
}

OUTPUT :
EXERCISE 3 :

#include <iostream>
using namespace std;
int main() {
int num;
cout<<"Enter a number to calculate Factorial: ";
cin>>num;
int temp=1;
for(int i=num;i>=1;i--){
cout<<i<<" x ";
temp*=i;
}
/* Here we use \b\b\b to erase the last " x " from the output.
\b is a backspace character that moves the cursor back one position.
Using it three times moves the cursor back over the last " x " we printed.*/
cout<<"\b\b\b";
cout<<" = "<<temp;
return 0;
}

OUTPUT :

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<<" * "<<i<<" = "<<table*i<<endl;
}
return 0;
}

OUTPUT :

WEEK 6

EXERCISE 1 :

#include<iostream>
using namespace std;
int main(){
int day;
cout<<"Input day number(1-7): ";
cin>>day;
switch(day){
case 1:cout<<"Monday"; break;
case 2:cout<<"Tuesday"; break;
case 3:cout<<"Wednesday"; break;
case 4:cout<<"Thrusday"; break;
case 5:cout<<"Friday"; break;
case 6:cout<<"Saturday"; break;
case 7:cout<<"Sunday"; break;
default: cout<<"Please input valid day number.";
}
}

OUTPUT :

EXERCISE 2 :

#include <iostream>
using namespace std;
int main() {
int month;
cout << "Enter 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" << endl;
break;
case 4:
case 6:
case 9:
case 11:
cout << "Total number of days = 30" << endl;
break;
case 2:
cout << "Total number of days = 28" << endl;
break;
default:
cout << "Invalid month number!" << endl;
}
return 0;
}
OUTPUT :

EXERCISE 3 (SWITCH METHOD) :

#include <iostream>
using namespace std;
int main() {
char ch;
// Input character
cout << "Enter a character: ";
cin >> ch;
switch(ch) {
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
cout << "'" << ch << "' is a vowel" << endl;
break;
default:
cout << "'" << ch << "' is a consonant" << endl;
}
return 0;
}

EXERCISE 3 (IF ELSE METHOD) :

#include <iostream>
using namespace std;
int main() {
char ch;
cout << "Enter an alphabet: ";
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." << endl;
}
else if((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')) {
cout << "'" << ch << "' is a consonant." << endl;
}
else {
cout << "Invalid input. Please enter an alphabet." << endl;
}
return 0;
}

OUTPUT :
Trying different outputs here.
EXERCISE 4 :

#include <iostream>
#include <iomanip>
using namespace std;
int main() {
int count=0;
for (int i = 129; i <= 255; ++i) {
if(count==8){
count=0;
cout<<endl;
}
cout << setw(5) << i << setw(5) << (char)(i) << setw(5);
count++;
}
cout << endl;
return 0;
}

OUTPUT :
WEEK 7

EXERCISE 1 :

#include <iostream>
using namespace std;
int main() {
char ch;
cout << "Input character: ";
cin.get(ch);
if (ch == '\n') {
cout << "Exit";
return 0;
}
if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))
cout << "'" << ch << "' is alphabet";
else if (ch >= '0' && ch <= '9')
cout << "'" << ch << "' is a number";
else
cout << "'" << ch << "' is special character";
return 0;
}
OUTPUT :

EXERCISE 2 :

#include<iostream>
using namespace std;
int main(){
int rows=10;
for (int i = 1; i <=rows ; i++) {
for (int j = 1; j <=i ; j++) {
cout<<"<";
}
cout<<" - ";
for (int k = rows-i; k>=0 ; k--) {
cout<<">";
cout<<endl;
}
return 0;
}

OUTPUT :
EXERCISE 3 :

#include<iostream>
using namespace std;
int main(){
for (int i = 1; i <=5 ; i++) {
for (int j =5; j >=i ; j--) {
cout<<" ";
}
for (int r = 1; r <=i ; r++) {
cout<<"*";
}
for (int l = 2; l <=i ; l++) {
cout<<"*";
}
cout<<endl;
}
return 0;
}

OUTPUT :

EXERCISE 4 :

#include<iostream>
#include<conio.h>
using namespace std;
int main()
{ int num,unit,tens;
do{
cout<<endl <<"Enter the number(1-99): ";
cin>>num;
if (num < 1 || num > 99) {
cout << "Number out of range, please enter a number between 1 and 99." << endl;
continue;
}
unit=num % 10;
tens=num / 10;
switch (tens)
{ case 1:
switch(unit)

{
case 0:cout<<"Ten"; break;
case 1:cout<<"Eleven"; break;
case 2:cout<<"Twelve"; break;
case 3:cout<<"Thirteen"; break;
case 4:cout<<"Fourteen"; break;
case 5:cout<<"Fifteen"; break;
case 6:cout<<"Sixteen"; break;
case 7:cout<<"Seventeen"; break;
case 8:cout<<"Eighteen"; break;
case 9:cout<<"Ninteen"; break;
}break;
case 2:cout<<"Twenty"; break;
case 3:cout<<"Thirty"; break;
case 4:cout<<"Fourty"; break;
case 5:cout<<"Fifty"; break;
case 6:cout<<"Sixty"; break;
case 7:cout<<"Seventy"; break;
case 8:cout<<"Eighty"; break;
case 9:cout<<"Ninty"; break;
}
switch(tens)
{ case 1: cout<<""; break;
default:
switch(unit)
{
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 8:cout<<" Eight"; break;
case 9:cout<<" Nine"; break;
} break;
}
}while(num != '\r' );
return 0;
}

OUTPUT :

WEEK 8

EXERCISE 1 :

#include <iostream>
using namespace std;
int main() {
int a[5];
int sum = 0;
float average;
cout << "Enter 5 integers:" << endl;
for(int i = 0; i < 5; i++) {
cout << "Enter element " << i+1 << ": ";
cin >> a[i];
sum += a[i];
}
// Calculate the average of the elements
average = (float) (sum) / 5;
cout << "The sum of the elements is: " << sum << endl;
return 0;
}

OUTPUT :

EXERCISE 2 :

#include <iostream>
using namespace std;
int main() {
int size;
cout<<"Enter the size of array: ";
cin>>size;
int a[size];
// Take input for the array elements from the user
cout << "Enter " << size << " integers:" << endl;
for (int i = 0; i < size; i++) {
cout << "Enter element " << i + 1 <<": ";
cin >> a[i];
}
// Initialize the smallest variable to the first element of the array
int smallest = a[0];
// Find the smallest element in the array
for (int i = 1; i < size; i++) {
if (a[i] < smallest) {
smallest = a[i];
}
}
// Print the smallest element
cout << "The smallest element in the array is: " << smallest << endl;
return 0;
}

OUTPUT :

EXERCISE 3 :

#include <iostream>
using namespace std;
int main() {
int matrix1[3][3], matrix2[3][3], result[3][3];
// Input for first matrix
cout << "Input elements in the first matrix:" << endl;
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 3; ++j) {
cout << "[" << i << "][" << j << "] : ";
cin >> matrix1[i][j];
}
}
// Input for second matrix
cout << "Input elements in the second matrix:" << endl;
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 3; ++j) {
cout << "[" << i << "][" << j << "] : ";
cin >> matrix2[i][j];
}
}
// Addition of two matrices
for (int i = 0; i < 3; ++i) {

for (int j = 0; j < 3; ++j) {


result[i][j] = matrix1[i][j] + matrix2[i][j];
}
}
// Displaying the result
cout << "The First matrix is :" << endl;
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 3; ++j) {
cout << matrix1[i][j] << " ";
}
cout << endl;
}
cout << "The Second matrix is :" << endl;
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 3; ++j) {
cout << matrix2[i][j] << " ";
}
cout << endl;
}
cout << "The Addition of two matrices is :" << endl;
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 3; ++j) {
cout << result[i][j] << " ";
}
cout << endl;
}
return 0;
}

OUTPUT :

WEEK 9

EXERCISE 1 :
#include <iostream>
#include <ctype.h>
using namespace std;
int main() {
string regNumber;
while (true) {
cout << "Enter car registration number: ";
cin >> regNumber;
// Check if the length is exactly 6 characters
if (regNumber.length() != 6) {
cout << "Sorry, Enter again" << endl;
continue;
}
// Check if the first three characters are alphabetic
bool valid = true;
for (int i = 0; i < 3; ++i) {
if (!isalpha(regNumber[i])) {
valid = false;
break;
}
}
// Check if the last three characters are numeric
if (valid) {
for (int i = 3; i < 6; ++i) {
if (!isdigit(regNumber[i])) {
valid = false;
break;
}
}
}
if (valid) {
cout << "Registration Number Verified" << endl;
break;
} else {
cout << "Sorry, Enter again" << endl;
}
}
return 0;
}

OUTPUT :
EXERCISE 2 :

#include <iostream>
#include <string>
using namespace std;
int main() {
string url;
cout << "Enter URL: ";
cin >> url;
if (url.substr(0, 4) == "www." && url.substr(url.length() - 4, 4) == ".com") {
// Extract the name of the site
string siteName = url.substr(4, url.length() - 8); // Removing "www." and ".com"
cout << "Name of Site: " << siteName << endl;
} else {
cout << "Invalid URL. Please enter a URL that starts with 'www.' and ends with
'.com'" << endl;
}
return 0;
}

OUTPUT :
WEEK 10

EXERCISE 1 :

#include <iostream>
using namespace std;
// Function to convert Fahrenheit to Celsius
double fahrenheitToCelsius(double fahrenheit) {
return (fahrenheit - 32) * 5.0 / 9.0;
}
int main() {
double fahrenheit;
cout << "Enter Temperature in Fahrenheit: ";
cin >> fahrenheit;
double celsius = fahrenheitToCelsius(fahrenheit);
cout << "Temperature in Celsius is: " << celsius << endl;
return 0;
}

OUTPUT :

EXERCISE 2 :

#include <iostream>
using namespace std;
// Function to convert hours, minutes, and seconds to total seconds
long hms_to_secs(int hours, int minutes, int seconds) {
return hours * 3600 + minutes * 60 + seconds;
}
int main() {
int hours, minutes, seconds;
while (true) {
cout << "Enter Hours: ";
cin >> hours;
if (hours < 0 || hours > 24) {
cout << "Wrong Value, Renter" << endl;
continue;
}
cout << "Enter Min: ";
cin >> minutes;
if (minutes < 0 || minutes >= 60) {
cout << "Wrong Value, Renter" << endl;
continue;
}
cout << "Enter Sec: ";
cin >> seconds;
if (seconds < 0 || seconds >= 60) {
cout << "Wrong Value, Renter" << endl;
continue;
}
long totalSeconds = hms_to_secs(hours, minutes, seconds);
cout << "The Value in Seconds is: " << totalSeconds << endl;
break;
}
cout << "Exiting Program" << endl;
return 0;
}

OUTPUT :
EXERCISE 3 :

#include <iostream>
#include <cstring> // For strlen()
using namespace std;
// Function to reverse a C-string
void reversit(char str[]) {
int length = strlen(str);
for (int i = 0; i < length / 2; ++i) {
// Swap the characters
char temp = str[i];
str[i] = str[length - 1 - i];
str[length - 1 - i] = temp;
}
}
int main() {
const int MAX_LENGTH = 100;
char str[MAX_LENGTH];
cout << "Enter a string: ";
cin.getline(str, MAX_LENGTH);
reversit(str);
cout << "Reversed string: " << str << endl;
return 0;
}

OUTPUT :

You might also like