Ads 468x60px

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(); }

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; }

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; }

C and C++ program to accept an integer and convert it into an equivalent binary

In this post I am going to publish a C++ program which will accept an integer from the user and print it's binay on the output screen. The coding of the program goes as follows :

// 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

In this article I am going to post a C+ program which will generate first N prime numbers where N will be obtained as a input from the user. If there is a wrong input, the program will display the error message. The coding of program goes as follows:
// 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++; } }

Tuesday, 13 December 2011

C++ program for matching braces in a parenthesised algebraic expression.

This is a simple C++ program which checks for matching braces in a parenthesised algebraic expression. If the match is successful, the function defined will return 1 or else return 0. The program goes as follows:

#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

This is a C++ program that accepts string from user and search for a sub string (group of continuous characters). If the search is successful, the function should return the position of sub string and otherwise -1.
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