0% found this document useful (0 votes)
52 views38 pages

Programs LMS

The document contains a list of 25 programming exercises related to basic C++ concepts like loops, functions, classes, arrays etc. along with sample code and output for some of the programs. It also includes a brief explanation of cin and cout functions in C++. The programs are aimed at helping students learn and practice common programming structures through short programs.

Uploaded by

SHYAM
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
52 views38 pages

Programs LMS

The document contains a list of 25 programming exercises related to basic C++ concepts like loops, functions, classes, arrays etc. along with sample code and output for some of the programs. It also includes a brief explanation of cin and cout functions in C++. The programs are aimed at helping students learn and practice common programming structures through short programs.

Uploaded by

SHYAM
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 38

SSN College of Engineering

Department of Information Technology

Contents:

S. N. Program Title Link to access program


1. Program to print a given number n times Program 1
2. Sum of 1 to n using while loop Program 2
3. Program to reverse a number Program 3
4. Sum of random input values until 0 is given (do..while) Program 4
5. Print iteration number using for loop Program 5
6. Negative initialization in for loop Program 6
7. Multidimensional arrays Program 7
8. Get string input Program 8
9. sizeof Program 9
10. Length of the string Program 10
11. String Arrays Program 11
12. Different datatypes and Multi-dimensional arrays Program 12
13. String programs in PPT PPT Programs
14. Goto and Pointers Program 14
15. Switch case Program 15
16. Functions – To return the larger number Program 16
17. Functions – GCD Program 17
18. Classes in C++ Program 18
19. Sum and difference of two matrices using functions Program 19
20. Program to compute the area of the rectangle using a Program 20
constructor in C++
21. Increment and decrement operators Program 21
22. Simple program to study class syntax Program 22
23. Function definition outside the class Program 23
24. Function overloading Program 24
25. Call by value and Call by reference Program 25

/*
std::cin and std::cout always go on the left-hand side of the statement.
std::cout is used to output a value (cout = character output)
std::cin is used to get an input value (cin = character input)
<< is used with std::cout, and shows the direction that data is moving (if std::cout represents
the console, the output data is moving from the variable to the console). std::cout << 5 moves
the value of 5 to the console
>> is used with std::cin, and shows the direction that data is moving (if std::cin represents the
keyboard, the input data is moving from the keyboard to the variable). std::cin >> x moves
the value the user entered from the keyboard into x
*/

Prepared by: J. K. Josephine Julina Page |1


SSN College of Engineering
Department of Information Technology
//Program to print a given number n times
#include <iostream>
using namespace std;
int main()
{
int num, count, i=1;
cout<<"Enter a number: ";
cin>>num;
cout<<"Enter number of terms to be printed: ";
cin>>count;
if(num>0)
{
while(i!=count+1)
{
cout<<num<<"\t";
i++;
}
}
return 0;
}

Enter a number: 8
Enter number of terms to be printed: 8
8 8 8 8 8 8 8 8

//Program - while
#include <iostream>
int main()
{
int sum=0,val = 1,limit;
std::cout<<"Enter a limit: ";
std::cin>>limit;
while(val<=limit) {
sum+=val;
val++;
}
std::cout<<"Sum of 1 to "<<limit<<" is "<<sum;
return 0;
}
Enter a limit: 5
Sum of 1 to 5 is 15

Prepared by: J. K. Josephine Julina Page |2


SSN College of Engineering
Department of Information Technology
//Program to reverse a number
#include <iostream>
using namespace std;
int main()
{
int num, rem;
cout<<"Enter a number"<<endl;
cin>>num;
while(num>0) {
rem = num%10;
cout<<rem;
num = num/10;
}
return 0;
}
Enter a number
45
54

//Program - do....while
#include <iostream>
using namespace std;
int main(){
int sum=0,num;
do
{
cout<<"Enter a number\n";
cin>>num;
sum+=num;
} while(num!=0);
cout<<"Sum of all the entered values = "<<sum;
return 0;
}

Enter a number
12
Enter a number
34
Enter a number
0
Sum of all the entered values = 46

Prepared by: J. K. Josephine Julina Page |3


SSN College of Engineering
Department of Information Technology
//Program - for loop
#include <iostream>
using namespace std;
int main()
{
int i,limit;
cout<<"Enter a limit: ";
cin>>limit;
for(i=1;i<=limit;i++)
cout<<"This is "<<"Iteration "<<i<<"\n";
return 0;
}

Enter a limit: 4
This is Iteration 1
This is Iteration 2
This is Iteration 3
This is Iteration 4

//Negative initialization in for loop


#include <iostream>
using namespace std;
int main()
{
int sum=0;
for(int i=-100; i<=100;++i)
{
cout<<"\nIteration "<<i;
sum+=i;
cout<<"\nIntermediate sum "<<sum;
}
cout<<"\nFinal sum is "<<sum;
return 0;
}

Iteration -100
Intermediate sum -100
Iteration -99
Intermediate sum -199
Iteration -98
Intermediate sum -297
Iteration -97
Intermediate sum -394
Iteration -96
Intermediate sum -490

