Practical C
Practical C
--------------------------------------------------------------------------------------
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];
int main()
{ int n;
cout<<"Input a number\n";
cin>>n;
factl(n);
return 0;
}