Sunday, May 25, 2014

Calculate the size of an array at run time

Calculate the size of an array at run time


In c language there is no direct way to calculate the size of an array at run time;

For example:

int array[10] = {1,2,3,4,54,6,7,8,9,3}

To calculate the size of above array at run time:

int size = sizeof(array) / sizeof(array[0]);


sizeof(array) = 20 for 16 bit (2 Byte) compiler
sizeof(array) = 40 for 32 bit (4 Byte) compiler 


sizeof(array[0]) = 2 for 16 bit (2 Byte) compiler
sizeof(array[0]) = 4 for 32 bit (4 Byte) compiler


// For 16 Bit compiler
size = 20 / 2
size = 10
//For 32 Bit Compiler
size = 40 / 4 for 32 Bit
size = 10

What is sizeof() ?

By +Dixit Singla 

Saturday, November 16, 2013

Sum of primes below 5000

What is the sum of all prime numbers below 5000?

Language C++:


#include<iostream>
using namespace std;

class prime {
          private:
                int sumprime;
          public:
                prime() { 
                      sumprime = 2;
                }
                void calc_sumprime() { 
                      int i,counter = 0;
                      int num = 3;
                      while (num <= 5000) { 
                              for(i = 2 ; i < num ; i++) { 
                                      if((num % i) == 0) {
                                              counter++;
                                              break;
                                       }
                               }
                               if(counter == 0) {
                                      sumprime += num;
                               }
                               counter = 0;
                               num++;    
                       }
                       cout<<"sum = "<<sumprime<<endl;
                 }
};

int main() {
        prime obj;
        obj.calc_sumprime();
        return(0);
}

Output:
1548136

By +Dixit Singla 

Sum of first 250 prime numbers


In math, a prime number is a number only divisible by 1 and itself.

Given the first few prime numbers2 3 5 7 11 13 17 ...

What is the sum of the first 250 prime numbers? Note that you are seeing the first 7 primes above.

Language C++:

#include<iostream>
using namespace std;

class prime {
       private:
              unsigned int sum_prime;
       public:
              prime() {
              sum_prime = 2;
              }
              void sum_of_primes_calc() { 
                          int pcounter = 1;
                          int num = 3;
                          int i,counter = 0;
                          while(pcounter != 250) {
                                   for (i = 2 ; i < num ; i++) { 
                                               if((num % i) == 0) {
                                                       counter++;
                                                }
                                    }
                                    if(counter == 0) { 
                                            sum_prime += num;
                                            pcounter++;
                                    }
                                    counter = 0;
                                    num++;
                           }
                           cout<<sum_prime<<endl;
              }
};

int main() {
        prime obj;
        obj.sum_of_primes_calc();
        return(0);
}

Output:
182109

Note: To find the sum of primes beyond 250, just change the value in while condition "While(pcounter  !=  250)"

By +Dixit Singla 

Friday, November 15, 2013

Sum of digits


As an example, the sum of the digits of 2 to the 10th power is:

2^10 = 1024  => 1 + 0 + 2 + 4 => 7


What is the sum of the digits of  2^50?
Note:  ^  means exponent

#include<iostream>
using namespace std;

class sumofdigit {
 
             private:
                    long long int val;
             public:
                    sumofdigit() {
                              val = 1;
                    }
                    void calc_sumofdigit () {
                              long long int temp = 0;
                              int i,sum = 0;
                              for (i = 1 ; i <= 50 ; i++) { 
                                          val = val * 2;
                              }
                              cout<<"2^50 = "<<val<<endl;
                              while (val != 0) { 
                                          temp = val % 10;
                                          sum = sum + temp;
                                          val = val/10;
                               }
                               cout<<"Sum = "<<sum<<endl;
                     }
};

int main() { 
        sumofdigit obj;
        obj.calc_sumofdigit();
        return(0);
}

Output:


2^50 = 1125899906842624
Sum = 76

By +Dixit Singla 

Reverse Alphabet Codes


Given the following information:

a = 26, b = 25, c = 24, d = 23 ..... x = 3, y = 2, z = 1

What is the sum of each letter of this sentence: "The quick brown fox jumped over the cow"? 

Note: A white space has no value

Language C++: 

#include<iostream>
using namespace std;

class rev_alph {
 
            private:
                   int sum;
                   string str;
            public:
                   rev_alph() { 
                           sum = 0;
                           str = "The quick brown fox jumped over the cow";
                   }
                   void reversealpha() { 
                           int i;
                           str[0] = tolower(str[0]);
                           int len = str.length();
                           for (i = 0 ; i < len ; i++) { 
                                     if (str[i] == ' ') {
                                               sum += 0;
                                      }
                                      else {
                                               sum += (26 - (str[i] - 'a'));
                                      }
                            }
                            cout<<sum<<endl;
   
                     }
};