Prepared by: J. K. Josephine Julina Page |4


SSN College of Engineering
Department of Information Technology
Iteration -95
Intermediate sum -585
Iteration -94
Intermediate sum -679
Iteration -93
Intermediate sum -772
Iteration -92
Intermediate sum -864
Iteration -91
Intermediate sum -955
Iteration -90
Intermediate sum -1045
Iteration -89
Intermediate sum -1134
Iteration -88
Intermediate sum -1222
Iteration -87
Intermediate sum -1309
Iteration -86
Intermediate sum -1395
Iteration -85
Intermediate sum -1480
Iteration -84
Intermediate sum -1564
Iteration -83
Intermediate sum -1647
Iteration -82
Intermediate sum -1729
Iteration -81
Intermediate sum -1810
Iteration -80
Intermediate sum -1890
Iteration -79
Intermediate sum -1969
Iteration -78
Intermediate sum -2047
Iteration -77
Intermediate sum -2124
Iteration -76
Intermediate sum -2200
Iteration -75
Intermediate sum -2275
Iteration -74
Intermediate sum -2349
Iteration -73
Intermediate sum -2422

Prepared by: J. K. Josephine Julina Page |5


SSN College of Engineering
Department of Information Technology
Iteration -72
Intermediate sum -2494
Iteration -71
Intermediate sum -2565
Iteration -70
Intermediate sum -2635
Iteration -69
Intermediate sum -2704
Iteration -68
Intermediate sum -2772
Iteration -67
Intermediate sum -2839
Iteration -66
Intermediate sum -2905
Iteration -65
Intermediate sum -2970
Iteration -64
Intermediate sum -3034
Iteration -63
Intermediate sum -3097
Iteration -62
Intermediate sum -3159
Iteration -61
Intermediate sum -3220
Iteration -60
Intermediate sum -3280
Iteration -59
Intermediate sum -3339
Iteration -58
Intermediate sum -3397
Iteration -57
Intermediate sum -3454
Iteration -56
Intermediate sum -3510
Iteration -55
Intermediate sum -3565
Iteration -54
Intermediate sum -3619
Iteration -53
Intermediate sum -3672
Iteration -52
Intermediate sum -3724
Iteration -51
Intermediate sum -3775
Iteration -50
Intermediate sum -3825

Prepared by: J. K. Josephine Julina Page |6


SSN College of Engineering
Department of Information Technology
Iteration -49
Intermediate sum -3874
Iteration -48
Intermediate sum -3922
Iteration -47
Intermediate sum -3969
Iteration -46
Intermediate sum -4015
Iteration -45
Intermediate sum -4060
Iteration -44
Intermediate sum -4104
Iteration -43
Intermediate sum -4147
Iteration -42
Intermediate sum -4189
Iteration -41
Intermediate sum -4230
Iteration -40
Intermediate sum -4270
Iteration -39
Intermediate sum -4309
Iteration -38
Intermediate sum -4347
Iteration -37
Intermediate sum -4384
Iteration -36
Intermediate sum -4420
Iteration -35
Intermediate sum -4455
Iteration -34
Intermediate sum -4489
Iteration -33
Intermediate sum -4522
Iteration -32
Intermediate sum -4554
Iteration -31
Intermediate sum -4585
Iteration -30
Intermediate sum -4615
Iteration -29
Intermediate sum -4644
Iteration -28
Intermediate sum -4672
Iteration -27
Intermediate sum -4699

Prepared by: J. K. Josephine Julina Page |7


SSN College of Engineering
Department of Information Technology
Iteration -26
Intermediate sum -4725
Iteration -25
Intermediate sum -4750
Iteration -24
Intermediate sum -4774
Iteration -23
Intermediate sum -4797
Iteration -22
Intermediate sum -4819
Iteration -21
Intermediate sum -4840
Iteration -20
Intermediate sum -4860
Iteration -19
Intermediate sum -4879
Iteration -18
Intermediate sum -4897
Iteration -17
Intermediate sum -4914
Iteration -16
Intermediate sum -4930
Iteration -15
Intermediate sum -4945
Iteration -14
Intermediate sum -4959
Iteration -13
Intermediate sum -4972
Iteration -12
Intermediate sum -4984
Iteration -11
Intermediate sum -4995
Iteration -10
Intermediate sum -5005
Iteration -9
Intermediate sum -5014
Iteration -8
Intermediate sum -5022
Iteration -7
Intermediate sum -5029
Iteration -6
Intermediate sum -5035
Iteration -5
Intermediate sum -5040
Iteration -4
Intermediate sum -5044

Prepared by: J. K. Josephine Julina Page |8


