0% found this document useful (0 votes)
0 views8 pages

CPP Lab Programs

The document contains a series of C++ lab programs that demonstrate basic programming concepts such as input validation, control structures, and functions. Each program includes code snippets, expected outputs, and descriptions of tasks like checking number properties, finding the largest number, and calculating sums. The document is prepared by Rakhi S HSST CS from GHSS Vellur Kannur.

Uploaded by

eatmybullets505
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)
0 views8 pages

CPP Lab Programs

The document contains a series of C++ lab programs that demonstrate basic programming concepts such as input validation, control structures, and functions. Each program includes code snippets, expected outputs, and descriptions of tasks like checking number properties, finding the largest number, and calculating sums. The document is prepared by Rakhi S HSST CS from GHSS Vellur Kannur.

Uploaded by

eatmybullets505
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/ 8

C++ Lab programs

1.Input a number and check whether it is positive , negative or zero.


#include<iostream>
using namespace std;
int main( )
{
int num;
cout<<"Enter a number:";
cin>>num;
if(num>0)
{
cout<<"\n Number is Positive";
}
else if(num<0)
{
cout<<"\n Number is Negative";
}
else
{
cout<<"\n Number is Zero";
}
return 0;
}
output1:
Enter a number: -7
Number is Negative

output2:
Enter a number: 8
Number is Positive

output3:
Enter a number: 0
Number is zero

2.Input three number and find the largest number.


#include<iostream>
using namespace std;
int main( )
{
int num1,num2,num3;
cout<<"Enter three numbers:";
cin>>num1>>num2>>num3;
if(num1>num2 && num1>num3)
{
cout<<num1<<" is the largest number";
}
else if(num2>num3)
{
cout<<num2<<" is the largest number";
}
else
{
cout<<num3<<" is the largest number";
}

Prepared by Rakhi S HSST CS , GHSS Vellur Kannur


return 0;
}
output:
Enter three numbers:45 78 90
90 is the largest number

3.Input a digit and display the corresponding word using switch.


#include<iostream>
using namespace std;
int main( )
{
int digit;
cout<<"Enter a digit (0 to 9) : ";
cin>>digit;
switch(digit)
{
case 1:
cout<<" One";
break;
case 2:
cout<<" Two";
break;
case 3:
cout<<" Three";
break;
case 4:
cout<<" Four";
break;
case 5:
cout<<" Five";
break;
case 6:
cout<<" Six";
break;
case 7:
cout<<" Seven";
break;
case 8:
cout<<" Eight";
break;
case 9:
cout<<" Nine";
break;
default:
cout<<" Not a single digit";
}
return 0;
}
output1:
Enter a digit (0 to 9) : 6
Six
output 2:
Enter a digit (0 to 9) : 12
Not a single digit

Prepared by Rakhi S HSST CS , GHSS Vellur Kannur


4.Find the sum of digits of an integer number.
#include<iostream>
using namespace std;
int main( )
{
int num,digit,sum=0;
cout<<"enter a number: ";
cin>>num;
while(num!=0)
{
digit=num % 10;
sum=sum + digit;
num=num / 10;
}
cout<<" The sum of digits is:"<<sum;
return 0;
}
output:
enter a number: 4567
The sum of digits is:22

5.Display the multiplication table of a number having 12 rows.


#include<iostream>
using namespace std;
int main( )
{
int num;
cout<<"Enter a number: ";
cin>>num;
for(int i=1;i<=12;i++)
{
cout<<num<<" * "<<i<<" = "<<num*i<<"\n";
}
return 0;
}
output:
Enter a number: 9
9*1=9
9 * 2 = 18
9 * 3 = 27
9 * 4 = 36
9 * 5 = 45
9 * 6 = 54
9 * 7 = 63
9 * 8 = 72
9 * 9 = 81
9 * 10 = 90
9 * 11 = 99
9 * 12 = 108

6.Find the sum of squares of first N natural numbers without using any formula.
#include<iostream>
using namespace std;

Prepared by Rakhi S HSST CS , GHSS Vellur Kannur


