Program to find the sum of the series 0.9, 0.99, 0.999, 0.9999, ..........

Problem:

WAP to display the series 0.9, 0.99, 0.999, 0.9999, .......... and also find the sum of the series.

Source Code:

/*to find the sum of the series 0.9, 0.99, 0.999, 0.9999, ..........*/
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int main()
{
    int i, n;
    double j=0, sum=0;
    printf("Enter the length of the series: ");
    scanf("%d", &n);
    printf("The series is\n");
    for(i = 1; i <= n; i++)
    {
        j += 0.9/pow(10, i-1);
        printf("%f, ", j);
        sum += j;
    }
    printf("\nSum of  the series is %f", sum);
    system ("pause");
    return 0;
}

Sample Run:

Enter the length of the series: 5
The series is
0.900000, 0.990000, 0.999000, 0.999900, 0.999990,
Sum of  the series is 4.888890

Program to decide the type of a triangle.

Problem:

Given the lengths of three sides of a triangle. Write a program in C which decides the type (Right Angled, Acute, Obtuse, Isosceles, Scalene, Equilateral etc.) of the triangle.

Source Code:

/*to decide the type of triangle*/
#include <stdio.h>
#include <stdlib.h>
int main()
{
    float a,b,c;
    printf("Enter the length of three sides of a triangle: ");
    scanf("%f%f%f",&a,&b,&c);
    if(a > b + c || b > c + a || c > a+b)
    {
        printf("This is not a triangle.");
        system ("pause");
        exit(0);
    }
    else if( a == b && b == c && c == a)
        printf("This is an equilateral triangle.");
    else if(a==b || b==c || c==a)
        printf("This is an isosceles triangle.");
    else if(a*a == b*b + c*c || b*b == c*c + a*a || c*c == a*a + b*b)
        printf("This is a right angled triangle.");
    else if(a*a > b*b + c*c || b*b > c*c + a*a || c*c > a*a + b*b)
        printf("This is an obtuse angled triangle.");
    else if(a*a < b*b + c*c || b*b < c*c + a*a || c*c < a*a + b*b)
        printf("This is an acute angled triangle.");
    else
        printf("This is a scalene triangle.");
    system ("pause");
    return 0;
}

Sample Output:

Enter the length of three sides of a triangle: 5 6 7
This is an acute angled triangle.

Program to display the number in reverse order.

Problem:

WAP to read four digit number and display the number in reverse order.


Source Code:

/*to display the numbers in reverse order*/
#include<stdio.h>
int main()
{
int n;
printf("Enter a four digit number: ");
scanf("%d", &n);
 printf("The number in reverse order is ");
printf("%d", n %10);
n = n/10;
printf("%d", n %10);
n = n/10;
printf("%d", n %10);
n = n/10;
printf("%d", n %10);
return 0;
}

Sample Run:

Enter a four digit number: 1234
The number in reverse order is 4321


Program to find net salary

Problem:

WAP to read basic salary. Apart from basic salary, allowances are given as follows
a. TA (Travelling Allowance) is 5% of basic salary
b. DA (Daily Allowance) is 3% of basic salary
c. HRA (House Rate Allowance) is 8% of basic salary
Also, tax is witheld 1% of basic salary.
Finally, calculate the net salary


Source Code:

/*to find net salary*/
#include<stdio.h>
int main()
{
float bs, ta, hra, da, tax, ns;
printf("Enter the amount of basic salary: ");
scanf("%f", &bs);
ta = 0.05 * bs;
da = 0.03 * bs;
hra = 0.08 * bs;
tax = 0.01 * bs;
ns = bs + ta + da + hra - tax;
printf("Travelling Allowance = %f", ta);
printf("\nDaily Allowance = %f", da);
printf("\nHome Rate Allowance = %f", hra);
printf("\nTax = %f", tax);
printf("\nNet Salary = %f", ns);
return 0;
}

Sample Run:

Enter the amount of basic salary: 50000
Travelling Allowance = 2500.00
Daily Allowance = 1500.00
Home Rate Allowance = 4000.00
Tax = 500.00
Net Salary = 57500.00


