#include"iostream.h" #include"conio.h" #include"stdio.h" void findlength() { char str[30]; int l=0; cout<<"\n Enter the string (size < =30) "; gets(str); while(str[l]!='\0') { l++; } cout<<"\n Length Of the given String is: "<< l<< endl; } void compare() { char str1[30], str2[30]; int l1=0,l2=0,i=0,flag=0; cout<<"\n Enter the string1 (size< =30) "; gets(str1); while(str1[l1]!='\0') { l1++; } cout<<"\n Enter the string2 (size< =30) "; gets(str2); while(str2[l2]!='\0') { l2++; } if(l2!=l1) { cout<<"\n Strings are not Equal "; } else { for(i=0;i< l1; i++ ) { if(str1[i]!=str2[i]) { flag=1; break; } } if(flag==1) { cout<< "\n Strings are not Equal "; } else { cout<< "\n Strings are Equal "; } } } void concat() { char str1[30], str2[30]; int l1=0,l2=0,i=0,flag=0; cout<<"\n Enter the string1 (size< =30 ) "; gets(str1); while(str1[l1]!='\0') { l1++; } cout<<"\n Enter the string2 (size< =30 ) "; gets(str2); while(str2[l2]!='\0') { l2++; } for(i=0;i < l2;i++) { str1[l1+i]=str2[i]; } str1[l1+l2]='\0'; cout<<"\n The concatenated String is: "; puts(str1); } void main() { clrscr(); cout<<" Enter your choice \n \t1.Find length of string\n\t" "2.Compare two Strings \n\t3.Concatenate two strings\n\t4.Exit \n"; char ch; cin>> ch; do { if(ch=='1') findlength(); if(ch=='2') compare(); if(ch=='3') concat(); cout<< "Enter your choice \n \t1.Find length of string\n\t" "2.Compare two Strings \n\t3.Concatenate two strings\n\t4.Exit \n"; cin>> ch; }while(ch!='4'); getch(); }
Tuesday, 20 December 2011
C and C++ program to perform various operations on strings
Monday, 19 December 2011
C and C++ program Program to convert the lowercase letter into uppercase and to increment an integer
# include "iostream.h" # include "conio.h" char convert(char ch) { return(ch -32); } int increment(int n) { return(++n); } void main() { int num; char ch1; clrscr(); cout<< "Enter a character in lower case "; cin >> ch1; cout << "Enter an integer "; cin >> num; cout<< "\nUpper Case Aplhabet is => " << convert(ch1); cout<< "\nIncremented Integer is => " << increment(num); }
C and C++ program to find the minimum of two numbers using function overloading
# include "iostream.h" # include "conio.h" int min(int a, int b) { if (a< b ) return(a); else return(b); } char min(char a, char b) { if (a< b ) return(a); else return(b); } double min(double a, double b) { if (a< b ) return(a); else return(b); } void main() { int n, n1; char ch, ch1; double d, d1; clrscr(); cout << "Enter two integers "; cin >> n >> n1; cout << "Enter two characters "; cin >> ch >> ch1; cout << "Enter two double precision quantity "; cin >> d >> d1; cout << "\nMinimum of two integer "<< min(n,n1); cout << "\nMinimum of two characters "<< min(ch,ch1); cout << "\nMinimum of two double precision quantity "<< min(d,d1); }
C and C++ program to calculate the square root of different numbers
# include"iostream.h" # include"math.h" int sroot(int n) { return(sqrt(n)); } long int sroot(long int n) { return(sqrt(n)); } double sroot(double n) { return(sqrt(n)); } void main() { int num; long int num1; double num2; cout << "Enter the value of num "; cin >> num; cout << "Enter the value of num1"; cin >> num1; cout << "Enter the value of num2 "; cin >> num2; cout << "\nSquare root of int is " << sroot(num); cout << "\nSquare root of long int is " << sroot(num1); cout << "\nSquare root of double is " << sroot(num2); }
C and C++ program to calculate area of square and area of rectangle using function
#include "iostream.h" int area(int s) { return(s * s); } float area(int b , int h) { return(0.5 * b * h); } main() { cout << area(5) << endl; cout << area(4,3) << endl; cout << area(6, area(3)) << endl; return 0; }
C and C++ program to calculate the area of rectangle, area of triangle, and area of circle.
#include "iostream.h" #include "conio.h" #include "iomanip.h" #include "math.h" float area(float a, float b , float c); float area(float l, float w); float area(float r); void main() { char ch; float len, wid, n1, n2, n3, ar; float radius; int choice1; clrscr(); cout << "\n1. For area of triangle "; cout << "\n2. For area of rectangle "; cout << "\n3. For area of circle "; cout << "\nEnter choice : "; cin >> choice1; if (choice1 == 1) { cout << "\nEnter the three sides of triangle : "; cin >> n1 >> n2 >> n3; ar = area(n1, n2, n3); cout << "\nArea of triangle is : " << ar; } if (choice1 == 2) { cout << "\nEnter the length : "; cin >> len; cout << "\nEnter the width : "; cin >> wid; cout << "\nArea of rectangle is: " << area(len, wid); } if (choice1 == 3) { cout << "\nEnter the radius : "; cin >> radius; cout << "\nArea of circle : " << area(radius); } } float area(float a, float b , float c) { float s; float a1; s = (a + b + c) / 2; a1 = sqrt(s * (s-a) * (s-b) * (s-c)); return(a1); } float area(float l, float w) { return(l*w); } float area( float radius) { return(3.14 * radius * radius); }Source: www.LearnCandCpp.com
C and C++ program to calculate area of rectangle and triangle using function overloading
#include "iostream.h" #include "conio.h" #include "iomanip.h" #includefloat area(float a, float b , float c); float area(float l, float w); float area(float r); void main() { char ch; float len, wid, n1, n2, n3, ar; float radius; int choice1; clrscr(); cout << "\n1. For area of triangle "; cout << "\n2. For area of rectangle "; cout << "\n3. For area of circle "; cin >> choice1; if (choice1 == 1) { cout << "\nEnter the three sides of triangle : "; cin >> n1 >> n2 >> n3; ar = area(n1, n2, n3); cout << "\nArea of triangle is: " << ar; } if (choice1 == 2) { cout << "\nEnter the length "; cin >> len; cout << "\nEnter the width: "; cin >> wid; cout << "\nArea of rectangle is: " << area(len, wid); } if (choice1 == 3) { cout << "\nEnter the radius "; cin >> radius; cout << "\n Area of circle " << area(radius); } } Source: www.LearnCandCpp.com
Friday, 16 December 2011
C and C++ program to find the sum of a GP (Geometric progression)
#include "iostream.h" #include "conio.h" // Function to calculate the exponential values int expo(int power, int base) { int prod = 1, i = 1; while (i<=power) { prod = prod * base; i++; } return(prod); } void s3() { int n, a, r, i = 1, exp; float term, sum = 0; cout << "\n\tEnter the value of n in the series "; cout << "\n\t a + ar + ar^2 +...+ ar^(n-1) "; cin >> n; cout << "\n\tEnter the value of a in the series "; cout << "\n\t a + ar + ar^2 +...+ ar^(n-1) "; cin >> a; cout << "\n\tEnter the value of r in the series "; cout << "\n\t a + ar + ar^2 +...+ ar^(n-1) "; cin >> r; while(i<=n) { exp = expo((i - 1), r); term = float(a * exp); sum = sum + term; i++; } cout << "\n\n\tThe sum is " << sum; } void main() { clrscr(); s3(); }Source: www.LearnCandCpp.com
C and C++ program to find the sum of first N terms
#include "conio.h" #include "iostream.h" // Function to find the sum of series 1 + 2 + 3 + ......+ N int sumseries(int N) { int i, sum = 0; for(i = 0; i <= N; i++) sum = sum + i; return(sum); } void main() { clrscr(); int n, nsum = 0; cout << "Enter the value of N : "; cin >> n; nsum = sumseries(n); cout << "The sum up to N is : " << nsum; }Source: www.LearnCandCpp.com
C and C++ program to find number of vowels, consonants, digits and other characters in a string.
// Program to find total number of vowels, digits and other characters in string #include "iostream.h" #include "conio.h" #include "ctype.h" #include "stdio.h" void main() { char str[50]; int i, vc= 0, dc = 0, ot = 0; clrscr(); cout << "\n\tEnter any string -> "; gets(str); i = 0; while (str[i] != '\0') { str[i] = tolower(str[i]); if (((str[i] == 'a')) || ((str[i] == 'e')) || ((str[i] == 'o')) || ((str[i] == 'u'))) vc++; else if ((str[i] >= '0') && (str[i] <= '9')) dc++; else ot++; i++; } cout << "\n\tNumber of vowels are : " << vc; cout << "\n\tNumber of digits are : " << dc; cout << "\n\tNumber of other characters are : " << ot; }Source: www.LearnCandCpp.com
C and C++ program to accept an integer and convert it into an equivalent binary
// Program to convert an integer number into its equivalent binary #include "iostream.h" #include "conio.h" #include "stdlib.h" #include "stdio.h" #include "string.h" // Function to convert an integer number into binary number void integer_to_binary(int num) { long binary = 0; int decimal, X[10], d1, d2, q, r; // Convert the number into decimal d1 = num; int i = 0; while (d1 >= 2) { q = d1 / 2; r = d1 % 2; X[i] = r; d1 = q; if (d1 < 2) { i++; X[i] = d1; } else i++; } int j = i; for (i = j; i >= 0; i--) { cout << X[i]; } } main() { int num; clrscr(); cout << "Enter any integer number "; cin >> num; integer_to_binary(num); return 0; } Source: www.LearnCandCpp.com
C and C++ program to generate first N prime numbers
// Program to find the prime numbers in between n #include#include #include void main() { int n, i, flag; clrscr(); cout << "\n\t\t Enter any number => "; cin >> n; if (n < 5) { cout << "Your input is less than 5"; getch(); return; } i = 2; int j = 2; while (j < n-1 ) { flag = 0; i = 2; while (i< j ) { if(j%i == 0) { flag = 1; break; } i++; } if(flag == 0) cout<<"The number is " << j << endl; j++; } } Source: www.LearnCandCpp.com
Tuesday, 13 December 2011
C++ program for matching braces in a parenthesised algebraic expression.
#include#include #include int match_paren(char str[20]) { int i, j; cout << "\n\tThe entered string is "; for (i = 0; str[i] != '\0'; i++) cout << str[i]; int stp = 0; // Let starting parenthesis is 0 int enp = 0; // and ending parenthesis is also 0 for (i = 0; str[i] != '\0'; i++) { if (str[i] == '(') stp = stp + 1; else if (str[i] == ')') enp = enp + 1; } if (stp == enp) return 1; else return 0; } void main() { clrscr(); int ctr; char nstr[20]; cout << "Enter any algebraic equation : "; gets(nstr); ctr = match_paren(nstr); if (ctr == 1) cout << "\nThe parenthesis are match equally"; else cout << "\nParenthesis are not matched"; } Source: www.LearnCandCpp.com
If you have any difficulty in understanding the programming concept of this code, just reply with your comment. I shall reply soon.
Sunday, 11 December 2011
C++ program that returns the position for a substring
The program coding can be given as:
#include#include #include // Function to search a given string within a string and its position int search(char str[20], char substr[10]) { int j = 0, i, flag = 0, pos; for(i=0; str[i]!='\0'; i++) { if (substr[j] =='\0') break; if (str[i] == substr[j] ) { pos = i; flag = 1; if (substr[j] =='\0') break; j++; } else flag = 0; } for(j=0; substr[j]!='\0'; j++); if (flag == 1) { pos = (pos - j + 2); return(pos); } else return(-1); } void main() { char nstr[20], nsub[10]; int ctr = 0; cout << "Enter main string : "; gets(nstr); cout << "Enter search string : "; gets(nsub); ctr = search(nstr, nsub); if (ctr == -1) cout << "String does not found."; else cout << "String found at " << ctr << " position."; }
Source: www.LearnCandCpp.com
Saturday, 10 December 2011
C++ program to check if the string is palindrome or not
// Program to check whether the string is palindrome or not. #includeSource: www.LearnCandCpp.com#include #include void main() { char str[20]; int i, j; cout << "\n\t"; cout << "\n\tEnter the string "; cin >> str; // To read the string char flag = 'y'; cout << "\n\tThe entered string is "; for (i = 0; str[i] != '\0'; i++) cout << str[i]; for (j = 0, i -= 1; i>j; j++, i--) { if (str[i] != str[j]) // Checking for palindrome { flag = 'n'; break; } } if (flag == 'n') cout << " is not palindrome "; else cout << " is a palindrome"; getch(); }
For any query, post it as a comment below.
C++ program to calculate the sales of a salesman
Below the program that fulfills above description:
#include "iostream.h"
#include "conio.h"
main()
{
int sales, prod, i, j;
int array[3][3];
clrscr();
for (i = 0; i < =2; i++)
{
cout << "Enter the salesman " << i+1 << " product \n";
for (j = 0; j < =2; j++ )
cin >> array[i][j];
}
// Total sales of individual salesman
for (i = 0; i < =2; i++)
{
sales = 0;
for (j = 0; j < =2; j++)
sales = sales + array[i][j];
cout << "Total sales of salesman " << i+1 << " is " << sales << endl;
}
cout << endl;
// Total of individual products
for (i = 0; i < =2; i++)
{
prod = 0;
for (j = 0; j < =2; j++ )
prod = prod + array[j][i];
cout << "Total product of product " << i+1 << " is " << prod << endl;
}
return 0;
}
Source: www.LearnCandCpp.com
C++ program that counts the number of words in a string or a line
#include#include main() { char str[50]; int i, count = 1; cout << "\n\t Enter the string "; gets(str); while((str[i]!= '\0') && (str[i+1] != ' ')) { if ((str[i] == ' ') || (str[i] == '.')) count++; i++; } cout << "\n\t Number of words in a string is " << count; return 0; }
Source: www.LearnCandCpp.com
If you have any problem regarding this program, you may leave your comment below.
C++ program to print first 10 multiples of a number
#include#include main() { int N, i; clrscr(); cout << "Enter the number to find the multiple "; cin >> N; // Printing the multiple of N for (i = 1; i<=10; i++) cout << N << " * " << i << " =" << N * i << endl; return 0; }
Source: www.LearnCandCpp.com
A C++ program that interchange or swap the values of two integers without using third variable
#include#include // Function to swap A and B without using third variable void SWAP(int A, int B) { A = A + B; B = A - B; A = A - B; cout << "\n\tAfter swapping a is - > " << A; cout << "\n\tAfter swapping b is - > " << B; getch(); } void main() { clrscr(); int x, y; cout << "Enter first number : "; cin >> x; cout << "Enter second number : "; cin >> y; SWAP(x, y); } Source: www.LearnCandCpp.com
If you have any problem in understanding the concept of the program, you can ask your query by posting it as a response below.
C and C++ program to find the sum of exponential series
This is a simple C++ program which will show how to find the sum of series which are exponential in nature. Given below is one of the sample program
#include "iostream.h" #include "math.h" #include "conio.h" // The function is double SumSeries(double x, int m) { double s = 1/x; float xpower, factVal, fact; int i, j; for(i=0;i<=m;i++) { xpower = 1; for (j=2; j<=i; j++) xpower = xpower * x; factVal = i + 1; fact = 1; for (j=1; j<=factVal; j++) fact = fact * j; if ((i % 2) == 0) s = s - (fact/xpower); else s = s + (fact/xpower); } return(s); } void main() { clrscr(); int m; double sum = 0; double x; cout << "Enter the value of x : "; cin >> x; cout << "Enter the value of m : "; cin >> n; sum = SumSeries(x, m); cout << "The sum of series is : " << sum; } Source: www.LearnCandCpp.comIf you have any query regarding the programming structure or understanding in any concept, you may post it below as a response.
C and C++ program to find the sum of series in exponential form
1 + x/2! + x2/4! + x3/6! + x4/8! + ...... + xn/(2n)!
#include#include double seqsum(double x, int n) { double sum=1, num=1, div=2, fact=1, term; for(int i=2;i<=n;i++) { num=num*n; fact=fact*(div)*(div-1); term=(num/fact); div=div+2; sum=sum+term; } return sum; } void main() { clrscr(); int x, n; double sum = 0; cout << "Enter the value of x : "; cin >> x; cout << "Enter the value of n : "; cin >> n; sum = seqsum(x, n); cout << "The value of the series : " << sum; }
Source: www.LearnCandCpp.com
Saturday, 19 November 2011
Introduction
This is a C ++ programming forum. You can discuss every thing related to C++ programming and ask any thing related to C++ programming. Our expert team will answer it correctly,
Regards,
Team-[C++ Forums]
NOTE: One important thing to be told here that this blog has been inspired and prepared from one of it's inspiration. And hence most of the content of this blog can be found there.
Link to that blog is : www.LearnCandCpp.com