1. Write a c++ program to print the “Hello world!”
#include<iostream>
using namespace std;
int main(){
cout<<"Hello World!";
return 0;
}
2. Write a c++ program add two numbers
#include<iostream>
using namespace std;
int main(){
int n1, n2;
cout<<"Enter the number 1 : ";
cin>>n1;
cout<<"Enter the number 2 : ";
cin>>n2;
int add = n1 + n2;
cout<<"The sum of "<<n1<<" and "<<n2<<" is : "<<add<<endl;
return 0;
}
3. Write a c++ program to explain cascading of insertion (<<) operator and cascading of Extraction (>>).
#include<iostream>
using namespace std;
int main(){
int n1, n2;
cout<<"Enter two numbers : ";
cin>>n1>>n2;
int sub = n1 - n2;
cout<<"\n"<<n1<<" - "<<n2<<" = "<<sub<<endl;
return 0;
}
4. Write a c++ program print the lapmos.com but in the program also write “Hello world, Welcome to lapmos.com" but do not execute this statement.
#include<iostream>
using namespace std;
int main(){
// Hello World, welcome to lapmos.com
cout<<"lapmos.com";
return 0;
}
5. Write a c++ program to print “Hello world!” and each print in a new line 5 times.
#include<iostream>
using namespace std;
int main(){
cout<<"Hello World"<<endl;
cout<<"Hello World"<<endl;
cout<<"Hello World"<<endl;
cout<<"Hello World"<<endl;
cout<<"Hello World"<<endl;
return 0;
}
6. Write a program using setw() as an example.
#include<iostream>
#include<iomanip>
using namespace std;
int main(){
cout<<setw(10)<<"lapmos"<<endl;
cout<<setw(10)<<"lapmos.com"<<endl;
return 0;
}
7. Write a c++ program swap to numbers using third variable.
#include<iostream>
using namespace std;
int main(){
int n1, n2, temp;
cout<<"Enter two Numbers : ";
cin>>n1>>n2;
cout<<"Before Swapping : n1 = "<<n1<<" and n2 = "<<n2<<endl;
temp = n1;
n1 = n2;
n2 = temp;
cout<<"After Swapping : n1 = "<<n1<<" and n2 = "<<n2<<endl;
return 0;
}
8. Write a c++ program swap two number without using third variable.
#include<iostream>
using namespace std;
int main(){
int n1, n2, temp;
cout<<"Enter two Numbers : ";
cin>>n1>>n2;
cout<<"Before Swapping : n1 = "<<n1<<" and n2 = "<<n2<<endl;
n1 = n1 + n2;
n2 = n1 - n2;
n1 = n1 - n2;
cout<<"After Swapping : n1 = "<<n1<<" and n2 = "<<n2<<endl;
return 0;
}
9. Write a c++ program to print the value from 1 to 100.
#include<iostream>
using namespace std;
int main(){
for(int i = 1; i<=100; i++){
cout<<i<<" ";
}
return 0;
}
Comments
Oops!