To find total price paid by a customer

Problem:

WAP to read price of three items. Apart from normal cost, a customer has to pay 13% VAT and 10% service charge. Finally, calculate the total price paid by the customer.


Source Code:

/*to find total price*/
#include<stdio.h>
int main()
{
float nc1, nc2, nc3, nc, vat, sc, tp;
printf("Enter normal cost of first item: ");
scanf("%f", &nc1);
printf("Enter normal cost of second item: ");
scanf("%f", &nc2);
printf("Enter normal cost of third item: ");
scanf("%f", &nc3);
nc = nc1 + nc2 + nc3;
vat = 0.13 * nc;
sc = 0.10 * nc;
tp = nc + vat + sc;
printf("Total net cost = %f", nc);
printf("\nVat = %f", vat);
printf("\nService charge = %f", sc);
printf("\nTotal price = %f", tp);
return 0;
}


Sample Run:

Enter normal cost of first item: 3000
Enter normal cost of second item: 4500
Enter normal cost of third item: 6000
Total net cost = 13500.00
Vat = 1755.00
Service charge = 135.00
Total price = 15390.00


Program to read marks in five subjects and calculate the total and percentage

Source Code:

/*to find the total and percentage of an examination*/
#include<stdio.h>
int main()
{
int s1, s2, s3, s4, s5, tot;
float per;
printf("Enter the marks of 5 subjects: ");
scanf("%d %d %d %d %d", &s1, &s2, &s3, &s4, &s5);
tot = s1 + s2 + s3 + s4 + s5;
per = tot / 500.0 * 100;
printf("Total = %d", tot);
printf("\nPercentage = %f", per);
return 0;
}


Sample Run:

Enter the marks of 5 subjects: 90 89 88 87 86
Total = 440
Percentage = 88.00


Program to read principle, time and rate and calculate simple interest

Source Code:

/*to find the simple interest*/
#include<stdio.h>
int main()
{
float p, t, r, si;
printf("Enter the value of principle: ");
scanf("%f", &p);
printf("Enter the time (in years): ");
scanf("%f", &t);
printf("Enter the interest rate: ");
scanf("%f", &r);
si = (p * t * r) / 100;
printf("Simple Interest = %f", si);
return 0;
}

Sample Run:

Enter the value of principle: 10000
Enter the time (in years): 3
Enter the interest rate: 12
Simple Interest = 3600.00


Program to read the length and bredth of a rectangle and find the perimeter

Source Code:

/*to find the perimeter of a rectangle*/
#include<stdio.h>
int main()
{
    float l, b, p;
    printf("Enter the value of length: ");
    scanf("%f", &l);
    printf("Enter the value of breadth: ");
    scanf("%f", &b);
    p = 2 * (l + b);
    printf("Perimeter of the rectangle is %f", p);
    return 0;
}



Sample Run:

Enter the value of length: 5
Enter the value of breadth: 6
Perimeter of the rectangle is 22

 

C program to read the radius of a circle and calculate the area and circumference

Source Code:

/*to find the area and circumference of a circle*/
#include<stdio.h>
#include<math.h>
int main()
{
float r, a, c;
printf("Enter the value of radius: ");
scanf("%f", &r);
a = M_PI * r * r;
c = 2 * M_PI * r;
printf("Area of circle = %f", a);
printf("\nCircumference of the circle = %f", c);
return 0;
}

Sample run:

Enter the value of radius: 5
Area of circle = 78.539818
Circumference of the circle = 31.415926


Defining Member Function in C++

The data member of a class is declared within the body of the class. However, the member functions can be defined in one of the two places.
  • Inside the class definition
  • Outside the class definition

No matter whether the function is defines inside or outside the class definition, the function performs the same operation. But the syntax of the member function definition is different if declared inside or outside. The program code written inside the body of member function is the same whether it is declared inside or outside.

  1. Inside the class definition:

The general syntax of the function definition inside class definition will be as:

class classname
{
    //……………..
    public:
        return_type member_function(args…)
        {
             //function body
        }
};

Following example illustrates the definition of function inside class definition:

Source Code:
//definition inside the class
#include<iostream>
using namespace std;
class student
{
    private:
       int roll;
       char name[20];
       char phone[10];
    public:
       void getdata()
       {
           cout<<"\nEnter Roll Number: ";
           cin>>roll;
           cout<<"Enter Name: ";
           cin>>name;
           cout<<"Enter Phone Number: ";
           cin>>phone;
       }
       void showdata()
       {
           cout<<"Name: "<<name<<endl;
           cout<<"Roll Number: "<<roll<<endl;
           cout<<"Phone Number: "<<phone<<endl;
       }
}; //end of class

int main()
{
    student s1, s2;
    s1.getdata();
    s2.getdata();
    cout<<"First Student"<<endl;
    s1.showdata();
    cout<<"Second Student"<<endl;
    s2.showdata();
    return 0;
}

Sample Run:
Enter Roll Number: 1
Enter Name: John
Enter Phone Number: 1234567890

Enter Roll Number: 2
Enter Name: Marrie
Enter Phone Number: 9876543210

First Student
Name: John
Roll Number: 1
Phone Number: 1234567890
Second Student
Name: Marrie
Roll Number: 2
Phone Number: 9876543210



     2. Outside the class definition:


The general syntax of the function definition outside class definition will be as:

class classname
{
    //……………..
    public:
        return_type member_function(args…);
        //…………….
};

Return_type class_name::member_function(args…)
{
    //function body
}

The declaration can be done in private or public section.
We have presented the same example presented above where member functions are defined outside the class specification.

Source Code:
//definition outside the class
#include<iostream>
using namespace std;
class student
{
    private:
        int roll;
        char name[20];
        char phone[10];
    public:
        void getdata(); //function declaration
        void showdata();
}; //end of class

void student::getdata()  //function definition
{
     cout<<"\nEnter Roll Number: ";
     cin>>roll;
     cout<<"Enter Name: ";
     cin>>name;
     cout<<"Enter Phone Number: ";
     cin>>phone;
}

void student::showdata()
{
     cout<<"Name: "<<name<<endl;
     cout<<"Roll Number: "<<roll<<endl;
     cout<<"Phone Number: "<<phone<<endl;
}

int main()
{
     student s1, s2;
     s1.getdata();
     s2.getdata();
     cout<<"First Student"<<endl;
     s1.showdata();
     cout<<"Second Student"<<endl;
     s2.showdata();
     return 0;
}

Sample Run:
Enter Roll Number: 1
Enter Name: John
Enter Phone Number: 1234567890

Enter Roll Number: 2
Enter Name: Marrie
Enter Phone Number: 9876543210

First Student
Name: John
Roll Number: 1
Phone Number: 1234567890
Second Student
Name: Marrie
Roll Number: 2
Phone Number: 9876543210



Function Overloading in C++

In a program there can be many functions with different name. However, some functions conceptually perform the same task on objects of different types and numbers. In such a case it is convenient to give them same name. When the same name is used for different operation, it is called function overloading. When an overloaded function is called the function with matched arguments is invoked.
Examples of overloaded functions are as follows:
void display(); //function with no arguments
void display(int); //function with one int argument
void display(float); //function with one float argument
void display(int, float) //function with one int and
                         //one float argument

A Complete Example to demonstrate the function overloading is as follows:

Source Code:
//function overloading
#include<iostream>
using namespace std;
int max(int, int);
long max(long, long);
float max(float, float);
char max(char, char);

int main()
{
int i1 = 12, i2 = 22;
cout<<"Greater is "<<max(i1, i2)<<endl;
long l1 = 400000, l2 = 380000;
cout<<"Greater is "<<max(l1, l2)<<endl;
float f1 = 34.04, f2 = 54.455;
cout<<"Greater is "<<max(f1, f2)<<endl;
char c1 = 'f', c2 = 'F';
cout<<"Greater is "<<max(c1, c2)<<endl;
return 0;
}

int max(int i1, int i2)
{
return(i1 > i2 ? i1 : i2);
}

long max(long l1, long l2)
{
return(l1 > l2 ? l1 : l2);
}

float max(float f1, float f2)
{
return(f1 > f2 ? f1 : f2);
}