SSN College of Engineering
Department of Information Technology
Iteration -3
Intermediate sum -5047
Iteration -2
Intermediate sum -5049
Iteration -1
Intermediate sum -5050
Iteration 0
Intermediate sum -5050
Iteration 1
Intermediate sum -5049
Iteration 2
Intermediate sum -5047
Iteration 3
Intermediate sum -5044
Iteration 4
Intermediate sum -5040
Iteration 5
Intermediate sum -5035
Iteration 6
Intermediate sum -5029
Iteration 7
Intermediate sum -5022
Iteration 8
Intermediate sum -5014
Iteration 9
Intermediate sum -5005
Iteration 10
Intermediate sum -4995
Iteration 11
Intermediate sum -4984
Iteration 12
Intermediate sum -4972
Iteration 13
Intermediate sum -4959
Iteration 14
Intermediate sum -4945
Iteration 15
Intermediate sum -4930
Iteration 16
Intermediate sum -4914
Iteration 17
Intermediate sum -4897
Iteration 18
Intermediate sum -4879
Iteration 19
Intermediate sum -4860

Prepared by: J. K. Josephine Julina Page |9


SSN College of Engineering
Department of Information Technology
Iteration 20
Intermediate sum -4840
Iteration 21
Intermediate sum -4819
Iteration 22
Intermediate sum -4797
Iteration 23
Intermediate sum -4774
Iteration 24
Intermediate sum -4750
Iteration 25
Intermediate sum -4725
Iteration 26
Intermediate sum -4699
Iteration 27
Intermediate sum -4672
Iteration 28
Intermediate sum -4644
Iteration 29
Intermediate sum -4615
Iteration 30
Intermediate sum -4585
Iteration 31
Intermediate sum -4554
Iteration 32
Intermediate sum -4522
Iteration 33
Intermediate sum -4489
Iteration 34
Intermediate sum -4455
Iteration 35
Intermediate sum -4420
Iteration 36
Intermediate sum -4384
Iteration 37
Intermediate sum -4347
Iteration 38
Intermediate sum -4309
Iteration 39
Intermediate sum -4270
Iteration 40
Intermediate sum -4230
Iteration 41
Intermediate sum -4189
Iteration 42
Intermediate sum -4147

Prepared by: J. K. Josephine Julina P a g e | 10


SSN College of Engineering
Department of Information Technology
Iteration 43
Intermediate sum -4104
Iteration 44
Intermediate sum -4060
Iteration 45
Intermediate sum -4015
Iteration 46
Intermediate sum -3969
Iteration 47
Intermediate sum -3922
Iteration 48
Intermediate sum -3874
Iteration 49
Intermediate sum -3825
Iteration 50
Intermediate sum -3775
Iteration 51
Intermediate sum -3724
Iteration 52
Intermediate sum -3672
Iteration 53
Intermediate sum -3619
Iteration 54
Intermediate sum -3565
Iteration 55
Intermediate sum -3510
Iteration 56
Intermediate sum -3454
Iteration 57
Intermediate sum -3397
Iteration 58
Intermediate sum -3339
Iteration 59
Intermediate sum -3280
Iteration 60
Intermediate sum -3220
Iteration 61
Intermediate sum -3159
Iteration 62
Intermediate sum -3097
Iteration 63
Intermediate sum -3034
Iteration 64
Intermediate sum -2970
Iteration 65
Intermediate sum -2905

Prepared by: J. K. Josephine Julina P a g e | 11


SSN College of Engineering
Department of Information Technology
Iteration 66
Intermediate sum -2839
Iteration 67
Intermediate sum -2772
Iteration 68
Intermediate sum -2704
Iteration 69
Intermediate sum -2635
Iteration 70
Intermediate sum -2565
Iteration 71
Intermediate sum -2494
Iteration 72
Intermediate sum -2422
Iteration 73
Intermediate sum -2349
Iteration 74
Intermediate sum -2275
Iteration 75
Intermediate sum -2200
Iteration 76
Intermediate sum -2124
Iteration 77
Intermediate sum -2047
Iteration 78
Intermediate sum -1969
Iteration 79
Intermediate sum -1890
Iteration 80
Intermediate sum -1810
Iteration 81
Intermediate sum -1729
Iteration 82
Intermediate sum -1647
Iteration 83
Intermediate sum -1564
Iteration 84
Intermediate sum -1480
Iteration 85
Intermediate sum -1395
Iteration 86
Intermediate sum -1309
Iteration 87
Intermediate sum -1222
Iteration 88
Intermediate sum -1134

Prepared by: J. K. Josephine Julina P a g e | 12


SSN College of Engineering
Department of Information Technology
Iteration 89
Intermediate sum -1045
Iteration 90
Intermediate sum -955
Iteration 91
Intermediate sum -864
Iteration 92
Intermediate sum -772
Iteration 93
Intermediate sum -679
Iteration 94
Intermediate sum -585
Iteration 95
Intermediate sum -490
Iteration 96
Intermediate sum -394
Iteration 97
Intermediate sum -297
Iteration 98
Intermediate sum -199
Iteration 99
Intermediate sum -100
Iteration 100
Intermediate sum 0
Final sum is 0

Prepared by: J. K. Josephine Julina P a g e | 13


SSN College of Engineering
Department of Information Technology
//Multidimensional arrays
#include <iostream>
#include <string>
using namespace std;
int main()
{
int rsize,csize,i,j;
cout<<"Enter row and column size of the array: \n";
cin>>rsize>>csize;
int myIntArr[rsize][csize];
//Get Input
cout<<"Enter array values: \n";
for (i=0;i<rsize;i++)
for (j=0;j<csize;j++)
cin>>myIntArr[i][j];
//Print Array Contents
for (i=0;i<rsize;i++)
{
for (j=0;j<csize;j++)
cout<<myIntArr[i][j]<<"\t";
cout<<"\n";
}
return 0;
}

