LAB 1 Introduction to Algorithms and Programming C
LAB 1 Introduction to Algorithms and Programming
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); }
Exercise 1: Write a C++ program that read two numbers and find the addition and subtraction of them
/*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); }
Exercise 2: Write a C++ program that read a number and find the square of that given number
//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); }
Exercise 3: Write a C++ program that swap between two numbers
//program to swap between two numbers #include<iostream> using namespace std; int main() { int num 1, num 2, temp; cout<<"Enter the value for num 1"<<endl; cin>>num 1; cout<<"Enter the value for num 2"<<endl; cin>>num 2; temp=num 1; num 1=num 2; num 2=temp; cout<<"Value of num 1 after swapping"<<num 1<<endl; cout<<"Value of num 2 after swapping"<<num 2<<endl; return(0);
Exercise 4: Write and algorithm to compare three numbers and print the smallest number
//compare three numbers and print the smallest number #include <iostream> using namespace std; int main() { float n 1, n 2, n 3; cout<<"Enter the value for n 1"<<endl; cin>>n 1; cout<<"Enter the value for n 2"<<endl; cin>>n 2; cout<<"Enter the value for n 3"<<endl; cin>>n 3; { } if(n 1 <= n 2 && n 1 <= n 3) { cout << “Smallest number: " << n 1; } if(n 2 <= n 1 && n 2 <= n 3) { cout << " Smallest number: " << n 2; } if(n 3 <= n 1 && n 3 <= n 2) cout << " Smallest number: " << n 3; } return 0;
- Slides: 10