int main() {
       rev_alph obj;
       obj.reversealpha();
       return(0);
}

Output:
450

By +Dixit Singla 

A Power Function


Given the following function and it's results:

f(x,y) = x^y + y^x where x>0 and y>1f(1,2) = 1^2 + 2^1 = 3f(2,3) = 2^3 + 3^2 = 17f(3,4) = 3^4 + 4^3 = 145. . .

Using the above function denoted by f(x,y), and given that the initial values of x = 1 and y = 2, which increase by 1 each time, what are the last 4 digits of the sum of the first 15 function calls.

Language C++:

#include<iostream>
using namespace std;

class power_fun {

          private:
                 long long int sum,last_4_digit;
          public:
                 power_fun () { 
                          sum = 0;
                          last_4_digit = 0;
                 }
                 void show_last_digit() {
                           int x = 1,y = 2,i;
                           for(i = 0 ; i < 15 ; i++) {
                                     sum = sum + function_solver(x,y);
                                     x++;
                                     y++;
                            }
                            cout<<"sum = "<<sum<<endl;
                            last_4_digit = sum % 10000;
                            cout<<"last_4_digit = "<<last_4_digit<<endl;
                   }
                   long long int function_solver(int xx,int yy) { 
                            long long int t1 = 1,t2 = 1;
                            int i;
                            for (i = 1 ; i <= yy ; i++)
                                        t1 = t1*xx;
    
                            for (i = 1 ; i <= xx ; i++)
                                        t2 = t2*yy;
    
                            return(t1+t2);
                   }
};

int main() {
          power_fun obj;
          obj.show_last_digit();
          return(0);
}

Output:

sum = 7910956276398901049
last_4_digit = 1049

US Telephone Keypads


Given the following information about a US telephone touch tone keypad:

   1: (NONE)         2: A,B,C        3: D,E,F
     4: G,H,I              5: J,K,L         6: M,N,O
     7: P,R,S              8: T,U,V        9: W,X,Y

calculate the product of each characters value.

As an example, say the user enters: "Practice", the product would be:
7 * 7 * 2 * 2 * 8 * 4 * 2 * 3 = 37,632

What is the value of this string: "Programming Challenges are fun"?

Language C++:

#include<iostream>
#include<string.h>
using namespace std;

class uskeypad {
 
    private:
           string my_string;
           long long int prod;
    public:
  
           uskeypad() { 
                  prod = 1;
                  my_string = "Programming challenges are fun";
           }
           void calc_prod() {
   
                  int i;
                  int len = my_string.length();
                  for (i = 0 ; i < len ; i++) {
    
                            if(my_string[i] == ' ') {
                                      prod = prod * 1;
                            }
                            else if(
                                  my_string[i] == 'a' || 
                                  my_string[i] == 'b' || 
                                  my_string[i] == 'c' || 
                                  my_string[i] == 'A' || 
                                  my_string[i] == 'B' ||
                                  my_string[i] == 'C' 
                                  ) {
     
                                           prod = prod * 2;
                             }
                             else if(
                                  my_string[i] == 'd' || 
                                  my_string[i] == 'e' || 
                                  my_string[i] == 'f' || 
                                  my_string[i] == 'D' || 
                                  my_string[i] == 'E' ||
                                  my_string[i] == 'F' 
                                    ) {
                                           prod = prod * 3;
                             }
                             else if(
                                  my_string[i] == 'g' || 
                                  my_string[i] == 'h' || 
                                  my_string[i] == 'i' || 
                                  my_string[i] == 'G' || 
                                  my_string[i] == 'H' ||
                                  my_string[i] == 'I' 
                                    ) { 
                                           prod = prod * 4;
                             }
                             else if(
                                  my_string[i] == 'j' || 
                                  my_string[i] == 'k' || 
                                  my_string[i] == 'l' || 
                                  my_string[i] == 'J' || 
                                  my_string[i] == 'K' ||
                                  my_string[i] == 'L' 
                                   ) { 
                                           prod = prod * 5;
                             }
                             else if(
                                  my_string[i] == 'm' || 
                                  my_string[i] == 'n' || 
                                  my_string[i] == 'o' || 
                                  my_string[i] == 'M' || 
                                  my_string[i] == 'N' ||
                                  my_string[i] == 'O' 
                                   ) {
                                           prod = prod * 6;
                             }
                             else if(
                                  my_string[i] == 'p' || 
                                  my_string[i] == 'r' || 
                                  my_string[i] == 's' || 
                                  my_string[i] == 'P' || 
                                  my_string[i] == 'R' ||
                                  my_string[i] == 'S' 
                                   ) { 
                                           prod = prod * 7;
                             }
                             else if(
                                  my_string[i] == 't' || 
                                  my_string[i] == 'u' || 
                                  my_string[i] == 'v' || 
                                  my_string[i] == 'T' || 
                                  my_string[i] == 'U' ||
                                  my_string[i] == 'V' 
                                   ) {
                                           prod = prod * 8;
                             }
                             else if(
                                   my_string[i] == 'w' || 
                                   my_string[i] == 'x' || 
                                   my_string[i] == 'y' || 
                                   my_string[i] == 'W' || 
                                   my_string[i] == 'X' ||
                                   my_string[i] == 'Y' 
                                    ) { 
                                           prod = prod * 9;
                              }
                  }
                  cout<<"prod = "<<prod<<endl;
   
        }
};
int main() {

       uskeypad obj;
       obj.calc_prod();
       return(0); 
}

