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

Programming Fundamentals: LAB Task # 1

The document contains code snippets and output for 10 programs written in C++ by Muhammad Ali Qasim (ID: 32566) from the Computer Science department. The programs cover basic concepts like input/output, arithmetic operations, conditional statements, and a simple calculator. Each program is briefly described and includes the code, inputs/outputs to test the program functionality.

Uploaded by

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

Programming Fundamentals: LAB Task # 1

The document contains code snippets and output for 10 programs written in C++ by Muhammad Ali Qasim (ID: 32566) from the Computer Science department. The programs cover basic concepts like input/output, arithmetic operations, conditional statements, and a simple calculator. Each program is briefly described and includes the code, inputs/outputs to test the program functionality.

Uploaded by

Muhammad Ali
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 20

Programming Fundamentals: LAB Task # 1

Name: Muhammad Ali Qasim

ID: 32566

Department: BS (CS)

Compiler Used: CodeBlocks 20.03

 Write a C++ Program to Print Number Entered by User on the output


screen.

Code:
#include <iostream>
using namespace std;

int main()
{
int N;

cout << "Enter an integer: ";


//Reading and storing number in "N"
cin >> N;
//Displaying number entered by the user, stored in "N"
cout << "You entered " << N;
return 0;
}
Output:
 Write a C++ Program to Add Two Numbers Entered by User (Float)
Code:
#include <iostream>
using namespace std;
int main(){
//Declaring two integer variables
float N1, N2;

cout<<"Enter first integer number: ";


// cin is used to capture the user input
//and assign it to the variable.
cin>>N1;

cout<<"Enter second integer number: ";


cin>>N2;
cout<<"Sum of entered numbers is: "<<(N1+N2);
return 0;
}
Output:
 Write a C++ Program to Find Quotient and Remainder.
Code:
#include <iostream>
using namespace std;

int main()
{
int divisor, dividend, quotient, remainder;

cout << "Enter dividend: ";


cin >> dividend;

cout << "Enter divisor: ";


cin >> divisor;

quotient = dividend / divisor;


remainder = dividend % divisor;

cout << "Quotient = " << quotient << endl;


cout << "Remainder = " << remainder;

return 0;
}
Output:
 Write a C++ Program to Swap Two Numbers
Code:
#include <iostream>
using namespace std;

int main()
{

int a, b, temp;
cout << "Before Swapping\n";
cout << "Enter a: ";
cin >> a;
cout << "Enter b: ";
cin >> b;

cout << "a = " << a << ", b = " << b << endl;
temp = a;
a = b;
b = temp;

cout << "\nAfter swapping." << endl;


cout << "a = " << a << ", b = " << b << endl;

return 0;
}
Output:
 Write a C++ Program to Check Whether Number is Even or Odd.
Code:
#include <iostream>
using namespace std;

int main()
{
int N;

cout << "Enter an integer: ";


cin >> N;
//Checking if the number N is even or odd
if ( N % 2 == 0)
cout << N << " is even.";
else
cout << N << " is odd.";

return 0;
}
Output:
 Write a C++ Program to Calculate Sum of three integer Numbers
Code:
#include <iostream>
using namespace std;

int main()
{
int N1, N2, N3, Sum;

cout << "Enter three integers to add\n";


// taking input of three numbers
cin >> N1 >> N2 >> N3;

// adding three numbers


Sum = N1 + N2 + N3;
// displaying sum
cout <<"Sum of three numbers is: " << Sum << endl;
return 0;
}
Output:
 Write a C++ Program to Multiply two Numbers
Code:
#include <iostream>
using namespace std;

int main()
{
int N1, N2, Multi;

cout << "Enter two integers to multiply\n";

// taking input of two numbers


cin >> N1 >> N2;

// multiplying two numbers


Multi = N1 * N2;
// displaying Multi
cout <<"Multiplied two numbers are: " << Multi << endl;
return 0;
}
Output:
 Write a C++ Program to Check Whether a Number is positive
Code:
#include <iostream>

using namespace std;


int main()
{
int num1;
cout << "Enter the number" << endl;
cin>>num1;
if(num1>0){
cout<<num1<<" is a positive number";
else
cout<<num1<<" is not a positive number";

}
return 0;
}
Output:
 Write a C++ Program to Check Whether a Number is negative or Not C++
Code:
#include <iostream>
using namespace std;
int main()
{
int num1;
cout << "Enter the number" << endl;
cin>>num1;
if(num1<0)
cout<<num1<<" is a negative number";

else
cout<<num1<<" is not Negative number";
return 0;
}
Output:
 Program to Make a Simple Calculator to Add, Subtract, Multiply or Divide
Code:
# include <iostream>
using namespace std;

int main ()
{
            int num1,num2;
            char sign;
 cout<< "Enter 1st number="<<endl;
 cin>> num1;

 cout<<"Enter 2nd number="<<endl;


 cin>> num2;
 cout<< "Enter sign\n=";
 cin>>sign;
 if (sign=='+')
 {
             cout<<num1+num2<<endl;
 }
 else if (sign=='-')
 {
             cout<<num1-num2<<endl;
 }
 else if (sign=='*')
 {
             cout<< num1*num2<<endl;
 }
 else if (sign=='/')
 {
             cout<< num1/num2<<endl;
 }
 else
 {
             cout<<"You have enter an invalid sign"<<endl;
 }
 return 0;
}

Output:
 Write C++ program to check whether difference of two number is greater
than 10.
Code:
#include <iostream>
using namespace std;

int main()
{
int N1, N2, diff;

cout << "Enter two integers for difference\n";

// taking input of two numbers


cin >> N1 >> N2;

// difference two numbers


diff = N1 - N2;
if(diff>10)
cout<<" Difference is greater than 10\n";
else
cout<<" Difference is not greater than 10";
return 0;
}
Output:

You might also like