CS-1 (C++ Programs For Practice)
CS-1 (C++ Programs For Practice)
CS-1 (C++ Programs For Practice)
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
cout <<"Hello World\n";
getch();
}
Character Constant
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
cout <<"Hello World\n";
cout<<"This is CS class\t";
cout<<"Welcome";
getch();
}
Arithmetic Operators
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int n1,n2,result;
cout<<"Enter the 1st Number:\t";
cin>>n1;
cout<<"\nEnter the 2nd Number:\t";
cin>>n2;
result=n1+n2;
cout<<"Addition of two numbers is:\t"<<result<<"\n";
result=n1-n2;
cout<<"Subtraction is:\t"<<result<<endl;
result=n1*n2;
cout<<"Multiplication is:\t"<<result<<endl;
result=n1/n2;
cout<<"Division is:\t"<<result<<endl;
result=n1%n2;
cout<<"Modulus is:\t"<<result<<endl;
getch();
}
Sizeof
#include<iostream.h>
#include<conio.h>
void main() {
clrscr();
cout << "Size of char : " << sizeof(char)<<endl;
cout << "Size of int : " << sizeof(int) << endl;
cout << "Size of short int : " << sizeof(short int) << endl;
cout << "Size of long int : " << sizeof(long int) << endl;
cout << "Size of float : " << sizeof(float) << endl;
cout << "Size of double : " << sizeof(double) << endl;
getch();
}
Type cast
#include <iostream.h>
#include<conio.h>
void main() {
clrscr();
double a = 21.09399;
float b = 10.20;
int c;
c=(int)a;
cout << "Line 1 - Value of (int)a is :" << c << endl ;
c = (int) b;
cout << "Line 2 - Value of (int)b is :" << c << endl ;
getch();
}
if – else statement
#include<iostream.h>
#include<conio.h>
void main()
clrscr();
int a,b;
cin>>a;
cin>>b;
if(a>b)
cout<<"1st no is greater"<<endl;
else
cout<<"2nd no is greater"<<endl;
getch(); }
Compound Conditions in if(multiple if’s)
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a,b,c;
cout<<"Enter the 3 numbers\t";
cin>>a>>b>>c;
if(a>b && a>c)
{
cout<<"a is greater";
}
if(b>a && b>c)
cout<<"b is greater";
if(c>a && c>b)
cout<<"c is greater";
getch();
}
#include<iostream.h>
#include<conio.h>
void main()
int a,b,c;
if (a >= b)
{
if (a >= c)
else
else {
if (b >= c)
else
getch();
else-if ladder
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int t;
cin>>t;
cout<<"Distinction";
cout<<"First class";
cout<<"second class";
cout<<"fail";
else
cout<<"invalid";
getch();
SWITCH STATEMENT
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int day;
cout<<"Enter the day of week between 1-7:\t";
cin>>day;
switch(day)
{
case 1:
cout<<"\n Monday";
break;
case 2:
cout<<"\n Tuesday";
break;
case 3:
cout<<"\n Wednesday";
break;
case 4:
cout<<"\n Thursday";
break;
case 5:
cout<<"\n Friday";
break;
case 6:
cout<<"\n Saturday";
break;
case 7:
cout<<"\n Sunday";
break;
default:
cout<<"\n Invalid input";
break;
}
getch();
}
UNCONDITIONAL CONTROL STRUCTURE (goto)
#include <iostream.h>
#include<conio.h>
void main()
int num;
cin>>num;
if (num % 2==0){
goto even;
else {
goto odd;
even:
cout<<"Even Number";
goto end;
odd:
cout<<"odd number";
end:
getch();
for statement
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i;
for(i=1;i<=10;i++)
{
cout<<i<<endl;
}
getch();
}
Nested for loop
#include <iostream.h>
#include<conio.h>
void main() {
int rows = 5;
int columns = 3;
getch();
#include<conio.h>
void main()
clrscr();
int a,b,temp;
cin>>a>>b;
temp=a;
a=b;
b=temp;
getch();