Output:
prod = 208129028102553600

By +Dixit Singla 

Pattern

Write a program to display the following pattern in C language (Pyramid in square)?

+ + + + + * + + + + +
+       *   *       +
+     *       *     +
+   *           *   +
+ *               * +
* * * * * * * * * * *



#include<stdio.h>
int main() {

int i,j;
int n = 11;        // Value of n should be ODD

for (i = 0 ; i <= (n / 2) ; i++) {
      for (j = 0 ; j < n ; j++) {
                if (i == 0) {
                     if (j == (n / 2 ))
                              printf ("* "); 
                     else
                               printf ("+ ");
                }
                else if (i == (n / 2)) {
                     printf ("* ");
                } 
                else {
                      if (j == 0 || j == (n - 1))
                               printf ("+ ");
                      else if (j == ((n / 2) - i) || j == ((n / 2) + i))
                               printf("* ");
                      else
                               printf ("  ");
                      }
                 } 
                 printf ("\n");
      }
} 

Output:-
+ + + + + * + + + + +
+       *   *       +
+     *       *     +
+   *           *   +
+ *               * +
* * * * * * * * * * *

Note:- To increase or decrease the size of the pattern change the value of variable "n" But keep in mind that the value of n should be ODD (1,3,5,7.....)

By +Dixit Singla 

Sum of factorials

Given the first few factorials:
1! = 1
2! = 2 x 1 = 2
3! = 3 x 2 x 1 = 6
4! = 4 x 3 x 2 x 1 = 24
What is the sum of the first 15 factorials, NOT INCLUDING 0!?

Language C++ :

#include<iostream>
using namespace std;

class fact {
 
          private:
                 unsigned long long int num,sum;
          public:
                 fact() {
                       num = 0;
                       sum = 0;
                 }
                 void fact1() {
                       int i;
                       for(i = 1 ; i <= 15 ; i++) {
                                 sum = sum + factorial_finder(i); 
                       }
                       cout<<"Sum of first 15 factorials is = "<<sum<<endl;
                  }
                  long long int factorial_finder(int val) { 
                                 unsigned long long int i,f = 1;
                                 for (i = 1 ; i <= val ; i++) {
                                               f = f*i;
                                 }
                                 return(f);
                   }
};

int main() {
        fact obj;
        obj.fact1();
        return(0);
}

Output: 

1401602636313

Unsigned long long long int is of 8 Bytes (64 Bits) 

Environment used to run this program :
  • Gcc compiler
  • Ubuntu 11.10 (Linux)
  • vim editor
By +Dixit Singla


Monday, June 10, 2013

Find Prime number in C

Program to find Prime number in C?


Prime Number:  Prime number is a natural number that can only be divided  by 1 or itself .

For example:
13 is a prime number because no number can divide 13 completely. 13 can only be divided by 1 or 13(itself).

Program in C:

#include<stdio.h>
int main()
{
 int i;
 int n;
 int count = 0;
 printf("enter the number \n ");
 scanf("%d",&n);
 
 for(i = 2 ; i < n ; i++)
 {
  if((n%i) == 0)
  { 
   count++;
   break;
  }
  
 }
 
 if(count == 0)
 {
  printf("%d is a prime number \n",n);
 }
 
 else
 {
  printf("%d is not a prime number \n",n);
 }
 
 return 0;                                          
}   

Output:
enter the number
12
12 is not a prime number

enter the number
13
13 is a prime number

By:  +Dixit Singla