0% found this document useful (0 votes)
13 views12 pages

C++ Programs for Basic Math Functions

Free arrays

Uploaded by

e1hsannawaz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views12 pages

C++ Programs for Basic Math Functions

Free arrays

Uploaded by

e1hsannawaz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Name: Muhammad Ahsan Nawaz

Submitted to: Sir Mohib Ullah Khan

Subject: Programming Fundamentals

Topic: Array & Functions

Department: ICT
Program of all even number from 1 to n

#include<iostream>
using namespace std;
void even()
{
int a;
int num1=2, sum=0;
cout<<"Enter Last Number"<<endl;
cin>>a;
while(num1<=a)
{
sum=sum+num1;
num1+=2;
}
cout<<"The sum of all even numbers up to is: "<<sum<<endl;
}
int main(){
even();
}

Program to calculate sum of all Natural no from 1 to n

#include<iostream>
using namespace std;
void na()
{
int a;
int num1=1, sum=0;
cout<<"Enter Last Number"<<endl;
cin>>a;
while(num1<=a)
{
sum=sum+num1;
num1++;
}
cout<<"The sum of all even numbers up to is: "<<sum<<endl;
}
int main(){
na();
}

Program to find natural no from 1 to n


#include<iostream>
using namespace std;
int in()
{
int a,num1=1;
cout<<"Enter Last Number"<<endl;
cin>>a;
while(num1<=a)
{
cout<<num1<<endl;
num1++;
}

}
int main(){
in();
}

Program to find Factorial

#include<iostream>
using namespace std;
int in()
{
int a;
int num1=1,factorial=1;
cout<<"Enter Last Number"<<endl;
cin>>a;
while(num1<=a)
{
factorial=factorial*num1;
num1++;
}
cout<<"Factorial is "<<factorial<<endl;
return 0;
}
int main(){
in();
}

Program to find Prime Factor

#include <iostream>
using namespace std;
int in() {
int num;
cout<<"Enter a positive integer: ";
cin>>num;
int temp=num;
cout<<"Prime factors of "<<num<<" are: ";
while (temp%2==0){
cout<<2<<" ";
temp=temp/2;
}
for(int i=3;i*i<=temp;i=i+2) {
while(temp%i==0) {
cout<<i<<" ";
temp=temp /i;
}
}
if(temp>2) {
cout<<temp<<" ";
}
cout<<endl;
return 0;
}
int main(){
in();
}

Program to find LCM


#include <iostream>
using namespace std;
int in() {
int n1,n2, max, lcm;
cout<<"Enter two no"<<endl;
cin>>n1>>n2;
if (n1>n2) {
max=n1;
} else{
max=n2;
}
lcm = max;
while (1) {
if(lcm%n1==0 && lcm%n2==0) {
break;
}
lcm++;
}
cout<<lcm<<endl;
return 0;
}
int main(){
in();
}

Program to print alphabet a to z


#include<iostream>
#include<conio.h>
using namespace std;
void in() {
char char1='a';
while(char1<='z')
{
cout<<char1<<endl;
char1++;
}
}
int main(){
in();
}

Program to print all odd number from 1 to 100


#include<iostream>
using namespace std;
int in()
{
int a;
int num1=1, sum=0;
cout<<"Enter Last Number"<<endl;
cin>>a;
while(num1<=a)
{
sum=sum+num1;
num1+=2;
}
cout<<"The sum of all even numbers up to "<<sum<<endl;
return 0;
}
int main(){
in();
}

Program to print even no from 1 to 100


#include<iostream>
using namespace std;
int in()
{
int num;
num=0;
while(num<=100)
{
cout<<num<<endl;
num=num+2;
}
}
int main(){
in();
}

Program to print reverse number


#include<iostream>
using namespace std;
void in()
{
int a,num1=1;
cout<<"Enter Last Number"<<endl;
cin>>a;
num1=a-num1;
cout<<num1;
}
int main(){
in();
}

Program to find product of its digit


#include<iostream>
using namespace std;
int in()
{
int number,prod=1;
cout<<"Enter a number"<<endl;
cin>>number;
while(number>0)
{
prod*=number%10;
number/=10;
}
cout<<"Product of digits: "<<prod<<endl;
}
int main(){
in();
}

Program to find Sum of its digit


#include<iostream>
using namespace std;
int in()
{
int number,sum=0;
cout<<"Enter a number"<<endl;
cin>>number;
while(number>0)
{
sum+=number%10;
number/=10;//iteration
}
cout<<"Sum of digits: "<<sum<<endl;
}
int main(){
in();
}

Program for fibionci series


#include <iostream>
using namespace std;
void in() {
int n,t1=0,t2=1,nextTerm=0;
cout<<"Enter any number"<<endl;
cin>>n;
for(int i=1;i<=n;++i) {
if(i==1) {
cout<<t1<< " ";
continue;
}
if(i==2) {
cout<<t2<< " ";
continue;
}
nextTerm=t1 + t2;
t1 = t2;
t2 = nextTerm;
cout<<nextTerm<< " ";
}
}

int main(){
in();
}

You might also like