Enter row and column size of the array:


2
3
Enter array values:
1
2
3
4
5
6
1 2 3
4 5 6

Prepared by: J. K. Josephine Julina P a g e | 14


SSN College of Engineering
Department of Information Technology
//Get string input
#include <iostream>
using namespace std;
int main()
{
string txt1,txt2;
cout<<"Enter first string: ";
cin>>txt1;
cout<<"Enter second string: ";
cin>>txt2;
cout<<txt1+" "+txt2;
cout<<"\nEnter a sentence: ";
cin.ignore();
getline(cin,txt1);
cout<<"Enter another sentence: ";
getline(cin,txt2);
cout<<txt1+" "+txt2;
return 0;
}
Enter first string: Good
Enter second string: morning
Good morning
Enter a sentence: The climate is cool.
Enter another sentence: Enjoy the day.
The climate is cool. Enjoy the day.

Prepared by: J. K. Josephine Julina P a g e | 15


SSN College of Engineering
Department of Information Technology
//sizeof
#include <iostream>
using namespace std;
int main()
{
int i;
char c;
float f;
double d;
cout<<"The size of integer in bytes: "<<sizeof(i)<<endl;
cout<<"The size of character in bytes: "<<sizeof(c)<<endl;
cout<<"The size of float in bytes: "<<sizeof(f)<<endl;
cout<<"The size of double in bytes: "<<sizeof(d)<<endl;
return 0;
}
The size of integer in bytes: 4
The size of character in bytes: 1
The size of float in bytes: 4
The size of double in bytes:

//String length
#include <iostream>
using namespace std;
int main()
{
string txt1,txt2;
cout<<"Enter a string: ";
cin>>txt1;
cout<<"Enter another string: ";
cin>>txt2;
cout<<"The length of the first string is "<<txt1.length()<<". The length of the next string is
"<<txt2.size()<<".";
return 0;
}

Enter a string: Darjeeling


Enter another string: Goa
The length of the first string is 10. The length of the next string is 3.

Prepared by: J. K. Josephine Julina P a g e | 16


SSN College of Engineering
Department of Information Technology
//String Arrays
#include <iostream>
#include <string>
using namespace std;
int main()
{
int size,i,j;
cout<<"Enter size of the string array: ";
cin>>size;
string name[size];
cout<<"Enter all "<<size<<" string values one by one.\n";
for (i=0;i<size;i++)
cin>>name[i];
cout<<"Entered values are: \n";
for (j=0;j<size;j++)
cout<<name[j]<<" ";
}

Enter size of the string array: 4


Enter all 4 string values one by one.
Welcome
to
C++
Programming
Entered values are:
Welcome to C++ Programming

Prepared by: J. K. Josephine Julina P a g e | 17


SSN College of Engineering
Department of Information Technology
//Program to demonstrate different datatypes and multi-dimensional arrays

#include<iostream>
#include<string>
using namespace std;
int main()
{
cout<<"\"Datatypes in C++\"";
int i1,i2,i,j,pos;
cout<<"\nEnter two integer values: ";
cin>>i1>>i2;
cout<<"Entered integer values are "<<i1<<" and "<<i2;
double d1,d2;
cout<<"\n\nEnter two double values: ";
cin>>d1>>d2;
cout<<"Entered double values are "<<d1<<" and "<<d2;
bool isbool;
cout<<"\n\nBoolean Results: ";
isbool=i1>i2;
cout<<"\nInteger: "<<i1<<">"<<i2<<" is "<<isbool;
isbool=d1>d2;
cout<<"\nDouble: "<<d1<<">"<<d2<<" is "<<isbool;
char c;
cout<<"\n\nEnter a character: ";
cin>>c;
cout<<"The ASCII value of "<<c<<" is "<<int(c);

cout<<"\n\n\"Program to demonstrate arrays\"\n";


//String array
cout<<"\nString array";
string txt;
int limit;
cout<<"\nEnter the size of the 1D string array: ";
cin>>limit;
string s[limit];
//string s[3] = {"Programming"," is ","fun."};
//Get the input in 1D array
for(i=0;i<limit;i++)
{
cout<<"Enter contents for ["<<i<<"] = ";
cin>>s[i];
}
cout<<"\nDisplaying the string array contents as follows....\n";
for(i=0; i<limit;i++)
cout<<s[i]<<"\n";
cout<<"\nTrying to modify string array contents....";
//Modifying few initial array contents
cout<<"\nEnter the position of modification in the string array\n";
cin>>pos;
cout<<"\nEnter the new value: ";

Prepared by: J. K. Josephine Julina P a g e | 18


SSN College of Engineering
Department of Information Technology
cin>>txt;
s[pos]=txt;
//Display the modified array
cout<<"Modified array contents as follows....\n";
for(i=0; i<limit;i++)
cout<<s[i]<<"\n";
//Integer array
//int myarr[i1][i2]={{11,22,31},{14,25,36}};
int myarr[i1][i2];
cout<<"\n\nGet the integer values in 2D array\n";
for(i=0;i<i1;i++)
{
for(j=0;j<i2;j++)
{
cout<<"Enter contents for ["<<i<<"]"<<"["<<j<<"] = ";
cin>>myarr[i][j];
}
}
cout<<"\nDisplaying the Integer array contents as follows....\n";
//Display Integer array contents
for(i=0;i<i1;i++)
{
for(j=0;j<i2;j++)
{
cout<<myarr[i][j]<<"\t";
}
cout<<"\n";
}
return 0;
}

