Professional Documents
Culture Documents
C++ Programs
C++ Programs
Algorithm:
2. Check if the given numbers are divisible by any number less than
the number itself using for loop.
4. After termination of the loop, the last updated value in gcd will
be GCD.
#include<iostream>
using namespace std;
int main()
{
int fnum,snum,gcd,lcm;
cout<<"Enter first number";
cin>>fnum;
cout<<"\nEnter second number";
cin>>snum;
//find factors of both numbers
for(int i=1;i<=fnum && i<=snum;i++)
{
if(fnum%i==0 && snum%i==0)
gcd=i;
}
//find lcm of both numbers
lcm = fnum*snum/gcd;
cout<<"\n GCD of given numbers is:"<<gcd;
cout<<"\n LCM of given numbers is:"<<lcm;
return 0;
}
Output:
Algorithm:
#include<iostream>
using namespace std;
int main()
{
int n,sum=0;
cout<<"Enter number till which you would like to add";
cin>>n;
while(n>0)
{
sum+=n;
n--;
}
cout<<"\n sum is:"<<sum;
return 0;
}
Enter number till which you would like to add: 3
sum is:6
Program to Extract Substrings from a given string in C++
Algorithm:
8. Print substr.
Code:
#include <iostream>
#include <string>
using namespace std;
void substring(string str, int start, int length)
{
int i=start, j;
string substr;
for(j = 0; str[i] !='\0' && length > 0; i++, j ++)
{
substr[j] = str[i];
length--;
}
substr[j] = '\0';
cout<<"\n";
for(int k=0;substr[k]!='\0';k++)
cout<<substr[k];
}
int main()
{
string str;
int start,endlen,len;
cout<<"Enter a string: ";
getline(cin,str);
len=str.length();
cout<<"\n Enter starting position of substring : ";
cin>>start ;
cout<<"\n Enter length of substring: " ;
cin>>endlen;
if(start > 0 && start < 30 && endlen<len )
substring(str,start,endlen);
else
cout<<"Values are invalid\n";
return 0;
}
Output:
Morning
Program to Sort Characters of string in alphabetical order in
C++
Algorithm:
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string str;
cout<<"Enter the string to be sorted: ";
getline(cin,str);
int len = str.length();
cout<<"\n String before sorting: "<<str<<" \n";
Enter the string to be sorted: quick brown fox jumps over the lazy dog
String before sorting: quick brown fox jumps over the lazy dog
Algorithm:
4. Compare the original number with the final sum and output
accordingly.
Code:
#include<iostream>
using namespace std;
int calc(int n, int power)
{
int product=1;
for(int i=0;i<power;i++)
{
product*=n;
}
return product;
}
int main()
{
int i,count=0,rem,sum=0;
cout<<"Enter a number";
cin>>i;
int num=i;
//count digits in number
while(i>0)
{
i=1/10;
count++;
}
i=num;
while(i>0)
{
rem=i%10;
sum=sum+calc(rem,count);//calculate power of rem
i=i/10;
}
if(num==sum)
cout<<"\nArmstrong number!";
else
cout<<" \n Not an Armstrong Number!";
return 0;
}
Output:
Enter a number 3
Armstrong number!
Program to print inverted Floyd’s triangle star pattern in
C++
Algorithm:
#include <iostream>
using namespace std;
int main()
{
int n;
cout<<"Enter number of rows: ";
cin>>n;
cout<<"\n";
for (int i = n; i >= 1; --i)
{
for (int j = 1; j<= i; ++j)
{
cout<<"* ";
}
cout<<"\n";
}
return 0;
}
Output:
* * * * *
* * * *
* * *
* *
*
Program to Add two numbers using pointers in C++
Algorithm:
#include <iostream>
using namespace std;
int main()
{
int num1, num2;
int *ptr1,* ptr2;
int sum;
cout<<"\n Enter first number: ";
cin>>num1;
cout<<"\n Enter second number: ";
cin>>num2;
ptr1 = &num1; //assigning an address to pointer
ptr2 = &num2;
sum = *ptr1 + * ptr2; //values at address stored by pointer
cout<<"\n Sum is: "<< sum;
return 0;
}
Output:
Algorithm:
1. Take input.
3. Run a for loop which runs from 1 till the given number.
#include<iostream>
using namespace std;
int main()
{
int n,fact=1;
cout<<"Enter number";
cin>>n;
for(int i=1;i<=n;i++)
fact=fact*i;
cout<<"\n Factorial of given number is:"<<fact;
return 0;
}
Output:
Enter number: 6
Factorial of given number is:720
Program to print Fibonacci Series in C++
Algorithm:
2. Take two variable, pre and next and assign pre = 0 and next = 1.
3. Take another variable, last which will be the sum of pre and next.
#include<iostream>
using namespace std;
int main()
{
int n,pre,next,last;
cout<<"How many numbers of fibonacci series do you want to
print?";
cin>>n;
pre=0; //previous number
next=1; //next number
last=pre+next;
while(n>0)
{
cout<<"\n"<<pre;
pre=next; //pushing the three values ahead
next=last;
last=pre+next; //third number is sum of new first and
second number
n--;
}
return 0;
}
Output:
Algorithm:
#include <iostream.h>
#include<conio.h>
#include<string.h>
void main()
{
char str1[30]="blue";
char str2[30] = "oceans";
int i=0,stop;
i=stop+1;
//concate strings
for(int j = 0; str2[j] != '\0'; j++, i++)
str1[i] = str2[j]; //copying chars of string2 in 1, one
by one
Algorithm:
#include <iostream>
#include <string>
using namespace std;
int vowel(char c)
{
switch(c)
{
case 'a' :
{return 0;break;}
case 'A' :
{return 0;break;}
case 'e':
{return 1;break;}
case 'E':
{return 1;break;}
case 'i' :
{return 2;break;}
case 'I':
{return 2;break;}
case 'o':
{return 3;break;}
case 'O':
{return 3;break;}
case 'u' :
{return 4;break;}
case'U':
{return 4;break;}
default:
{return 5;break;}
}
}int main()
{
string str;
int count[5]={0},x;
cout<<"Enter a string: ";
getline(cin,str);
int len=str.length();
for(int i = 0; i<len; i++)
{ x=vowel(str[i]);
if(x<5)
count[x]+=1;
}
cout<<"\n a:"<< count[0];
cout<<"\n e:"<<count[1];
cout<<"\n i:"<<count[2];
cout<<"\n o:"<<count[3];
cout<<"\n u:"<<count[4];
return 0;
}
Output:
Algorithm:
1. Input a string.
#include <iostream>
#include <cstring>
using namespace std;
int vowel(char c)
{
if (c == 'a' || c == 'A' || c == 'e' || c == 'E' || c == 'i' || c
== 'I' || c =='o' || c=='O' || c == 'u' || c == 'U')
return 1; // a vowel
else
return 0; // not a vowel
}
int main()
{
string str,newstr;
cout<<"Enter a string: ";
getline(cin,str);
int len=str.length();
int j=0;
for(int i = 0; i<len; i++)
{
if(vowel(str[i]) == 0)
{
newstr[j] = str[i]; //newstr is string without
vowels
j++;
}
}
newstr[j] = '\0'; //terminate the string
strcpy(str, newstr); //copying the new string,
cout<<"Modified String:"<<str;
return 0;
}
Output:
Algorithm:
8. Print substr.
Code:
#include <iostream>
#include <string>
using namespace std;
void substring(string str, int start, int length)
{
int i=start, j;
string substr;
for(j = 0; str[i] !='\0' && length > 0; i++, j ++)
{
substr[j] = str[i];
length--;
}
substr[j] = '\0';
cout<<"\n";
for(int k=0;substr[k]!='\0';k++)
cout<<substr[k];
}
int main()
{
string str;
int start,endlen,len;
cout<<"Enter a string: ";
getline(cin,str);
len=str.length();
cout<<"\n Enter starting position of substring : ";
cin>>start ;
cout<<"\n Enter length of substring: " ;
cin>>endlen;
if(start > 0 && start < 30 && endlen<len )
substring(str,start,endlen);
else
cout<<"Values are invalid\n";
return 0;
}
Output:
Morning
Program to Swap two strings in C++
In this method,
#include <iostream>
int main() {
int n; //length of string
cin>>n;
char s1[n];
char s2[n];
char s3[n]; //temporary string
//Input String 1
cin>>s1;
//Input String 2
cin>>s2;
strcpy(s3,s1); //copy contents of s1 in s3
strcpy(s1,s2); // similar to s1=s2
strcpy(s2,s3);
cout<<s1<<"\n";
cout<<s2;
return 0;
}
Advantage:
Disadvantage:
1. Check if the character is between ‘a’ and ‘z’ i.e. it is a lower case
letter.
#include <iostream>
using namespace std;
int main()
{
string str;
cout<<"Enter the string ";
getline(cin,str);
lower_string(str); //function call to convert to lowercase
upper_string(str); //function call to convert to uppercase
return 0;
}
Output:
int main()
{
printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");
int num;
printf("\nHello world!\nWelcome to Studytonight: Best place to learn\n");
printf("\n\n\t\t\tCoding is Fun !\n\n\n");
return 0;
}
Output:
Program to take input of various datatypes in
C
#include<stdio.h>
int main()
{
printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");
return 0;
}
Output:
A C++ program to read an array of 10 elements and print the sum of all elements of array:
#include <iostream.h>
void main()
{
int a[10], sum=0;
for(int j=0; j<10; j++) //loop for reading
{
cout<< “Enter Element “<< j+1<<”:”;
cin>> a[j]
sum=sum+a[j]`;
}
cout<<”Sum : “<<sum;
}
Program: Here’s another example of an array this program , invites the user to enter a series of six
values representing sales for each day of the week (excluding Sunday), and then calculates the
average sales
#include <iostream.h>
void main()
{
const int SIZE = 6; //size of array
double sales[SIZE]; //array of 6 elements
double total = 0;
cout<< “Enter sales for 6 days\n”;
for(int j=0; j<SIZE; j++) //put figures in array
cin>> sales[j];
for(j=0; j<SIZE; j++) //read figures from array
total += sales[j]; //to find total
double average = total / SIZE; // find average
cout<< “Average = “ << average << endl;
}
Output:-