0% found this document useful (0 votes)
38 views7 pages

C++ Programs

This document contains 7 C++ code examples demonstrating different programming concepts: 1) A GCD function to find the greatest common divisor of two numbers. 2) A prime number checker function that tests if a number is prime. 3) A factorial function that calculates the factorial of a given number. 4) A Fibonacci series generator that prints the Fibonacci series up to a given number of terms. 5) A function that checks if a number is odd or even. 6) A function that counts the number of characters in a string. 7) A function that counts the number of vowels in a string.

Uploaded by

Shav Aggrawal
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)
38 views7 pages

C++ Programs

This document contains 7 C++ code examples demonstrating different programming concepts: 1) A GCD function to find the greatest common divisor of two numbers. 2) A prime number checker function that tests if a number is prime. 3) A factorial function that calculates the factorial of a given number. 4) A Fibonacci series generator that prints the Fibonacci series up to a given number of terms. 5) A function that checks if a number is odd or even. 6) A function that counts the number of characters in a string. 7) A function that counts the number of vowels in a string.

Uploaded by

Shav Aggrawal
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
You are on page 1/ 7

C++ CODES:

1)GCD:-
#include <iostream.h>
#include <conio.h>
#include <stdio.h>
class ABC
{
int gcd;
public:
int GCD(int x,int y)
{
if(x>0&&y>0)
{
for(int i=1;i<=x;i++)
{
if(x%i==0&&y%i==0)
{
gcd=i;
}
}
return gcd;
}
else
{
return 0;
}
}
};
void main()
{
cout<<"Enter 2 numbers to find GCD of"<<endl;
int num1,num2;
cin>>num1;
cin>>num2;
ABC obj;
if(obj.GCD(num1,num2)>0)
{
cout<<"GCD of 2 numbers = "<<obj.GCD(num1,num2)<<endl;
}
else
{
cout<<"INVALID NUMBERS";
}
}

2)PRIME NUMBER
#include <iostream.h>
#include <conio.h>
#include <stdio.h>
class PRIME
{
public:
void prime(int x)
{
int count=0;
for(int i=2;i<=x;i++)
{
if(x%i==0)
{
count++;
}
}
if(count==1)
{
cout<<"PRIME NUMBER";
}
else
{
cout<<"NOT PRIME NUMBER";
}
}
};
void main()
{
PRIME obj;
int n;
cout<<"Enter positive number"<<endl;
cin>>n;
obj.prime(n);
getch();
}

3)FACTORIAL
#include <iostream.h>
#include <conio.h>
#include <stdio.h>
class ABC
{
public:
void factorial(int n)
{
int prod=1;
for(int i=1;i<=n;i++)
{
prod*=i;
}
cout<<"Factorial of number entered = "<<prod;
}
};
void main()
{
clrscr();
ABC obj;
int num;
cout<<"Enter a positive number"<<endl;
cin>>num;
obj.factorial(num);
getch();
}

4)FIBONACCI
#include <iostream.h>
#include <conio.h>
#include <stdio.h>
class ABC
{
public:
void fibonacci(int n)
{
int a=0,b=1,c;
if(n==1)
{
cout<<"0";
}
if(n==2)
{
cout<<"0\t1";
}
if(n>2)
{
cout<<"0\t1";
for(int i=1;i<=n-2;i++)
{
c=b;
b=a+b;
a=c;
cout<<"\t"<<b;
}
}
}
};
void main()
{
clrscr();
int num;
cout<<"Enter number of terms of fibonacci series"<<endl;
cin>>num;
ABC obj;
obj.fibonacci(num);
getch();
}

5)ODD/EVEN
#include <iostream.h>
#include <conio.h>
#include <stdio.h>
class ABC
{
public:
void polarity(int n)
{
if(n%2==0)
{
cout<<"EVEN";
}
else
{
cout<<"ODD";
}
}
};
void main()
{
clrscr();
ABC obj;
int num;
cout<<"ENTER A NUMBER :"<<endl;
cin>>num;
obj.polarity(num);
getch();
}
6)COUNT NUMBER OF CHARACTERS
#include <iostream.h>
#include <conio.h>
#include <stdio.h>
#include <string.h>
class ABC
{
public:
void letter(char s1[100])
{
int l=strlen(s1);
int w=0;
for(int i=0;i<l;i++)
{
if(s1[i]!=' ')
{
w++;
}
}
cout<<"Number of characters in string is "<<w;
}
};
void main()
{
ABC obj;
char string[100];
cout<<"Enter String of max 100 words"<<endl;
cin.getline(string,100);
obj.letter(string);
}

7)To COUNT Vowels


#include <iostream.h>
#include <conio.h>
#include <stdio.h>
#include <string.h>
class ABC
{
public:
void vowel(char str[80])
{
int v=0;
int l=strlen(str);
for(int i=0;i<l;i++)
{
if(str[i]=='a'||str[i]=='e'||str[i]=='i'||str[i]=='o'||str[i]=='u'||
str[i]=='A'||str[i]=='E'||str[i]=='I'||str[i]=='O'||str[i]=='U')
{
v++;
}
}
cout<<"Number of Vowels in String = "<<v<<endl;
}
};
void main()
{
ABC obj;
char string[80];
cout<<"Enter String of Max 80 Letters"<<endl;
cin.getline(string,80);
obj.vowel(string);
}

You might also like