"Datatypes in C++"
Enter two integer values: 2
3
Entered integer values are 2 and 3

Enter two double values: 3.3


2.2
Entered double values are 3.3 and 2.2

Boolean Results:
Integer: 2>3 is 0
Double: 3.3>2.2 is 1

Enter a character: z
The ASCII value of z is 122

"Program to demonstrate arrays"

String array

Prepared by: J. K. Josephine Julina P a g e | 19


SSN College of Engineering
Department of Information Technology
Enter the size of the 1D string array: 5
Enter contents for [0] = Lion
Enter contents for [1] = Tiger
Enter contents for [2] = Kangaroo
Enter contents for [3] = Jackal
Enter contents for [4] = Cheetah

Displaying the string array contents as follows....


Lion
Tiger
Kangaroo
Jackal
Cheetah

Trying to modify string array contents....


Enter the position of modification in the string array
4

Enter the new value: Bear


Modified array contents as follows....
Lion
Tiger
Kangaroo
Jackal
Bear

Get the integer values in 2D array


Enter contents for [0][0] = 10
Enter contents for [0][1] = 12
Enter contents for [0][2] = 33
4Enter contents for [1][0] = 4
Enter contents for [1][1] = 44
Enter contents for [1][2] = 1

Displaying the Integer array contents as follows....


10 12 33
44 44 1

Prepared by: J. K. Josephine Julina P a g e | 20


SSN College of Engineering
Department of Information Technology
//PPT - Strings

#include <iostream>
using namespace std;
int main()
{
string hello = "Hello";
cout<<hello;
string world(hello);
cout<<"\n";
cout<<world;
string msg("Hi.. Good morning");
cout<<"\n";
cout<<msg;
string val(5,'h');
cout<<"\n";
cout<<val;
return 0;
}

Hello
Hello
Hi.. Good morning
hhhhh

#include<iostream>
using namespace std;
int main()
{
string word;
while (cin>>word)
{
if(word=="end")
break;
cout<<word<<endl;
}
return 0;
}
Joseph
Joseph
Kevin
Kevin
Kumar L
Kumar
L
end

Prepared by: J. K. Josephine Julina P a g e | 21


SSN College of Engineering
Department of Information Technology
#include<iostream>
using namespace std;
int main()
{
string line;
while (getline(cin,line))
{
if(line=="end")
break;
cout<<line<<endl;
}
return 0;
}
Jose
Jose
Moon is a natural satellite
Moon is a natural satellite
end

#include<iostream>
using namespace std;
int main()
{
string s;
string st("Enjoy well");
cout<<"The size is "<<st.size();
if(st.size()!=0)
cout<<"\nNon-empty string encountered.";
if(s.empty())
cout<<"\nEmpty string encountered.";
cout<<"\nEnter a string: ";
getline(cin,s);
//s="I am not well";
if(s>st)
cout<<"\nNew string is greater. ";
else if(s==st)
cout<<"\nSame strings ";
else
cout<<"\nNew string is lesser. ";
return 0;
}
The size is 10
Non-empty string encountered.
Empty string encountered.
Enter a string: Enjoy well
Same strings

Prepared by: J. K. Josephine Julina P a g e | 22


SSN College of Engineering
Department of Information Technology

#include<iostream>
using namespace std;
int main()
{
int i;
string s;
cout<<"\nEnter a string: ";
getline(cin,s);
//cout<<s.length();
for(i=0;i<s.size();i++)
cout<<s[i];
return 0;
}

Enter a string: Holiday


Holiday

#include<iostream>
#include<cctype>
using namespace std;
int main()
{
int i, punct=0;
string s("Ah!!!\"That's awesome buddy!\"");
cout<<"The string "<<s<<" has "<<s.size()<<" characters.";
cout<<"\nPunctions are ";
for(i=0;i<s.size();i++)
if(ispunct(s[i]))
{
cout<<s[i];
punct++;
}
cout<<"\nTotal punctuation count: "<<punct;
return 0;
}

The string Ah!!!"That's awesome buddy!" has 28 characters.


Punctions are !!!"'!"
Total punctuation count: 7

Prepared by: J. K. Josephine Julina P a g e | 23


