CPP Lab Programs
CPP Lab Programs
output2:
Enter a number: 8
Number is Positive
output3:
Enter a number: 0
Number is zero
6.Find the sum of squares of first N natural numbers without using any formula.
#include<iostream>
using namespace std;
return 0;
}
output 1:
enter a number: 1221
The number is palindrome
output 2:
enter a number: 1234
The number is not palindrome
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
#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:
*
* *
* * *
* * * *
* * * * *
#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
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;
}