Nothing Special   »   [go: up one dir, main page]

PF Assignment 1 - Mamoon Mamoon - F2023266192

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 44

Programming Fundamental Lab

Submitted To: Bilal Hussain

SCHOOL OF SYSTEM AND


TECNOLOGY (SST) UNIVERSITY
12/10/2023 OF MANAGEMENT AND
TECNOLOGY

Department: Computer Science (SST/CS)

Email: f2023266192@umt.edu.pk

Submitted By: Muhammad Mamoon Irfan

Student ID: F2023266192 BS(CS)

Assignment No: 1

Announcement date:12/5/2023

Subject: Programming Fundamental Lab

Section: V4
Programming Fundamental Lab

Programing Fundamental Lab


Assignment No 1
Question No: 1
Source Code:
/*
1. Write a C++ program to display your name and a welcome message using the "cout"
statement.
*/
#include <iostream>
using namespace std;
int main()
{
cout<<"Muhammad Mamoon Irfan"<<endl;
cout<<"Welcome";
return 0;
}

1
Programming Fundamental Lab

Object Code:

2
Programming Fundamental Lab

Question No: 2
Source Code:
/*
2. Create a program that takes two integer inputs from the user and displays their sum using
"cout."
*/
#include <iostream>
using namespace std;
int main()
{
int a,b,sum;
cout<<"Enter Two inter value to calculate Sum:";
cin>>a>>b;
sum=a+b;
cout<<"Sum of your given number="<<sum;
return 0;
}

3
Programming Fundamental Lab

Object Code:

4
Programming Fundamental Lab

Question No: 3
Source Code:
/*
3. Write a C++ program that calculates the area of a rectangle. Prompt the user for the
length and width, and then display the result using "cout."
*/
#include <iostream>
using namespace std;
int main()
{
int length,width,area;
cout<<"Enter Two inter value to calculate Area of a Rectangle:";
cin>>length>>width;
area=length*width;
cout<<"Area of a Rectangle="<<area;
return 0;
}

5
Programming Fundamental Lab

Object Code:

6
Programming Fundamental Lab

Question No: 4
Source Code:
/*
4.Develop a program that converts a temperature in Celsius to Fahrenheit. Prompt the user for
the temperature in Celsius, perform the conversion, and display the result using "cout."
*/
#include <iostream>
using namespace std;
int main()
{
double Celsius,Fahrenheit;
cout<<"Enter Temperauture In Calsius:";
cin>>Celsius;
Fahrenheit=(Celsius*9.0/5.0)+32.0;
cout<<"Temperauture In Fahrenheit="<<Fahrenheit;
return 0;
}

7
Programming Fundamental Lab

Object Code:

8
Programming Fundamental Lab

Question No: 5
Source Code:
/*
5. Create a simple calculator program that takes two numbers and an operator (+, -, *, /) as
input and displays the result of the operation using "cout."
*/
#include <iostream>
using namespace std;
int main()
{
float a,b,result;
char op;
cout<<"Enter Two operants to calculate(+,-,/,*)=";
cin>>a>>b;
cout<<"Enter an operator(+,-,/,*)=";
cin>>op;
if(op=='+')
{
result=a+b;
cout<<"Sum of your given is="<<result;
}
else if(op=='-')
{
result=a-b;
cout<<"Subtraction of your given is="<<result;
}
else if(op=='*')
{

9
Programming Fundamental Lab

result=a*b;
cout<<"Multiplication of your given is="<<result;
}
else if(op=='/')
{
result=a/b;
cout<<"Division of your given is="<<result;
}
else
{
cout<<"Enter A right operator";
}
return 0;
}

10
Programming Fundamental Lab

Object Code:

Question No: 6
11
Programming Fundamental Lab

Source Code:
/*
6. Write a program to find the square of a given number. Prompt the user for the input and
display the square using "cout."
*/
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
float num, square;
cout << "Enter A number to calculate Square of Number:";
cin >> num;

if (num >= 0)
{
square = sqrt(num);
cout << "Your Given number square is=" << square;
}
else
{
cout << "Math Error";
}
return 0;
}

12
Programming Fundamental Lab

Object Code:

13
Programming Fundamental Lab

Question No: 7
/*
7. Implement a program that calculates the factorial of a positive integer entered by the user and
displays the result using "cout."

*/
Source Code:
#include <iostream>
using namespace std;