SSN College of Engineering
Department of Information Technology
//21.10.2022 – Goto and Pointers
#include <iostream>
using namespace std;
int main()
{
int choice=0;
check1:
int num;
int *ip=&num;
cout<<"Check 1 - To check value stored in pointer";
cout<<"\nEnter a number: ";
cin>>num;
cout<<"The address of the integer pointer is "<<ip;
cout<<"\nThe value of the integer diplayed using pointer is "<<*ip;
if(choice==0)
goto check2;//forward reference
if(choice==1)
return 0;
check2:
cout<<"\nCheck 2 - To check contents of the array using pointers as loop variable";
int i,size;
cout<<"\nEnter the size of the array: ";
cin>>size;
int arr[size];
cout<<"Enter elements of the array: \n";
for(i=0;i<size;i++)
cin>>arr[i];
cout<<"The array elements are: ";
for(int *f=&arr[0];f!=&arr[size];f++)
cout<<*f<<"\t";
cout<<"\nPress 1 if you wish to check pointers and press 2 to check Array contents (Press
any other number to stop execution): ";
cin>>choice;
if(choice==1)
goto check1;//backward reference
else if(choice==2)
goto check2;
else
cout<<"\nThanks for checking the code. Have a nice day!";
return 0;
}

Check 1 - To check value stored in pointer


Enter a number: 56
The address of the integer pointer is 0x7fff1ae9ef0c
The value of the integer diplayed using pointer is 56

Prepared by: J. K. Josephine Julina P a g e | 24


SSN College of Engineering
Department of Information Technology
Check 2 - To check contents of the array using pointers as loop variable
Enter the size of the array: 5
Enter elements of the array:
1
2
3
4
5
The array elements are: 1 2 3 4 5
Press 1 if you wish to check pointers and press 2 to check Array contents (Press any
other number to stop execution): 2

Check 2 - To check contents of the array using pointers as loop variable


Enter the size of the array: 6
Enter elements of the array:
1
2
3
4
5
6
The array elements are: 1 2 3 4 5 6
Press 1 if you wish to check pointers and press 2 to check Array contents (Press any
other number to stop execution): 1
Check 1 - To check value stored in pointer
Enter a number: 5
The address of the integer pointer is 0x7fff1ae9ef0c
The value of the integer diplayed using pointer is 5

#include <iostream>
using namespace std;
int main()
{
int *val = new int;
int *ival = new int(5);
cout<<*val;
cout<<"\n"<<*ival;
delete val,ival;
return 0;
}

0
5

Prepared by: J. K. Josephine Julina P a g e | 25


SSN College of Engineering
Department of Information Technology
//Switch case statement
#include <iostream>
using namespace std;
int main()
{
int n;
cout<<"Enter integer switch argument : ";
cin>>n;
switch(n)
{
case 1:
cout<<"\nThe number is One";
break;
case 2:
cout<<"\nThe number is Two";
break;
default:
cout<<"Not a valid one.";
break;
}
char ch;
cout<<"\nEnter character switch argument : ";
cin>>ch;
switch(ch)
{
case 'a': case 'A':
cout<<"\nThe first letter of the alphabet";
break;
case 'b': case 'B':
cout<<"\nThe second letter of the alphabet";
break;
default:
cout<<"Not a valid one.";
break;
}
int cnt=0;
char vch;
cout<<"\nEnter character for counting vowels : ";
cin>>vch;
switch(vch)
{
case 'a': case 'e': case 'i': case 'o': case 'u': case 'A': case 'E': case 'I': case 'O': case 'U':
cnt++;
break;
default:
cout<<"Not a vowel";

Prepared by: J. K. Josephine Julina P a g e | 26


SSN College of Engineering
Department of Information Technology
break;
}
cout<<cnt;
return 0;
}

Enter integer switch argument : 1

The number is One


Enter character switch argument : a

The first letter of the alphabet


Enter character for counting vowels : E
1

//Program to demonstrate Functions

#include <iostream>
using namespace std;
int main()
{
int n1,n2;
//Function declaration
int findlarger(int,int);
cout<<"Enter two numbers: ";
cin>>n1>>n2;
cout<<"The larger number is: "<<findlarger(n1,n2);//n1 and n2 are Actual parameters
return 0;
}
//Function definition
int findlarger(int val1,int val2)//val1 and val2 are Formal parameters
{
if (val1>val2)
return val1;
else
return val2;
}
Enter two numbers: 44
22
The larger number is: 44

Prepared by: J. K. Josephine Julina P a g e | 27


SSN College of Engineering
Department of Information Technology
//Functions - GCD
#include <iostream>
using namespace std;
int main()
{
int n1,n2;
//function declaration
int findgcd(int,int);
cout<<"Enter two numbers: ";
cin>>n1>>n2;
cout<<"The Greatest Common Divisor number is: "<<findgcd(n1,n2);//function call
return 0;
}
//function definition
int findgcd(int val1,int val2)//val1 and val2 are formal parameters
{
while(val2)
{
int temp=val2;
val2=val1%val2;
val1=temp;
}
return val1;//this function returns an integer value
}

Enter two numbers: 55


60
The Greatest Common Divisor number is: 5

//Program to demonstrate class in C++


