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

Program To Add PDF

The document contains C++ code snippets for various mathematical and numerical algorithms including: 1) A program to add and multiply the digits of a number. 2) Code to calculate the reverse of a number. 3) Programs to calculate the sum of first n terms of harmonic series and alternating series. 4) Functions to check if a number is prime, find factors of a number, swap two numbers using a macro, and print a pyramid pattern. 5) Iterative and recursive implementations to compute Fibonacci numbers, factorials, and greatest common divisor (GCD) of two numbers.

Uploaded by

Chandan Shukla
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)
44 views

Program To Add PDF

The document contains C++ code snippets for various mathematical and numerical algorithms including: 1) A program to add and multiply the digits of a number. 2) Code to calculate the reverse of a number. 3) Programs to calculate the sum of first n terms of harmonic series and alternating series. 4) Functions to check if a number is prime, find factors of a number, swap two numbers using a macro, and print a pyramid pattern. 5) Iterative and recursive implementations to compute Fibonacci numbers, factorials, and greatest common divisor (GCD) of two numbers.

Uploaded by

Chandan Shukla
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/ 18

 PROGRAM TO ADD & MULTIPLY DIGITS OF A

NUMBER
#include<iostream>

using namespace std;

int main()

int i,n,sum=0,pro=1;

cout<<"ENTER A DIGIT : "<<endl;

cin>>n;

while(n>0)

i=n%10;

sum=sum+i;

pro=pro*i;

n=n/10;

cout<<"SUM OF DIGITS = "<<sum<<endl<<"PRODUCT OF DIGITS =


"<<pro;

}
 TO PRINT REVERSE OF A NUMBER
#include<iostream>

using namespace std;

int main()

int i,n,rev=0;

cout<<"ENTER A DIGIT : "<<endl;

cin>>n;

cout<<endl<<"ENTERED NUMBER IS : "<<n<<endl;

while(n>0)
{

i=n%10;

rev=rev*10+i;

n=n/10;

cout<<"REVERSE = "<<rev;

 TO COMPUTE SUM OF FIRST n TERMS OF


FOLLOWING SERIES :

S= 1+(1/2)+(1/3)+(1/4)………………(1/n)
#include<iostream>

using namespace std;


int main()

float i,n,sum=0;

cout<<"ENTER THE NUMBER OF TERMS TO ADD :"<<endl;

cin>>n;

for(i=1;i<=n;i++)

sum=sum+(1/i);

cout<<"SUM AFTER "<<n<<" terms IS :"<<endl<<sum;

 TO COMPUTE SUM OF FIRST n TERMS OF


FOLLOWING SERIES :
S= 1-2+3-4+5-6…………………..

#include<iostream>
using namespace std;

int main()

int i,n,s1=0,s2=0,sum;

cout<<"ENTER THE NUMBER OF TERMS :"<<endl;

cin>>n;

for(i=1;i<=n;i++)

if(i%2==0)

s1=s1+i;

else

s2=s2+i;

sum=s2-s1;

cout<<"SUM AFTER "<<n<<" terms is :"<<endl<<sum;

}
 WRITE A FUNCTION TO FIND WHETHER A GIVEN NUMBER IS
PRIME OR NOT . USE IT TO GENERATE PRIME NUMBERS
BETWEEN 1 & 100.
#include<iostream>

using namespace std;

int prime(int a)

int i,count=0;

for(i=1;i<=a;i++)

if(a%i==0)

count+=1;

}
if(count==2)

cout<<a<<" is prime"<<endl;

else

cout<<a<<" is not prime";

int main()

int n;

cout<<"ENTER A NUMBER TO CHECK WHETHER IT'S PRIME OR NOT :"<<endl;

cin>>n;

prime(n);

}
 WAP TO COMPUTE FACTORS OF A GIVEN
NUMBER.

#include<iostream>

using namespace std;

int main()

int i,n;

cout<<"ENTER THE NUMBER OF TERMS :"<<endl;

cin>>n;

for(i=1;i<=n;i++)

if(n%i==0)

cout<<i<<endl;

}
 TO SWAP 2 NUMBERS USING MACRO.

#include<iostream>

#define swap(a,b) b=a+b-(a=b)

using namespace std;

int main()

int i,n;

cout<<"ENTER THE 2 NUMBERS TO SWAP :"<<endl;

cin>>i>>n;

cout<<"ENTERED NUMBERS ARE :"<<i<<endl<<n<<endl<<endl;

swap(i,n);

cout<<"NUMBERS ARE SWAPPING ARE :"<<i<<endl<<n;

}
 WAP TO PRINT PYRAMID.
#include<iostream>

using namespace std;

int main()

int i,n,j,k,rows;

cout<<"ENTER THE NUMBER OF ROWS :"<<endl;

cin>>n;

for(i=1;i<=n;i++)

for(k=i;k<rows;k++)

cout<<" ";

for(j=1;j<=(2*i-1);j++)
{

cout<<"*";

cout<<endl;

 WAP TO DISPLAY FIBONACCI USING ITERATION


#include<iostream>

using namespace std;

int main()

int f=0 , s=1, next ,c,n;

cout<<"ENTER THE NUMBER OF TERMS :"<<endl;

cin>>n;
cout<<"FIRST "<<n<<"TERMS ARE :"<<endl;

for(c=1;c<=n;c++)

if(c<=1)

next = c;

else

next=f+s;

f=s;

s=next;

cout<<next<<endl;

return 0;

}
 WAP TO DISPLAY FIBONACCI USING RECURSION

#include<iostream>

using namespace std;

int fib(int n)

if(n==0)

return 0;

else if(n==1)

return 1;

else

return fib(n-1)+fib(n-2);

}
int main()

int i=0 ,c,n;

cout<<"ENTER THE NUMBER OF TERMS :"<<endl;

cin>>n;

cout<<"FIRST "<<n<<"TERMS ARE :"<<endl;

for(c=1;c<=n;c++)

cout<<fib(i)<<endl;

i++;

return 0;

}
 WAP TO CALCULATE FACTORIAL USING
ITERATION
#include<iostream>

using namespace std;

int main()

int s=1,c,n;

cout<<"ENTER THE NUMBER TO FIND FACTORIAL :"<<endl;

cin>>n;

int t=n;

if(n<0)

cout<<"NEGATIVE INTEGERS ARE NOT ALLOWED "<<endl;

else

while(n>0)

s=s*n;

n--;

cout<<t<<"! is : "<<s;

return 0;

}
 WAP TO CALCULATE FACTORIAL USING
RECURSION
#include<iostream>

using namespace std;

int fact(int n)

return n*fact(n-1);;

int main()

int s,c,n;

cout<<"ENTER THE NUMBER TO FIND FACTORIAL :"<<endl;

cin>>n;

s=fact(n);
cout<<n<<"! is : "<<s;

return 0;

 WAP TO CALULATE GCD BY ITERATION

#include<iostream>

using namespace std;

int main()

int a,c,b;

cout<<"ENTER THE NUMBER TO FIND GCD OF 2 NUMBERS :"<<endl;

cin>>a>>b;

cout<<endl;

if(a>b)

c=a;
else

c=b;

for(int i=0;i<=c;i++)

if(a%i==0 && b%i==0)

cout<<"GCD is : "<<i<<endl;

return 0;

You might also like