Posts

C++ User defined Function in Array Programming.

1. C++ Function Program to Sum all the digits from an Integer Array. The below program will work as the points mention hereafter: Explanation: the main( ) function will call the user defined function named arraysum(). the prototype of arraysum() is interger and it will pass two parameter(int[],int). The first part is for array and the second part is for length of the given array. The arraysum() function receives all the data and doing sum using looping technique and print inside the function. Here function does not returns anything. It only receives value from main( ) program. #include<iostream> #include<conio.h> using namespace std; int arraysum(int[],int); int main() { int n,j; cout<<"Enter the length of the array"<<endl; cin >>n; //initializing the array length int x[n]; //fill up the array using integer data cout<<"Enter the elements :"<<endl; for(j=0;j<n;j++) {  cin>>x[j]; } //call the fu

C Program for Sum of all the Multiples of 3 or 5 below 1000 : From ProjectEuler.net

/* If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23. Find the sum of all the multiples of 3 or 5 below 1000. */ #include<stdio.h> #include<conio.h> int main() {     long sum=0,range=1;     clrscr();     do{     if(range%3==0 || range%5==0)         sum+=range;         range++;     }while(range<1000);     printf("\nThe sum is: %ld", sum);     getch();     return 0; } Answer: 233168 233168 233168 233168 Hints/Background Knowledge: Type Bytes Range --------------------------------------------------------------- short int 2 -32,768 -> +32,767 (32kb) unsigned short int 2 0 -> +65,535 (64Kb) unsigned int 4 0 -> +4,294,967,295 ( 4Gb) int 4 -2,147,483,648 -> +2,147,483,647( 2Gb) long int 4 -2,147,483,648 -> +2,147,483,647( 2Gb) signed char 1 -128 -> +127 unsi

C++ Function: Overloading Example

#include <iostream>   using namespace std ;   /* Number of arguments are different */   void display ( char [ ] ) ; // print the string passed as argument void display ( char [ ] , char [ ] ) ;   int main ( ) { char first [ ] = "C programming" ; char second [ ] = "C++ programming" ;   display ( first ) ; display ( first, second ) ;   return 0 ; }   void display ( char s [ ] ) { cout << s << endl ; }   void display ( char s [ ] , char t [ ] ) { cout << s << endl << t << endl ; }

C++ Function: Print 45 Asterisks for Design the output

using namespace std; void starline( ); into main () { starline ( ); cout << "Data type Range" << endl; starline ( ); cout << "char -128 to 127 " << endl << "short -32 ,768 to 32,767"<< endl << "int system independent: << endl << " long q-2,147,483,648 to 2,147,483,647" << endl; starline ( ); return 0; } //"""""""""""""""""""""""".. //starline ( ) // function defintion void starline ( ) { for(into j=0;j<45; j++) cout << "*" ; cout << endl; }      The output from the program looks like this ************************************* Data type Range ************************************* Char -128 to 127 Short -32,768 to 32,767 Into system dependent Double -2,147,483,648 to 2,147,483,647 *******

C++ Function: Adding Two Numbers with default value parameter

#include <iostream> using namespace std ; int sum ( int a , int b = 20 ) { int result ; result = a + b ; return ( result ); } int main () { // local variable declaration: int a = 100 ; int b = 200 ; int result ; // calling a function to add the values. result = sum ( a , b ); cout << "Total value is :" << result << endl ; // calling a function again as follows. result = sum ( a ); cout << "Total value is :" << result << endl ; return 0 ; }

C++ Function : Find Maximum value between two natural numbers

#include <iostream> using namespace std ; // function declaration int max ( int num1 , int num2 ); int main () { // local variable declaration: int a = 100 ; int b = 200 ; int ret ; // calling a function to get max value. ret = max ( a , b ); cout << "Max value is : " << ret << endl ; return 0 ; } // function returning the max between two numbers int max ( int num1 , int num2 ) { // local variable declaration int result ; if ( num1 > num2 ) result = num1 ; else result = num2 ; return result ; }

Char Array Calculation

Write a C++ program which will accept an expression as input and performs the operation based on operators and operands. Sample input:  c = a * 3 + b / 2 (white space is not allowed)  Hints: Char Array / String Array will be required to perform this operation.