#include <iostream>
#include <string>
using namespace std;
class Animal
{
private:
string name[30];
public:
void display();
void display(string name)
{
cout<<name;
}
};
int main()
{

Prepared by: J. K. Josephine Julina P a g e | 28


SSN College of Engineering
Department of Information Technology
string nameoftheanimal;
cout<<"Enter an animal name: ";
cin>> nameoftheanimal;
Animal obj;
obj.display(nameoftheanimal);
}
Enter an animal name: Lion
Lion

#include <iostream>
#include <string>
using namespace std;
class Animal
{
//Access specifier
private:
//Private data member
string name[30];//Cannot be accessed from outside
public:
//Public data members
int num=5;//can be accessed
void display();//Member function
//Function definition
void display(string name)
{
cout<<"The name of the animal entered by the user in the main function is "<<name;
cout<<"\n";
cout<<"\nThe value of an integer data member is "<<num;//Displays assigned value 10 in
the object not 5 that is used in the class
}
};
int main()
{
int i=0;
char letter;
char animalarr[30];
cout<<"Enter an animal name: ";
while(cin>>letter)
{
if(letter!='x')
{
animalarr[i]=letter;
i++;
}
else
{

Prepared by: J. K. Josephine Julina P a g e | 29


SSN College of Engineering
Department of Information Technology
cout<<"\nNo. of characters entered in the text is "<<i<<"\n";
break;
}
}
Animal obj;
//num variable declared as public data member can be accessed
obj.num = 10;
//obj.name = animalarr;//error as private members of the class cannot be accessed
obj.display(animalarr);
}
Enter an animal name: Horse
x

No. of characters entered in the text is 5


The name of the animal entered by the user in the main function is Horse

The value of an integer data member is 10

//Sum and difference of two matrices using functions


#include<iostream>
using namespace std;
void add(int a[10][10], int b[10][10], int m,int n);
void sub(int a[10][10], int b[10][10], int m,int n);
int main(void){
int i,j,m,n,a[10][10],b[10][10];
cout<<"enter the size"<<endl;
cin>>m>>n;
cout<<"enter matrix a"<<endl;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
cin>>a[i][j];
}
cout<<"enter matrix b"<<endl;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
cin>>b[i][j];
}
add(a,b,m,n);
sub(a,b,m,n);
}
void add(int a[10][10],int b[10][10],int m,int n){
int c[10][10],i,j;
for(i=0;i<m;i++)
{

Prepared by: J. K. Josephine Julina P a g e | 30


SSN College of Engineering
Department of Information Technology
for(j=0;j<n;j++)
c[i][j]=a[i][j]+b[i][j];
}
cout<<"sum is"<<endl;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
cout<<c[i][j]<<" ";
cout<<endl;
}
}
void sub(int a[10][10],int b[10][10],int m,int n){
int c[10][10],i,j;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
c[i][j]=a[i][j]-b[i][j];
}
cout<<"difference is"<<endl;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
cout<<c[i][j]<<" ";
cout<<endl;
}
}
enter the size
2
2
enter matrix a
2
3
1
4
enter matrix b
1
2
4
3
sum is
35
57
difference is
11
-3 1

Prepared by: J. K. Josephine Julina P a g e | 31


SSN College of Engineering
Department of Information Technology
//Program to compute area of the rectangle using constructor in C++
#include<iostream>
using namespace std;
class Rectangle
{
int side1,side2;
public:
Rectangle(int,int);
int area()
{
return (side1*side2);
}
};
Rectangle::Rectangle(int a, int b)
{
side1 = a;
side2 = b;
}
int main()
{
int l,b;
cout<<"Enter the sides of the rectangle: ";
cin>>l>>b;
Rectangle r(l,b);
cout<<"Area = "<<r.area()<<endl;
return 0;
}
/*
Output:
Enter the sides of the rectangle: 5
6
Area = 30
*/

//Program to demonstrate increment and decrement operators


#include <iostream>
using namespace std;
int main()
{
int a(100);
cout<<"a = "<<a;
cout<<"\na++ = "<<a++;
cout<<"\na = "<<a;
cout<<"\n++a = "<<++a;
cout<<"\na = "<<a;
cout<<"\na-- = "<<a--;

Prepared by: J. K. Josephine Julina P a g e | 32


SSN College of Engineering
Department of Information Technology
cout<<"\na="<<a;
cout<<"\n--a ="<<--a;
cout<<"\na = "<<a;
return 0;
}
/*
a = 100
a++ = 100
a = 101
++a = 102
a = 102
a-- = 102
a=101
--a =100
a = 100
*/

//Simple program to study class syntax


#include <iostream>
using namespace std;
class Student
{
public:
int num;
void display()
{
cout<<"\nThe value of an integer data member is "<<num;
}
};
int main()
{
int i=0;
cout<<"Enter a number: ";
cin>>i;
Student st;
st.num = i;
st.display();
}
Enter a number: 10

The value of an integer data member is 10

Prepared by: J. K. Josephine Julina P a g e | 33