int main() {
int num;
unsigned long long factorial = 1;

cout << "Enter a positive integer: ";


cin >> num;

if (num < 0) {
cout << "Factorial is not defined for negative numbers." <<endl;
}
else if (num == 0) {
cout << "The factorial of 0 is 1" << endl;
}
else {
for (int i = 1; i <= num; ++i) {
factorial *= i;
}
cout << "The factorial of " << num << " is: " << factorial <<endl;
}

14
Programming Fundamental Lab

return 0;
}

Object Code:

15
Programming Fundamental Lab

Question No: 8
Source Code:
/*
8. Create a program that takes a character as input and displays its ASCII value using
"cout."
*/
#include <iostream>
using namespace std;
int main()
{
char a;
cout << "Enter a Character to know about its ASCII value=";
cin >> a;
cout << "The ASCII Value against this" << a << "Character is=" << int(a);

16
Programming Fundamental Lab

return 0;
}

Object Code:

17
Programming Fundamental Lab

Question No: 9
Source Code:
/*
9. Write a program that prompts the user for their name and then displays a personalized
greeting using "cout."
*/
#include <iostream>
using namespace std;
int main()
{
string Name;
cout << "Enter your name to know about personalized greeting=";
cin >>Name;
cout << "Hello, " << Name << "! Welcome to the program." << endl;

18
Programming Fundamental Lab

return 0;
}

Object Code:

19
Programming Fundamental Lab

Question No: 10
Source Code:
/*
10. Develop a program that simulates a simple shopping cart. Allow the user to enter the
names and prices of items, and then display the total cost using "cout."*/
#include <iostream>
using namespace std;
int main()
{
string Name1, Name2, Name3;
int price1, price2, price3,total;
cout << "Enter the name of your First item and its price:";
cin >> Name1 >> price1;
cout << "Enter the name of your second item and its price:";

20
Programming Fundamental Lab

cin >> Name2 >> price2;


cout << "Enter the name of your third item and its price:";
cin >> Name3 >> price3;
total=price1+price2+price3;
cout<<"you Shopping Cart price of these
things"<<Name1<<"+"<<Name2<<"+"<<Name3<<"="<<total;
return 0;
}

Object Code:

21
Programming Fundamental Lab

Question No: 11
Source Code:
/*
11. Create a program that displays the first 10 natural numbers using "cout.""
*/
#include <iostream>
using namespace std;
int main()
{
cout << "1" << endl;
cout << "2" << endl;
cout << "2" << endl;
cout << "3" << endl;
cout << "4" << endl;

22
Programming Fundamental Lab

cout << "5" << endl;


cout << "6" << endl;
cout << "7" << endl;
cout << "8" << endl;
cout << "9" << endl;
cout << "10" << endl;

return 0;
}

Alternate:
#include <iostream>

using namespace std;

int main()
{
int a;
for (a = 1; a <= 10; a++)
{
cout << a << endl;
}
}

23
Programming Fundamental Lab

Object Code:

24
Programming Fundamental Lab

Question No: 12
Source Code:
/*
12. Write a C++ program that displays the multiplication table of a number entered by the
user using "cout."
*/
#include <iostream>
using namespace std;

int main() {
int number;

cout << "Enter a number to display its multiplication table: ";


cin >> number;

cout << "Multiplication Table of " << number << ":" << endl;

cout << number << " * 1 = " << (number * 1) << endl;
cout << number << " * 2 = " << (number * 2) << endl;
cout << number << " * 3 = " << (number * 3) << endl;
cout << number << " * 4 = " << (number * 4) << endl;
cout << number << " * 5 = " << (number * 5) << endl;
cout << number << " * 6 = " << (number * 6) << endl;
cout << number << " * 7 = " << (number * 7) << endl;
cout << number << " * 8 = " << (number * 8) << endl;
cout << number << " * 9 = " << (number * 9) << endl;
cout << number << " * 10 = " <<(number * 10) << endl;

25
Programming Fundamental Lab

return 0;
}

Object Code:

26
Programming Fundamental Lab

Question No: 13
Source Code:
/*
13. Develop a program that converts a distance in kilometers to miles and displays the result
using "cout."
*/
#include <iostream>
using namespace std;
int main()
{
float distance,Miles;
cout << "Enter distance in Kilometer: ";
cin >> distance;
Miles=distance*0.621371;
cout<<"Your Given Distance into miles"<<Miles;
return 0;
}

