0% found this document useful (0 votes)
10 views

Practical C

The document contains source code for 7 C++ programs with the following functions: 1. Check if a number is positive, negative, or zero 2. Calculate the sum of squares of the first N natural numbers 3. Find the largest of three input numbers 4. Display the word for a number input using switch case 5. Print the first N terms of the Fibonacci series 6. Count even and odd numbers in an array 7. Calculate the factorial of a number using a user-defined function

Uploaded by

Sinan K
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)
10 views

Practical C

The document contains source code for 7 C++ programs with the following functions: 1. Check if a number is positive, negative, or zero 2. Calculate the sum of squares of the first N natural numbers 3. Find the largest of three input numbers 4. Display the word for a number input using switch case 5. Print the first N terms of the Fibonacci series 6. Count even and odd numbers in an array 7. Calculate the factorial of a number using a user-defined function

Uploaded by

Sinan K
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/ 7

NUMBER CHECK

--------------------------------------------------------------------------------------

Aim: Write a C++ program to input a number and check whether it is


positive, negative or zero

Source code:

#include<iostream>
using namespace std;
int main()
{
int n;
cout<<"Enter a number";
cin>>n;
if(n==0)
cout<<"The number is Zero";
else
{
if(n>0)
cout<<"The number is Positive";
else
cout<<"The number is Negative";
}
return 0;
}
Sum of squares of first N natural numbers
================================================================
Aim: write a C++ program to find the sum of the squares of the first N
natural numbers without using any formula.

Source code:

#include<iostream>
using namespace std;
int main()
{
int n,s=0,i;
cout<<"Enter a digit";
cin>>n;
for(i=0;i<=n;i++)
{
s=s+i*i;
}
cout<<"Sum of the squares ="<<s;
return 0;
}
Input three numbers and find the largest.
Aim: Write a C++ program to input three numbers and find the largest
Source code:
#include <iostream>
using namespace std;
int main()
{ int a,b,c;
cout<<"Input three numbers \n";
cin>>a>>b>>c;
if (a>b )
{
if( a>c)
{
cout<<a<<" is largest";
}
else
{
cout<<c<<" is largest";
}
}
else
{
if(b>c)
{
cout<<b<<" is largest";
}
else
{
cout<<c<<" is largest";
}
} return 0;
}
SWITCH Statement
============================================
Aim: Write a C++ program to Input a digit and display the corresponding word
using switch
Source Code:
#include <iostream>
using namespace std;
int main()
{
int d;
cout<<"Input a digit and display word using switch\n";
cin>>d;
switch (d)
{
case 0: cout<<"Zero";
break;
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 digit";
}
return 0
FIBONACCI SERIES
=============================================================================
AIM: Write a C++ program to display the first N terms of Fibonacci
series.
Source code:
#include <iostream>
Using namespace std;
int main()
{
int a=0,b=1,c ,n, count=2;
cout<<”Enter value for n”;
cin>>n;
cout<<<a<<”\t”<<b;
while(count<=n)
{
c=a+b;
cout<<”\t”<<c;
a=b;
b=c;
count=count+1;
}
return 0;

}
EVEN COUNT , ODD COUNT
=================================================================
Aim: Write a C++ program to find the number of odd elements and
number of even elements in an array.
Source code:

#include<iostream>
using namespace std;
int main()
{
int arr[100];
int i,size,odd=0,even=0;
cout<<"Enter size of the array\n";
cin>>size;
cout<<"\nEnter elements of the array";
for(i=0; i<size; i++)
cin>>arr[i];

for(i=0; i<size; i++)


{
if(arr[i]%2= =0)
{
even++;
}
else
{
odd++;
}
}
cout<<"\nTotal even numbers of an array :"<<even;
cout<<"\nTotal odd numbers of an array : "<<odd;
return 0;
}
FACTORIAL
=================================================================================
AIM: Write a C++ program to find the factorial of a number with the
help of a user-defined function.
Source code:
#include <iostream>
using namespace std;
void fact(int n)
{
long f=1;
int i;
for(i=1;i<=n;i++)
{
f= f * i;
}
}
cout<<" factorial of "<<n<<" = "<<f;
}

int main()
{ int n;
cout<<"Input a number\n";
cin>>n;
factl(n);
return 0;
}

You might also like