SSN College of Engineering
Department of Information Technology
//Function definition outside the class
#include <iostream>
using namespace std;
class Student
{
public:
int num;
void display(int);
};
void Student::display(int num)
{
cout<<"\nThe value of an integer data member is "<<num;
}
int main()
{
Student st;
cout<<"Enter a number: ";
cin>>st.num;
st.display(st.num);
}
Enter a number: 50

The value of an integer data member is 50


#include <iostream>
using namespace std;
class Student
{
public:
int num;
void display();
};
void Student::display()
{
cout<<"\nThe value of an integer data member is "<<num;
}
int main()
{
Student st;
cout<<"Enter a number: ";
cin>>st.num;
st.display();
}
Enter a number: 88

The value of an integer data member is 88

Prepared by: J. K. Josephine Julina P a g e | 34


SSN College of Engineering
Department of Information Technology
//Function Overloading
#include <iostream>
using namespace std;

class fnoverloading
{
public:
int a, b;
char c, d;
float e, f;
double g,h;
void getinput(int ch)
{
switch(ch)
{
case 1:
cout<<"\nEnter Integers: ";
cin>>a>>b;
break;
case 2:
cout<<"\nEnter Characters: ";
cin>>c>>d;
break;
case 3:
cout<<"\nEnter Float values: ";
cin>>e>>f;
break;
case 4:
cout<<"\nEnter double values: ";
cin>>g>>h;
break;
}
}
void display(int n1, int n2)
{
a=n1;
b=n2;
cout<<"\nIntegers are "<<n1<<" and "<<n2;
sum(1);

}
void display(char n1, char n2)
{
c=n1;
d=n2;
cout<<"\nChar values are "<<n1<<" and "<<n2;

Prepared by: J. K. Josephine Julina P a g e | 35


SSN College of Engineering
Department of Information Technology
sum(2);
}
void display(float n1, float n2)
{
e=n1;
f=n2;
cout<<"\nFloat values are "<<n1<<" and "<<n2;
sum(3);
}
void display(double n1, double n2)
{
g=n1;
h=n2;
cout<<"\nFloat values are "<<n1<<" and "<<n2;
sum(4);
}
void sum(int ch)
{
switch(ch)
{
case 1:
cout<<"\nThe integer sum is "<<a+b;
break;
case 2:
cout<<"\nThe char sum is "<<c+d;
break;
case 3:
cout<<"\nThe float sum is "<<e+f;
break;
case 4:
cout<<"\nThe double sum is "<<g+h;
break;
}
}
};
int main()
{
fnoverloading obj;
obj.getinput(1);
obj.getinput(2);
obj.getinput(3);
obj.getinput(4);
obj.display(obj.a,obj.b);
obj.display(obj.c,obj.d);
obj.display(obj.e,obj.f);
obj.display(obj.g,obj.h);

Prepared by: J. K. Josephine Julina P a g e | 36


SSN College of Engineering
Department of Information Technology
return 0;
}

Enter Integers: 3
6

Enter Characters: a
z

Enter Float values: 3.0


6.8

Enter double values: 3.777777777777


8.9999999999999999999

Integers are 3 and 6


The integer sum is 9
Char values are a and z
The char sum is 219
Float values are 3 and 6.8
The float sum is 9.8
Float values are 3.77778 and 9
The double sum is 12.7778

//Call by value and Call by reference


#include<iostream>
using namespace std;
int main()
{
//Variable declarations
int a, b, c;
//Get 2 numbers as input from the user
cout<<"Enter first number: ";
cin>>a;
cout<<"Enter second number: ";
cin>>b;
//Function declarations
void sum(int,int);
int findlarger(int,int);
void swapbyvalue(int, int);
void swapbyreference(int*, int*);
//Function call
sum(a,b);
c=findlarger(a,b);
if(a==c)
cout<<"\nThe smaller number is "<<b;
else if(b==c)

Prepared by: J. K. Josephine Julina P a g e | 37


SSN College of Engineering
Department of Information Technology
cout<<"\nThe smaller number is "<<a;
swapbyvalue(a,b);
cout<<"\nNot swapped in main routine: "<<a<<" and "<<b;
swapbyreference(&a,&b);
cout<<"\nSwapped in main routine: "<<a<<" and "<<b;
return 0;
}
//Function definitions
//Function without a return
void sum(int x, int y)
{
cout<<"The sum of "<<x<<" and "<<y<<" is "<<x+y;
}
//Function with a integer return type
int findlarger(int x, int y)
{
int c;
c=(x>y)?x:y;
cout<<"\nThe larger number is "<<c;
return c;
}
//Call by value
void swapbyvalue(int a, int b)
{
int temp;
temp = a;
a = b;
b = temp;
cout<<"\nThe swapped values inside the function definition are "<<a<<" and "<<b;
}
//Call by reference
void swapbyreference(int* a, int* b)
{
int temp;
temp = *a;
*a = *b;
*b = temp;
}
Enter first number: 10
Enter second number: 20
The sum of 10 and 20 is 30
The larger number is 20
The smaller number is 10
The swapped values inside the function definition are 20 and 10
Not swapped in main routine: 10 and 20
Swapped in main routine: 20 and 10

Prepared by: J. K. Josephine Julina P a g e | 38

You might also like