27
Programming Fundamental Lab

Object Code:

28
Programming Fundamental Lab

Question No: 14
Source Code:
/*
14. Create a simple menu-driven program that displays options to the user and prints their
choice using "cout."
*/
#include <iostream>
using namespace std;

int main()
{
int option;
cout << "Enter your Choice:";
cin >> option;
switch (option)
{
case 1:
{
cout << "Your option is 1";
}
break;
case 2:
{
cout << "Your option is 2";
}
break;
case 3:
{

29
Programming Fundamental Lab

cout << "Your option is 3";


}
break;
case 4:
{
cout << "Your option is 4";
}
break;
default:
{
cout << "Enter please Write option";
}
break;
}
return 0;
}

30
Programming Fundamental Lab

Object Code:

31
Programming Fundamental Lab

Question No: 15
Source Code:
/*
15. Write a program to display a countdown from 10 to 1, each number on a separate line
using "cout."
*/
#include <iostream>
using namespace std;

int main()
{
cout << "10" << endl;
cout << "9" << endl;
cout << "8" << endl;
cout << "7" << endl;
cout << "6" << endl;
cout << "5" << endl;
cout << "4" << endl;
cout << "3" << endl;
cout << "2" << endl;
cout << "1" << endl;

return 0;
}

32
Programming Fundamental Lab

Object Code:

33
Programming Fundamental Lab

Question No: 16
Source Code:
/*
16. Implement a program that displays the square roots of the first 10 positive integers using
"cout."
*/
#include <iostream>
#include <math.h>
using namespace std;

int main()
{
int a=1,b=2,c=3,d=4,e=5,f=6,g=7,h=8,i=9,j=10;
float square;
square=sqrt(a);
cout<<"Square root of a="<<square<<endl;
square=sqrt(b);
cout<<"Square root of a="<<square<<endl;
square=sqrt(c);
cout<<"Square root of a="<<square<<endl;
square=sqrt(d);
cout<<"Square root of a="<<square<<endl;
square=sqrt(e);
cout<<"Square root of a="<<square<<endl;
square=sqrt(f);
cout<<"Square root of a="<<square<<endl;
square=sqrt(g);
cout<<"Square root of a="<<square<<endl;

34
Programming Fundamental Lab

square=sqrt(i);
cout<<"Square root of a="<<square<<endl;
square=sqrt(j);
cout<<"Square root of a="<<square;
return 0;
}

35
Programming Fundamental Lab

Object Code:

36
Programming Fundamental Lab

Question No: 17
Source Code:
/*
17. Create a program that calculates and displays the perimeter of a rectangle. Prompt the
user for the length and width using "cout."
*/
#include <iostream>
using namespace std;

int main()
{
int length,width,perimeter;
cout<<"Enter Two intger value to calculate primeter of a Rectangle:";
cin>>length>>width;
perimeter=2*(length*width);
cout<<"perimeter of a Rectangle="<<perimeter;
return 0;
}

37
Programming Fundamental Lab

Object Code:

38
Programming Fundamental Lab

Question No: 18
Source Code:
/*
18. Write a program to display a pattern of even numbers from 2 to 20 using "cout."
*/
#include <iostream>

using namespace std;

int main()
{
cout << "2" << endl;
cout << "4" << endl;
cout << "6" << endl;
cout << "8" << endl;
cout << "12" << endl;
cout << "14" << endl;
cout << "16" << endl;
cout << "18" << endl;
cout << "20" << endl;
}

Alternate:

#include <iostream>

using namespace std;

39
Programming Fundamental Lab

int main()
{
int a;
for (a = 1; a <= 20; a++)
{
if (a % 2 == 0)
{
cout << a << endl;
}
}
}

40
Programming Fundamental Lab

Object Code:

41
Programming Fundamental Lab

Question No: 19
Source Code:
/*
19. Develop a program that displays the current date and time using "cout."
*/
#include <iostream>
#include <chrono>
#include <ctime>
using namespace std;

int main() {
// Get current time
auto currentTime = chrono::system_clock::to_time_t(chrono::system_clock::now());

// Convert current time to a string


char* dateTime = ctime(&currentTime);

cout << "Current date and time: " << dateTime << endl;

return 0;
}

42
Programming Fundamental Lab

Object Code:

43

You might also like