Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Algorithms and Programming

Similar presentations


Presentation on theme: "Introduction to Algorithms and Programming"— Presentation transcript:

1 Introduction to Algorithms and Programming
LAB 1 Introduction to Algorithms and Programming

2 C++ Program structure // My first program in C++
/*To Explain the C++ program structure This is the Best Example Program*/ #include<iostream> using namespace std; int main( ) { cout<<"This is the First Program \n"; cout<<"University of Nizwa"; return(0); }

3 Exercise 1: Write a C++ program that read two numbers and find the addition and subtraction of them

4 #include<iostream> using namespace std; int main() {
/*this program read two numbers and find the addition and subtraction of them*/ #include<iostream> using namespace std; int main() { int a,b,c,d; cout<<"Enter the value for a"<<endl; cin>>a; cout<<"Enter the value for b"<<endl; cin>>b; c=a+b; d=a-b; cout<<"Result of addition"<<c<<endl; cout<<"Result of subtraction"<<d<<endl; return(0); }

5 Exercise 2: Write a C++ program that read a number and find the square of that given number

6 //This program read a number and find the square of that given number
#include<iostream> using namespace std; int main() { int number, result; cout<<"Enter the value for number"<<endl; cin>>number; result =number*number; cout<<"The result is"<< result <<endl; return(0); }

7 Exercise 3: Write a C++ program that swap between two numbers

8 //program to swap between two numbers
#include<iostream> using namespace std; int main() { int num1,num2,temp; cout<<"Enter the value for num1"<<endl; cin>>num1; cout<<"Enter the value for num2"<<endl; cin>>num2; temp=num1; num1=num2; num2=temp; cout<<"Value of num1 after swapping"<<num1<<endl; cout<<"Value of num2 after swapping"<<num2<<endl; return(0);

9 Exercise 4: Write and algorithm to compare three numbers
and print the smallest number

10 //compare three numbers and print the smallest number
#include <iostream> using namespace std; int main() { float n1, n2, n3; cout<<"Enter the value for n1"<<endl; cin>>n1; cout<<"Enter the value for n2"<<endl; cin>>n2; cout<<"Enter the value for n3"<<endl; cin>>n3; if(n1 <= n2 && n1 <= n3) cout << “Smallest number: " << n1; } if(n2 <= n1 && n2 <= n3) cout << " Smallest number: " << n2; if(n3 <= n1 && n3 <= n2) cout << " Smallest number: " << n3; return 0;


Download ppt "Introduction to Algorithms and Programming"

Similar presentations


Ads by Google