int main( )
{
int sum=0,N;
cout<<"Enter the limit ";
cin>>N;
for(int i=1;i<=N;i++)
{
sum = sum + (i*i);
}
cout<<"The sum of squares of first "<<N<<" natural numbers are:"<<sum;
return 0;
}
output:
Enter the limit 5
The sum of squares of first 5 natural numbers are:55

7.Input a number and check whether it is palindrome or not.


#include<iostream>
using namespace std;
int main( )
{
int num,digit,rev=0,copy;
cout<<"enter a number: ";
cin>>num;
copy=num;
while(num!=0)
{
digit=num % 10;
rev=(rev*10)+digit;
num=num / 10;
}
if(rev==copy)
{
cout<<" The number is palindrome";
}
else
{
cout<<" The number is not palindrome";
}

return 0;
}
output 1:
enter a number: 1221
The number is palindrome

output 2:
enter a number: 1234
The number is not palindrome

8.Find the factorial of a number with the help of a user-defined function.


#include<iostream>
using namespace std;
int fact(int);
int main( )

Prepared by Rakhi S HSST CS , GHSS Vellur Kannur


{
int num,f;
cout<<"Enter a number:";
cin>>num;
f=fact(num);
cout<<"The factorial of "<<num<<"is: "<<f;
return 0;
}

int fact(int a)
{
int f=1;
for(int i=1;i<=a;i++)
{
f=f * i;
}
return f;
}
output:
Enter a number:5
The factorial of 5i s:120

9.Write a C++ program to print the following pattern.


*
* *
* * *
* * * *
* * * * *

#include<iostream>
using namespace std;
int fact(int);
int main( )
{
for(int i=1;i<=5;i++)
{
for(int j=1;j<=i;j++)
{
cout<<"*\t";
}
cout<<"\n\n";
}
return 0;
}
output:
*
* *
* * *
* * * *
* * * * *

Prepared by Rakhi S HSST CS , GHSS Vellur Kannur


10.Write a C++ program to print the following pattern.
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5

#include<iostream>
using namespace std;
int fact(int);
int main( )
{
for(int i=1;i<=5;i++)
{
for(int j=1;j<=i;j++)
{
cout<<j<<"\t";
}
cout<<"\n\n";
}
return 0;
}
output:
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5

11.Find the length of a string without using strlen( ) function.


#include<iostream>
#include<cstdio>
using namespace std;
int main( )
{
char chr[20];
int count=0;
cout<<"Enter a string: ";
gets(chr);
for(int i=0;chr[i]!='\0';i++)
{
count++;
}
cout<<"The length of the string is: "<<count;
return 0;
}
output:
Enter a string: computer application
The length of the string is: 20

Prepared by Rakhi S HSST CS , GHSS Vellur Kannur


12. Input the height of 10 students and find the average height.
#include<iostream>
using namespace std;
int main( )
{
float height[10],s=0;
cout<<"Enter the height of 10 student: ";
for(int i=0;i<=9;i++)
{
cin>>height[i];
s=s+height[i];
}
cout<<"Average height is: "<<s/10;
return 0;
}
output:
Enter the height of 10 student: 60.00
67.89
50.78
65.34
59.90
78.00
45.12
80.00
77.88
88.99
Average height is: 67.39

13. Create an array of N numbers and count the number of even numbers and odd numbers
in the array.
#include<iostream>
using namespace std;
int main( )
{
int N,num[100],even=0,odd=0;
cout<<"Enter the array limit: ";
cin>>N;
cout<<"Enter the array elements: ";
for(int i=0;i<N;i++)
{
cin>>num[i];
if(num[i] % 2 ==0)
{
even++;
}
else
{
odd++;
}
}
cout<<"Total number of even number are: "<<even;
cout<<"\nTotal number of odd numbers are: "<<odd;
return 0;
}

Prepared by Rakhi S HSST CS , GHSS Vellur Kannur


output:
Enter the array limit: 5
Enter the array elements: 67
88
90
45
23
Total number of even number are: 2
Total number of odd numbers are: 3

Prepared by Rakhi S HSST CS , GHSS Vellur Kannur

You might also like