#include <iostream.h> #include <conio.h> void main( ) { int *ptr; int i,n; clrscr(); cout<<”Enter how many elements you want to enter\n”; cin>>n; ptr = new int[n]; cout<<”Enter”<<n<<” elements\n”; for(i=0; i<n;i++) cin>> ptr[i]; cout<<”You have entered \n”; for(i=0; i<n;i++) cout<< ptr[i]<<”\n”; delete ptr; getch(); } Output : Enter how many elements you want to enter 5 Enter 5 elements 1 2 3 4 5 You have entered : 1 2 3 4 5
#include <iostream.h> #include <conio.h> void main( ) { int x=3; int *ptr; clrscr(); ptr = &x; cout<<”Address of x = “<< &x<<”\n”; cout<<”Address in ptr = “<< ptr<<”\n”; cout<<”Address of pointer = “<< &ptr<<”\n” cout<<”Value in x = “<< x<<”\n”; cout<<”Value at address of x = “<< *(&x)<<”\n”; cout<<”Value at pointer = “<< *ptr<<”\n”; getch(); } Output : Address of x = 64482 Address in ptr = 64482 Address of pointer = 64484 Value in x = 3 Value at address of x = 3 Value at pointer = 3
#include <iostream.h> #include <conio.h> class Book { private : int idno; float price; public : void readData( ) { cin>>idno; cin>>price; } void printData( ) { cout<<”Id no =”<<idno<<”\n”; cout<<”Price =”<<price<<”\n”; } }; void main( ) { Book books[5]; cout<<”Enter id no and price for 5 books\n”; for(int i =0; i<5; i++) books[i]. readData( ); cout<<”You have entered :\n”; for(i=0 ; i<5; i++) books[i]. printData( ); } Output : Enter id no and price for 5 books 112 150 224 345 342 120 445 450 566 230 You have entered : Id no = 112 Price = 150 Id no = 224 Price = 345 Id no = 342 Price = 120 Id no = 445 Price = 450 Id no = 566 Price = 230
#include <iostream.h> #include <conio.h> int add(int, int ); // prototype declaration void main( ) { int a,b,c; clrscr( ); cout<<”Enter two numbers\n”; cin>>a>>b; c=add(a,b); cout<<”Ans = “<<c<<”\n”; getch(); } int add(int x, int y ) { int z; z = x + y; return(z); } Output : Enter two numbers 10 20 Ans = 30
#include <iostream.h> #include <conio.h> void fun( ); // prototype declaration void main( ) { clrscr( ); cout<<”We are in main\n”; fun( ); // function call cout<<”Back in main\n”; getch(); } void fun( ) // function definition { cout<<”We are in function\n”; } Output : We are in main We are in function Back in main
#include <iostream.h> #include <conio.h> //Read a number and display weekday accordingly void main( ) { int day; clrscr(); cout<<”Enter a number between 1..7\n”; cin>>day; switch (day) { case 1 : cout<<”Monday\n”; break; case 2 : cout<<”Tuesday\n”; break; case 3 : cout<<”Wednesday\n”; break; case 4 : cout<<”Thursday\n”; break; case 5 : cout<<”Friday\n”; break; case 6 : cout<<”Saturday\n”; break; case 7 : cout<<”Saturday\n”; break; default : cout<<”Invalid input\n”; } getch(); } Output : Enter a number between 1..7 5 Friday
#include <iostream.h> #include <conio.h> void main( ) { int num,denum,ans; clrscr(); cout<<”Enter two numbers\n”; cin>>num>>denum; try { If ( denum ==0) throw(denum); ans = num/denum; cout<<”Ans =”<<ans<<”\n”; } catch(int x) { cout<<”Division by zero is not allowed\n”; } Output : Enter two numbers 10 0 Division by zero is not allowed
#include <iostream.h> #include <conio.h> class Time { private : int hour; int min; public : Time ( ) { hour = min = 0; } Time ( int h, int m) { hour = h; min = m; } void showTime() { cout<<hour<<” : “<<min<<”\n”; } }; void main( ) { Time t1; t1.showTime( ); Time t2(11,30); t2.showTime(); } Output : 0 : 0 11 : 30
#include <iostream.h> #include <conio.h> void main( ) { int num; clrscr(); cout<<”Enter an integer number \n”; cin>>num; cout<<”You have entered :”<<num<<”\n”; getch(); } Output : Enter an integer number 10 You have entered : 10