0% found this document useful (0 votes)
353 views21 pages

Kerala HSS CS Lab C++ Sample Programs

This document contains C++ source code and output for several programs that demonstrate basic C++ concepts like arrays, functions, pointers, structures, and switch-case statements. The programs include examples that read data into arrays and print selected elements, calculate factorials recursively, perform linear search on an array, find the average of numbers using pointers, calculate string length without built-in functions, define and use a structure to calculate employee salary, find the sum of digits in a number, calculate the sum of squares of numbers, swap two variables using a function, and use a switch-case statement to output a group name based on a given code.
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)
353 views21 pages

Kerala HSS CS Lab C++ Sample Programs

This document contains C++ source code and output for several programs that demonstrate basic C++ concepts like arrays, functions, pointers, structures, and switch-case statements. The programs include examples that read data into arrays and print selected elements, calculate factorials recursively, perform linear search on an array, find the average of numbers using pointers, calculate string length without built-in functions, define and use a structure to calculate employee salary, find the sum of digits in a number, calculate the sum of squares of numbers, swap two variables using a function, and use a switch-case statement to output a group name based on a given code.
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/ 21

Kerala HSS CS Lab

C++ Programs Source Code and


Output
By Farzeen | My Website
More Downloads | Project Website

ARRAY1.CPP source
01:
02:
03:
04:
05:
06:
07:
08:
09:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:

/*
* Q: Read N numbers into an array
* and print those which are larger than
* the average
*/
#include <iostream>
using namespace std;
int main() {
//Defining variables
int array[100]; //to store umbers entered by user
int N;
//how many items will be entered
int avg;
//to store average of numbers in array
cout<<"How many numbers would you like: ";
cin>>N;
cout<<"Enter the numbers:"<<endl;
//Reading into array[0], array[1],
//array[2]....., array[N-1]
for(int i=0; i<N; i++) {
cin>>array[i];
}
avg=0;
//Average = Sum of all numbers / Total number of items
//Step 1: Sum of all numbers
//avg = array[0] + .... + array[N-1]
for(int i=0; i<N; i++) {
avg+=array[i];
}
//Step 2: Divide by total number of items
avg/=N;
cout<<endl<<"The average is: "<<avg<<endl;
cout<<endl;
cout<<"The numbers greater than average are:"<<endl;
//if array[0]>avg, print array[0] ... array[N-1]>avg, print array[N-1]
for(int i=0; i<N; i++) {
if(array[i]>avg) cout<<array[i]<<endl;
}
cout<<endl;
//Tell the operating system that everything is OK (exit code 0)
//More info in higher classes
return 0;
}

ARRAY1.CPP output
01:
02:
03:
04:
05:
06:
07:
08:
09:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:

How many numbers would you like: 10


Enter the numbers:
1
2
3
4
5
6
7
8
9
10
The average is: 5
The numbers greater than average are:
6
7
8
9
10

FACTORIAL.CPP source
01:
02:
03:
04:
05:
06:
07:
08:
09:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:

/*
* Write a program to find the factorial
* of a number using a user defined function
* and recursion
*/
#include <iostream>
using namespace std;
/* this is the recursive factorial function
* which repeatedly calls itself
* until n is not greater than 1
*/
int factorial(int n) {
if(n>1) return n*factorial(n-1);
else return n;
}
int main() {
int num;
cout<<"Enter the number to find factorial:";
cin>>num;
int fact=factorial(num);
cout<<num<<"! = "<<fact<<endl;
//Tell OS everything's OK
return 0;
}

FACTORIAL.CPP output
1: Enter the number to find factorial:5
2: 5! = 120
3:

LINEAR-SEARCH1.CPP source
01:
02:
03:
04:
05:
06:
07:
08:
09:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:

/*
* Program to read the admission numbers of N
* students in a class and search given admission
* no. from the list using linear search
*/
#include<iostream>
using namespace std;
int main() {
int adm_nos[100];
int N, to_search;
cout<<"Enter the number of students:";
cin>>N;
cout<<"Enter admission numbers:"<<endl;
//Read n numbers into array
for(int i=0;i<N;i++) {
cout<<"["<<i<<"]"<<":"; //Display [0]:, [1]:, ..[n-]:
cin>>adm_nos[i];
}
cout<<endl;
cout<<"Enter the admission no. to search:";
cin>>to_search;
bool found=false; //to test whether an item is found
for(int i=0;i<N;i++) {
if(adm_nos[i]==to_search) {
cout<<"Found "<<to_search<<" at index "<<i<<endl;
found=true; //set to true, so that error message is not shown
break;
}
}
if(!found) //show the error message if not found
cout<<to_search<<" was not found"<<endl;
return 0;
}

LINEAR-SEARCH1.CPP output
01:
02:
03:
04:
05:
06:
07:
08:
09:
10:
11:
12:
13:
14:
15:
16:

Enter the number of students:10


Enter admission numbers:
[0]:101
[1]:102
[2]:103
[3]:104
[4]:105
[5]:106
[6]:107
[7]:108
[8]:109
[9]:110
Enter the admission no. to search:108
Found 108 at index 7

POINTER1.CPP source
01:
02:
03:
04:
05:
06:
07:
08:
09:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:

/*
* Program to create two pointers initialised with
* two numbers and find their average.
*/
#include <iostream>
using namespace std;
int main() {
int* m = new int(24);
int* n = new int(32);
int* avg = new int(0);
cout<<"value of m:"<<*m<<endl;
cout<<"value of n:"<<*n<<endl;
*avg = (*m+*n)/2;
cout<<"average:"<<*avg<<endl;
return 0;
}

POINTER1.CPP output
1: value of m:24
2: value of n:32
3: average:28
4:

STRING-LENGTH.CPP source
01:
02:
03:
04:
05:
06:
07:
08:
09:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:

/*
* Program to find string length without
* using strlen function
*/
#include <iostream>
using namespace std;
int main() {
char str[100];
int length = 0;
cout<<"Enter the string: ";
cin>>str;
// Example string="Hello" = {'H','e','l','l','o','\0'}
// length is initially zero
// str[0] = 'H' != '\0'; so length+=1 and continue loop
// ...
// str[5] = '\0' == '\0' stop looping
// print 5
while(str[length]!='\0') {
length++;
}
cout<<"The length of given string: "<<length<<endl;
return 0;
}

STRING-LENGTH.CPP output
1: Enter the string: Hello
2: The length of given string: 5
3:

STRUCT1.CPP source
01:
02:
03:
04:
05:
06:
07:
08:
09:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:

/*
* Program to find the net salary of an employee
* by defining a struct with the details:
* employee code,name,basic pay,DA,HRA,PF.
*/
#include <iostream>
using namespace std;
struct employee {
int emp_code;
char name[25];
int basic_pay;
int da;
int hra;
int pf;
int net_salary;
};
int main() {
employee emp;
cout<<"Enter the details of the employee:"<<endl;
cout<<"\tEmployee code:";
cin>>emp.emp_code;
cout<<"\tName:";
cin>>emp.name;
cout<<"\tBasic Pay:";
cin>>emp.basic_pay;
cout<<"\tDA:";
cin>>emp.da;
cout<<"\tHRA:";
cin>>emp.hra;
cout<<"\tPF:";
cin>>emp.pf;
emp.net_salary = (emp.basic_pay + emp.da + emp.hra) - emp.pf;
cout<<"Net salary:"<<emp.net_salary<<endl;
return 0;
}

STRUCT1.CPP output
1: Enter the details of the employee:
2:
Employee code:123
3:
Name:Hercules
4:
Basic Pay:10000
5:
DA:1000
6:
HRA:1000
7:
PF:600
8: Net salary:11400
9:

SUM-OF-DIGITS.CPP source
01:
02:
03:
04:
05:
06:
07:
08:
09:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:

/* Program to input a number and find the sum of its digits */


#include<iostream>
using namespace std;
int main() {
int number, sum_of_digits=0;
cout<<"Enter a number:";
cin>>number;
// The content of the loop will be
// executed as long as number > 0
while(number>0) {
//add the 1's digit of the number to sum_of_digits
//trick: division by 10 and take reminder gives 1's digit
sum_of_digits+=number%10;
//divide number by 10 (ignore decimals. ie integer division)
//so the 10's place will become 1's place
//100's place -> 10's place and so on.
number/=10;
}
cout<<"Sum of digits:"<<sum_of_digits<<endl;
return 0;
}

SUM-OF-DIGITS.CPP output
1: Enter a number:1234
2: Sum of digits:10
3:

SUM-OF-SQUARES.CPP source
01:
02:
03:
04:
05:
06:
07:
08:
09:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:

/*
* Program to display the sum of squares of N natural numbers
* without using equations
*/
#include <iostream>
using namespace std;
int main() {
int n, sum_of_sqrs = 0;
cout<<"Enter the number:";
cin>>n;
//the following code will be executed for all values of i
//from 1 to n
//ie, sum_of_sqrs+=1*1; sum_of_sqrs+=2*2; ... sum_of_sqrs+=n*n;
for(int i=1;i<=n;i++) {
sum_of_sqrs+=i*i;
}
cout<<"Sum of squares of "<<n<<" natural numbers:"<<sum_of_sqrs<<endl;
return 0;
}

SUM-OF-SQUARES.CPP output
1: Enter the number:10
2: Sum of squares of 10 natural numbers:385
3:

SWAP.CPP source
01:
02:
03:
04:
05:
06:
07:
08:
09:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:

/*
* Write a program to swap two variables with the help
* of a user defined function
*/
#include <iostream>
using namespace std;
/*
* swap is a function which accepts two variables
* passed by reference
*/
void swap(int& var1, int& var2) {
int temp = var1;
var1 = var2;
var2 = temp;
}
int main() {
//define two variables
int var1,var2;
//read values from user
cout<<"Enter the values of variables:"<<endl;
cout<<"var1=";
cin>>var1;
cout<<"var2=";
cin>>var2;
//call the swap function
swap(var1,var2);
//output the new values
cout<<"New values:"<<endl;
cout<<"var1="<<var1<<endl;
cout<<"var2="<<var2<<endl;
//Tell OS that everything is ok
return 0;
}

SWAP.CPP output
1:
2:
3:
4:
5:
6:
7:

Enter the values of variables:


var1=10
var2=100
New values:
var1=100
var2=10

SWITCH-CASE1.CPP source
01:
02:
03:
04:
05:
06:
07:
08:
09:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:

/*
* Program to input a group code and
* output corresponding group name based
* on the following:
* |-----------------------------------|
* | Code no. | Subject
|
* |----------|------------------------|
* | 5
| computer science
|
* | 33
| computer application
|
* | 39
| science
|
* | other
| invalid option
|
* |-----------------------------------|
*/
#include<iostream>
using namespace std;
int main() {
int group_code;
cout<<"Enter the group code:";
cin>>group_code;
cout<<"Group name:";
switch(group_code) {
case 5:
cout<<"Computer Science";
break;
case 33:
cout<<"Computer Application";
break;
case 39:
cout<<"Science";
break;
default:
cout<<"Invalid Option";
}
cout<<endl;
return 0;
}

SWITCH-CASE1.CPP output
1: Enter the group code:33
2: Group name:Computer Application
3:

You might also like