char max(char c1, char c2)
{
return(c1 > c2 ? c1 : c2);
}

Sample Run
Greater is 22
Greater is 400000
Greater is 54.455
Greater is f

How to make own captcha in Joomla?

The captcha can be used in many places to validate whether the user is real or not. It helps in maintaining the security of the site.
The following function will make an image that contains a random captcha in Joomla.

<?php
        public function makeCaptcha()
        {
            $string = JUserHelper::genRandomPassword ('6');
            $session = JFactory::getSession();
            $session->set('value', $string);
            $width      = 100;
            $height     = 25;
            $image      = imagecreatetruecolor ($width , $height);
            $text_color = imagecolorallocate($image, 130, 130, 130);
            $bg_color   = imagecolorallocate($image, 190, 190, 190);
            imagefilledrectangle($image, 0, 0, $width, $height, $bg_color);
            imagestring($image, 5, 16, 4, $string, $text_color);
            ob_start();
            imagejpeg ($image);
            $jpg = ob_get_clean ();
            return "data:image/jpeg;base64," . base64_encode($jpg);
        }
?>


How to Compile a C Program on Ubuntu

This article will guide you to compile a C program on Ubuntu using the GNU gcc/g++ compiler. Additions were made in order to simplify and clarify the creation of a C program on Ubuntu.

1. Open up a terminal on Ubuntu and install the build-essential package by typing the following command in the terminal
  • sudo apt-get install build-essential
  • This will install the necessary C development libraries for your Ubuntu system to create C programs.
2. Create a directory and a sub directory to hold your C programs and your main HelloWorld program.
  • mkdir -p CProgram/HelloWorld
  • We are using CProgram for the main directory to hold our created C programs and we are using the sub directory HelloWorld to hold our main program.
3. Then we will change into our created directory by issuing the following command
  • cd CProgram/HelloWorld
4. Next we will use a text editor such as gedit or nano to create our C or C++ source code using the following command.

  • gedit main.c
  • OR
  • nano main.c

5. Enter the source code of your program. For example: the HelloWorld program is as follows:

#include<stdio.h>
#include<stdlib.h>
int main()
{
        printf("Hello World,\nThis is my first program compiled on 
Ubuntu.");

        return 0;
}

6. Save the file as main.c and exit.

7. Compiling your C program
  • Make sure you are in the CProgram/HelloWorld directory before you compile your C programs.
        Now type in the terminal:
  • gcc -Wall -W -Werror main.c -o HelloWorldC
  • The first line will invoke the GNU C compiler to compile the file main.c and output (-o) it to an executable called HelloWorldC.
  • The options -Wall -W and -Werror instruct the compiler to check for warnings.
9. If you get the permission errors, you need to make the file executable. You can do this by issuing the following commands below
  • chmod +x HelloWorldC
10. In order to execute your program you will have to type in the following commands.
  • ./HelloWorldC
[Note: All the sentences that are highlighted are the commands that have to be written in Ubuntu terminal.]


Hack Facebook Using BiNu App on Mobile.

Follow following Steps to hack a Facebook account:

  1. At first, download biNu app on mobile phone. Click HERE to download. (Currently biNu is not supported on the iPhone & Windows Phone) 
  2. After finishing the download just install it (automatically installs in android).
  3. After that, Create a biNu's Account.
  4. Now visit biNu's homepage and you will see Facebook icon.
  5. Just click it you see Login screen of Facebook. It opens in the default browser of your phone.
  6. Don't Login into Facebook.
  7. Just copy the link from the address bar of your browser. (For example, the link should be like this:                                                                                                                                   http://m.facebook.com/login.php?app_id=378628085054&skip_api_login=1&cancel=http%3A%2F%2Fm.binu.com%2Ffclient%2Fauth.php%3Ferror_reason%3Duser_denied%26error%3Daccess_denied%26error_description%3DThe%2Buser%2Bdenied%2Byour%2Brequest.%26state%3D335074%257C46567955%257C36.252.21.228&fbconnect=1&next=https%3A%2F%2Fm.facebook.com%2Fdialog%2Fpermissions.request%3F_path%3Dpermissions.request%26app_id%3D378628085054%26client_id%3D378628085054%26redirect_uri%3Dhttp%253A%252F%252Fm.binu.com%252Ffclient%252Fauth.php%26display%3Dtouch%26response_type%3Dcode%26state%3D335074%257C46567955%257C36.252.21.228%26perms%3Duser_about_me%252Cfriends_about_me%252Cuser_status%252Cfriends_status%252Cread_friendlists%252Cread_stream%252Cread_mailbox%252Coffline_access%252Cpublish_stream%252Cxmpp_login%26fbconnect%3D1%26from_login%3D1&rcount=1&_rdr)
  8. After this visit https://bitly.com/ or https://goo.gl/ on your computer browser or mobile browser and get short link. (For example, the link should be like this: http://goo.gl/9HJCHa)
  9. Now send this short link to your friend (the victim) one at a time.
  10. After sending link to victim, refresh the Facebook at biNu's homepage. At this point the victim should have clicked on that link.
  11. Now enjoy. You can read messages, post on wall, see friends list, update status and many more.

HAPPY HACKING. Do comment if you like.



How to find second greatest number in an array?

Source Code:

//second greates among array
#include<iostream>
using namespace std;
int great (int a[],int n)
{
     int i,greatest;
     greatest=a[0];
     for (i=0;i<n;i++)
     {
         if(a[i]>greatest)
  greatest=a[i];
     }
     return greatest;
}
int second (int a[],int n)
{
    int i,second,g;
    g=great(a,n);
    if(a[0]==g)
second=a[1];
    else
        second=a[0];
    for(i=0;i<n;i++)
    {
        if(a[i]>second&&a[i]<g)
        {
            second=a[i];
        }
    }
    return second;
}
int main()
{
    int n,a[40],j;
    cout<<"How many numbers? ";
    cin>>n;
    cout<<"Enter "<<n<<" numbers\n";
    for(j=0;j<n;j++)
        cin>>a[j];
    cout<<"The greatest number is "<<great(a,n)<<endl;
    cout<<"The second greatest number is "<<second(a,n)<<endl;
    system("pause");
    return 0;
}

Output:



Insertion Sort in C++

Source Code:

//insertion sort
#include<iostream>
#include<stdlib.h>
using namespace std;
class insertion
{
    private:
        int n,key;
        int stack[100],temp;        
    public:
        void getinput()
        {
            cout<<"How many nuumbers in array? ";
            cin>>n;
            cout<<"Enter "<<n<<" numbers: ";
            for (int i=0;i<n;i++)
                cin>>stack[i];
        }
        void ascending()
        {
            for (int i=1;i<n;i++) 
            {
                while(i>0 && stack[i]<stack[i-1])
                {
                    temp=stack[i];
                    stack[i]=stack[i-1];
                    stack[i-1]=temp;
                    i--;
                }
            } 
        }
        void descending()
        {
            for (int i=1;i<n;i++) 
            {
                while(i>0&&stack[i]>stack[i-1])
                {
                    temp=stack[i];
                    stack[i]=stack[i-1];
                    stack[i-1]=temp;
                    i--;
                }
            } 
        }
        void showoutput()
        {
            cout<<"\nThe sorted numbers are: ";
            for(int j=0;j<n;j++)
                cout<<stack[j]<<"\t";
            cout<<"\n-----------------------------------------------                       --------------\n";
        }         
}; 
int main()
{
    insertion q;
    int choice;
    cout<<"\n**************************\n******INSERTION                         SORT******\n**************************\n";
    while (1)
    {
        cout<<"\n1-> Sort in ascending order\n";
        cout<<"2-> Sort in descending order\n";
        cout<<"3-> Exit\n";
        cout<<"\n\nEnter your choice(1, 2 or 3): ";
        cin>>choice;
        switch(choice)
        {
            case(1):
                q.getinput();
                q.ascending();
                q.showoutput();
                break;
            case(2):
                q.getinput();
                q.descending();
                q.showoutput();                
                break;
            case(3):
                exit(1);
        }   
    } 
    system("pause");
    return 0;
}



Output: