Pages

Monday, March 31, 2014

C Program to display its own source code using __FILE__

#include <stdio.h>
int main()
{
    FILE *fp;
    char c;
    fp = fopen(__FILE__,"r");
    do {
         c = getc(fp);
         putchar(c);
    }
    while(c != EOF);
    fclose(fp);
    return 0;
}

C Program to display its own source code using __FILE__

#include <stdio.h>
int main()
{
    FILE *fp;
    char c;
    fp = fopen(__FILE__,"r");
    do {
         c = getc(fp);
         putchar(c);
    }
    while(c != EOF);
    fclose(fp);
    return 0;
}

C Program to check a number has perfect square or not

/** C Program to check Perfect Square **/
#include <stdio.h>
int main()
{
    int a, n;
    printf("Enter a number: ");
    scanf("%d", &n);
    for(a = 0; a <= n; a++)
    {
        if (n == a * a)
        {
            printf("YES");
            return 0;
        }
    }
    printf("NO");
    system("pause");
    return 0;
}

C Program to check a number has perfect square or not

/** C Program to check Perfect Square **/
#include <stdio.h>
int main()
{
    int a, n;
    printf("Enter a number: ");
    scanf("%d", &n);
    for(a = 0; a <= n; a++)
    {
        if (n == a * a)
        {
            printf("YES");
            return 0;
        }
    }
    printf("NO");
    system("pause");
    return 0;
}

Saturday, March 29, 2014

C Program to swap two numbers without using temp

/* to swap two numbers without using temp */
#include<stdio.h>
#include<stdlib.h>
int main()
{
    int a,b;
    printf("Enter first number: ");
    scanf("%d",&a);
    printf("Enter second number: ");
    scanf("%d",&b);
    a=a+b;
    b=a-b;
    a=a-b;
    printf("The first number: %d\n",a);
    printf("The second number: %d\n",b);
    system("pause");
    return 0;
}

C Program to swap two numbers without using temp

/* to swap two numbers without using temp */
#include<stdio.h>
#include<stdlib.h>
int main()
{
    int a,b;
    printf("Enter first number: ");
    scanf("%d",&a);
    printf("Enter second number: ");
    scanf("%d",&b);
    a=a+b;
    b=a-b;
    a=a-b;
    printf("The first number: %d\n",a);
    printf("The second number: %d\n",b);
    system("pause");
    return 0;
}

C Program to demonstrate infinite loop

/*example of infinite loop*/
#include <stdio.h>
#include<stdlib.h>
int main()
{
    int n=-5;
    while(n!=0)
    {
        printf("%d \t",n);
        n--;
    }
    system ("pause");
    return 0;
}

C Program to demonstrate infinite loop

/*example of infinite loop*/
#include <stdio.h>
#include<stdlib.h>
int main()
{
    int n=-5;
    while(n!=0)
    {
        printf("%d \t",n);
        n--;
    }
    system ("pause");
    return 0;
}

Dynamic memory allocation and array as function argument in Dev C++

/*array as function argument and Dyanamic allocation*/
#include<stdio.h>
#include<stdlib.h>
int max(int *a,int n);
int main()
{
    int *a,n,i;
    printf("how many number? ");
    scanf("%d",&n);
    a=(int*)malloc(n*sizeof(int));
    printf("enter %d number: ",n);
    for(i=0;i<n;i++)
        scanf("%d",a+i);
    printf("\n Largest number=%d",max(a,n));
    system ("pause");
    return 0;
}
int max(int *a,int n)
{
    int i,large;
    large=*a;
    for(i=1;i<n;i++)
    {
        if(large<*(a+i))
            large=*(a+i);
    }
    return large;
}

Dynamic memory allocation and array as function argument in Dev C++

/*array as function argument and Dyanamic allocation*/
#include<stdio.h>
#include<stdlib.h>
int max(int *a,int n);
int main()
{
    int *a,n,i;
    printf("how many number? ");
    scanf("%d",&n);
    a=(int*)malloc(n*sizeof(int));
    printf("enter %d number: ",n);
    for(i=0;i<n;i++)
        scanf("%d",a+i);
    printf("\n Largest number=%d",max(a,n));
    system ("pause");
    return 0;
}
int max(int *a,int n)
{
    int i,large;
    large=*a;
    for(i=1;i<n;i++)
    {
        if(large<*(a+i))
            large=*(a+i);
    }
    return large;
}

C Program to calculate the memory allocated by structure and union

/*memory allocate by the union and structure*/
#include <stdio.h>
#include<stdlib.h>
struct
{
      float f;
      int i;
      char c;
}s;
union
{
      float f;
      int i;
      char c;
}u;
int main()
{
    printf("memory allocate by structure=%d",sizeof(s));
    printf("\n memory allocate by union=%d",sizeof(u));
    system("pause");
    return 0;
}

C Program to calculate the memory allocated by structure and union

/*memory allocate by the union and structure*/
#include <stdio.h>
#include<stdlib.h>
struct
{
      float f;
      int i;
      char c;
}s;
union
{
      float f;
      int i;
      char c;
}u;
int main()
{
    printf("memory allocate by structure=%d",sizeof(s));
    printf("\n memory allocate by union=%d",sizeof(u));
    system("pause");
    return 0;
}

Thursday, March 27, 2014

C Program to demonstrate use of fread() to read data from a file

/*eg of fread*/
#include<stdio.h>
#include<stdlib.h>
struct student
{
    char name[25];
    int rollno;
    float fee;
};
int main( )
{
    struct student s;
    FILE *ptvar;
    ptvar=fopen("record.dat","rb");
    while(1)
    {
        if (fread(&s,sizeof(s),1,ptvar)==0)
            break;
        printf ("\nName: %s",s.name);
        printf ("\nRollno: %d",s.rollno);
        printf ("\nFee: %f",s.fee);
    }
    fclose(ptvar);
    system ("pause");
    return 0;
}

C Program to demonstrate use of fread() to read data from a file

/*eg of fread*/
#include<stdio.h>
#include<stdlib.h>
struct student
{
    char name[25];
    int rollno;
    float fee;
};
int main( )
{
    struct student s;
    FILE *ptvar;
    ptvar=fopen("record.dat","rb");
    while(1)
    {
        if (fread(&s,sizeof(s),1,ptvar)==0)
            break;
        printf ("\nName: %s",s.name);
        printf ("\nRollno: %d",s.rollno);
        printf ("\nFee: %f",s.fee);
    }
    fclose(ptvar);
    system ("pause");
    return 0;
}

C Program to demonstrate use of fwrite() to write into a file

/*eg of fwrite( )*/
#include<stdio.h>
#include<stdlib.h>
struct student
{
    char name[25];
    int rollno;
    float fee;
};
int main( )
{
    struct student s;
    FILE *ptvar;
    ptvar=fopen ("record.dat","wb");
    do
    {
        printf ("Enter Name: ");
        scanf ("%s",s.name);
        printf ("Enter rollno: ");
        scanf ("%d",&s.rollno);
        printf ("Enter fee: ");
        scanf ("%f",&s.fee);
        fwrite (&s, sizeof(s),1,ptvar);
        printf ("\nAny more records?(y/n): ");
        fflush(stdin);
    }while (getchar( )=='y');
    fclose (ptvar);
    system ("pause");
    return 0;
}

C Program to demonstrate use of fwrite() to write into a file

/*eg of fwrite( )*/
#include<stdio.h>
#include<stdlib.h>
struct student
{
    char name[25];
    int rollno;
    float fee;
};
int main( )
{
    struct student s;
    FILE *ptvar;
    ptvar=fopen ("record.dat","wb");
    do
    {
        printf ("Enter Name: ");
        scanf ("%s",s.name);
        printf ("Enter rollno: ");
        scanf ("%d",&s.rollno);
        printf ("Enter fee: ");
        scanf ("%f",&s.fee);
        fwrite (&s, sizeof(s),1,ptvar);
        printf ("\nAny more records?(y/n): ");
        fflush(stdin);
    }while (getchar( )=='y');
    fclose (ptvar);
    system ("pause");
    return 0;
}

C Program to demonstrate example of Dynamic Memory Allocation

/*array as function argument and dynamic memory allocation*/
#include<stdio.h>
#include<stdlib.h>
int max(int*a,int n);
int main()
{
    int *a,n,i;
    printf("How many numbers? ");
    scanf("%d",&n);
    a=(int*)malloc(n*sizeof(int));
    printf("Enter %d numbers: ",n);
    for(i=0;i<n;i++)
        scanf("%d",a+i);
    printf("\n lagrest number=%d", max(a,n));
    system ("pause");
    return 0;
}
int max(int*a,int n)
{
    int i,large;
    large=*a;
    for(i=1;i<n;i++)
    {
        if(large<*(a+i))
            large=*(a+i);
    }
    return large;
}

C Program to demonstrate example of Dynamic Memory Allocation

/*array as function argument and dynamic memory allocation*/
#include<stdio.h>
#include<stdlib.h>
int max(int*a,int n);
int main()
{
    int *a,n,i;
    printf("How many numbers? ");
    scanf("%d",&n);
    a=(int*)malloc(n*sizeof(int));
    printf("Enter %d numbers: ",n);
    for(i=0;i<n;i++)
        scanf("%d",a+i);
    printf("\n lagrest number=%d", max(a,n));
    system ("pause");
    return 0;
}
int max(int*a,int n)
{
    int i,large;
    large=*a;
    for(i=1;i<n;i++)
    {
        if(large<*(a+i))
            large=*(a+i);
    }
    return large;
}

C Program to convert Celcius to Fahernheit

/*to convert celcius into fahernheit*/
#include <stdio.h>
#include<stdlib.h>
int main()
{
     int fahr, celsius;
     int lower, upper, step;
     lower = 0;    
     upper = 300;   
     step = 20;     
     fahr = lower;
     while (fahr <= upper)
    {
         celsius = 5 * (fahr-32) / 9;
         printf("%d\t%d\n", fahr, celsius);
         fahr= fahr + step;
    }
    system("pause");
    return 0;
}

C Program to convert Celcius to Fahernheit

/*to convert celcius into fahernheit*/
#include <stdio.h>
#include<stdlib.h>
int main()
{
     int fahr, celsius;
     int lower, upper, step;
     lower = 0;    
     upper = 300;   
     step = 20;     
     fahr = lower;
     while (fahr <= upper)
    {
         celsius = 5 * (fahr-32) / 9;
         printf("%d\t%d\n", fahr, celsius);
         fahr= fahr + step;
    }
    system("pause");
    return 0;
}

C Program to demonstrate use of nested loop

/*example of nested loop*/
#include <stdio.h>
#include<stdlib.h>
int main()
{
    int i,j;
    for (i=1;i<=5;i++)
    {
        printf("\n");
        for (j=1;j<=5;j++)
            printf("%d \t",i*j);
    }
    system("pause");
    return 0;
}

C Program to demonstrate use of nested loop

/*example of nested loop*/
#include <stdio.h>
#include<stdlib.h>
int main()
{
    int i,j;
    for (i=1;i<=5;i++)
    {
        printf("\n");
        for (j=1;j<=5;j++)
            printf("%d \t",i*j);
    }
    system("pause");
    return 0;
}

C Program for passing value to a function by address

/*example of pass by address*/
#include<stdio.h>
#include<stdlib.h>
void swap (int *,int *);
int main()
{
    int a=5,b=9;
    printf("before swapping a=%d and b=%d",a,b);
    swap(&a,&b);
    printf("\n after swapping a=%d and b=%d",a,b);
    system ("pause");
    return 0;
}
void swap (int *x,int *y)
{
     int temp;
     temp=*x;
     *x=*y;
     *y=temp;
}

C Program to demonstrate use of nested structure

/*example of nested structure*/
#include<stdio.h>
#include<stdlib.h>
struct date
{
       int day,month,year;
};
struct student
{
       char name[25];
       struct date dob;
}s;
int main()
{
    printf("enter name of student: ");
    scanf("%s",s.name);
    printf("enter date of birth: ");
    scanf("%d%d%d",&s.dob.day,&s.dob.month,&s.dob.year);
    printf("\n date of dirth of student %s is %d/%d/%d",s.name,s.dob.day,s.dob.month,s.dob.year);
    system("pause");
    return 0;
}

C Program to demonstrate use of nested structure

/*example of nested structure*/
#include<stdio.h>
#include<stdlib.h>
struct date
{
       int day,month,year;
};
struct student
{
       char name[25];
       struct date dob;
}s;
int main()
{
    printf("enter name of student: ");
    scanf("%s",s.name);
    printf("enter date of birth: ");
    scanf("%d%d%d",&s.dob.day,&s.dob.month,&s.dob.year);
    printf("\n date of dirth of student %s is %d/%d/%d",s.name,s.dob.day,s.dob.month,s.dob.year);
    system("pause");
    return 0;
}

C Program for passing value to a function by address

/*example of pass by address*/
#include<stdio.h>
#include<stdlib.h>
void swap (int *,int *);
int main()
{
    int a=5,b=9;
    printf("before swapping a=%d and b=%d",a,b);
    swap(&a,&b);
    printf("\n after swapping a=%d and b=%d",a,b);
    system ("pause");
    return 0;
}
void swap (int *x,int *y)
{
     int temp;
     temp=*x;
     *x=*y;
     *y=temp;
}

C Program to demonstrate use of pow() function to find power

/*to find power*/
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int main()
{
    float x,y, pw;
    printf("enter base: ");
    scanf("%f",&x);
    printf("enter index: ");
    scanf("%f",&y);
    pw=pow(x,y);
    printf("%f to the power %f is %f",x,y,pw);
    system ("pause");
    return 0;
}

C Program to demonstrate use of pow() function to find power

/*to find power*/
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int main()
{
    float x,y, pw;
    printf("enter base: ");
    scanf("%f",&x);
    printf("enter index: ");
    scanf("%f",&y);
    pw=pow(x,y);
    printf("%f to the power %f is %f",x,y,pw);
    system ("pause");
    return 0;
}

C Program to find factorial of a number using recursion

/*to find factorial using recursion*/
#include <stdio.h>
#include <stdlib.h>
long factorial (int);
int main()
{
    int n;
    printf("enter the number: ");
    scanf("%d",&n);
    printf("factorial of %d is %ld", n, factorial(n));
    system ("pause");
    return 0;
}
long factorial (int n)
{
    if (n<=1)
        return 1;
    else
        return (n*factorial(n-1));
}

C Program to find factorial of a number using recursion

/*to find factorial using recursion*/
#include <stdio.h>
#include <stdlib.h>
long factorial (int);
int main()
{
    int n;
    printf("enter the number: ");
    scanf("%d",&n);
    printf("factorial of %d is %ld", n, factorial(n));
    system ("pause");
    return 0;
}
long factorial (int n)
{
    if (n<=1)
        return 1;
    else
        return (n*factorial(n-1));
}

Wednesday, March 26, 2014

C Program to demonstrate use of sizeof() operator

/* use of size of operator*/
#include<stdio.h>
#include<stdlib.h>
int main()
{
    char c;
    int i;
    float f;
    double d;
    printf("memory allocate by character=%u",sizeof(c));
    printf("\n memory allocate by integer=%u",sizeof(i));
    printf("\n memory allocate by float=%u",sizeof(f));
    printf("\n memory allocate by double=%u",sizeof(d));
    system ("pause");
    return 0;
}

C Program to demonstrate use of sizeof() operator

/* use of size of operator*/
#include<stdio.h>
#include<stdlib.h>
int main()
{
    char c;
    int i;
    float f;
    double d;
    printf("memory allocate by character=%u",sizeof(c));
    printf("\n memory allocate by integer=%u",sizeof(i));
    printf("\n memory allocate by float=%u",sizeof(f));
    printf("\n memory allocate by double=%u",sizeof(d));
    system ("pause");
    return 0;
}

C program to sort numbers in an array using quick sort

/*to sort the number in ascending order*/
#include <stdio.h>
#include <stdlib.h>
int main()
{
    int n,i,j,a[25],temp;
    printf("How many numbers?");
    scanf("%d",&n);
    printf("enter %d number",n);
    for(i=0;i<n;i++)
        scanf("%d",&a[i]);
    for(i=0;i<n;i++)
    {
        for (j=i+1;j<n;j++)
        {
            if (a[i]>a[j])
            {
                temp=a[i];
                a[i]=a[j];
                a[j]=temp;
            }
        }
    }
    printf("\n Sorted Number are: ");
    for(i=0;i<n;i++)
        printf("%d \t",a[i]);
    system("pause");
    return 0;
}

C program to sort numbers in an array using quick sort

/*to sort the number in ascending order*/
#include <stdio.h>
#include <stdlib.h>
int main()
{
    int n,i,j,a[25],temp;
    printf("How many numbers?");
    scanf("%d",&n);
    printf("enter %d number",n);
    for(i=0;i<n;i++)
        scanf("%d",&a[i]);
    for(i=0;i<n;i++)
    {
        for (j=i+1;j<n;j++)
        {
            if (a[i]>a[j])
            {
                temp=a[i];
                a[i]=a[j];
                a[j]=temp;
            }
        }
    }
    printf("\n Sorted Number are: ");
    for(i=0;i<n;i++)
        printf("%d \t",a[i]);
    system("pause");
    return 0;
}

C Program to demonstrate the use of strlen() function to find length of string

/*to use the strlen()*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>>
int main()
{
    char str[80];
    int l;
    printf("enter the string: ");
    gets(str);
    l=strlen(str);
    printf("length of the string=%d",l);
    system ("pause");
    return 0;
}

C Program to demonstrate the use of strlen() function to find length of string

/*to use the strlen()*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>>
int main()
{
    char str[80];
    int l;
    printf("enter the string: ");
    gets(str);
    l=strlen(str);
    printf("length of the string=%d",l);
    system ("pause");
    return 0;
}

C Program to convert string to lowercase by the use of strlwr() function

/*to use the strlwr()*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>>
int main()
{
    char str[80];
    printf("enter the string: ");
    gets(str);
    printf("string in lowercase: ");
    puts(strlwr(str));
    system ("pause");
    return 0;
}

C Program to convert string to lowercase by the use of strlwr() function

/*to use the strlwr()*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>>
int main()
{
    char str[80];
    printf("enter the string: ");
    gets(str);
    printf("string in lowercase: ");
    puts(strlwr(str));
    system ("pause");
    return 0;
}

C Program to convert string to uppercase by the use of strupr() function

/*to use the strupr()*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>>
int main()
{
    char str[80];
    printf("enter the string: ");
    gets(str);
    printf("string in uppercase: ");
    puts(strupr(str));
    system ("pause");
    return 0;
}

C Program to convert string to uppercase by the use of strupr() function

/*to use the strupr()*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>>
int main()
{
    char str[80];
    printf("enter the string: ");
    gets(str);
    printf("string in uppercase: ");
    puts(strupr(str));
    system ("pause");
    return 0;
}

C Program to display current date in Dev C++

/*to display current date*/
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int main()
{
    char date[9];
    _strdate(date);
    printf("Date: %s \n", date);
    system ("pause");
    return 0;
}

C Program to display current date in Dev C++

/*to display current date*/
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int main()
{
    char date[9];
    _strdate(date);
    printf("Date: %s \n", date);
    system ("pause");
    return 0;
}

C Program to demonstrate If Else Statement

/*example of if else statement*/
#include <stdio.h>
#include <stdlib.h>
int main()
{
    int num;
    printf("enter number;");
    scanf("%d", &num);
    if(num>0)
             printf("%d is positive", num);
    else
             printf("%d is negative or zero",num);
    system("pause");
    return 0;
}

C Program to demonstrate If Else Statement

/*example of if else statement*/
#include <stdio.h>
#include <stdlib.h>
int main()
{
    int num;
    printf("enter number;");
    scanf("%d", &num);
    if(num>0)
             printf("%d is positive", num);
    else
             printf("%d is negative or zero",num);
    system("pause");
    return 0;
}

C Program to demonstrate Use of DoWhile Loop

/*use of do while loop*/
#include <stdio.h>
#include<stdlib.h>
int main()
{
    int mark;
    do
    {
        printf("\nEnter the number: ");
        scanf("%d",&mark);
        if(mark>100||mark<0)
            printf("Error:mark should be between 0 and 100: ");
    }while (mark>100||mark<0);
    system ("pause");
    return 0;
}

C Program to demonstrate Use of DoWhile Loop

/*use of do while loop*/
#include <stdio.h>
#include<stdlib.h>
int main()
{
    int mark;
    do
    {
        printf("\nEnter the number: ");
        scanf("%d",&mark);
        if(mark>100||mark<0)
            printf("Error:mark should be between 0 and 100: ");
    }while (mark>100||mark<0);
    system ("pause");
    return 0;
}

C Program to Find Octal and Hexadecimal Equivalent of a Decimal Number

/*to read in decimal& print in octal & hexadecimal*/
#include<stdio.h>
#include<stdlib.h>
int main()
{
    int num;
    printf("enter numbrer: ");
    scanf("%d",&num);
    printf("\n octal equivalent=%o",num);
    printf("\n hexadecimal equivalent=%x",num);
    system("pause");
    return 0;
}

C Program to Find Octal and Hexadecimal Equivalent of a Decimal Number

/*to read in decimal& print in octal & hexadecimal*/
#include<stdio.h>
#include<stdlib.h>
int main()
{
    int num;
    printf("enter numbrer: ");
    scanf("%d",&num);
    printf("\n octal equivalent=%o",num);
    printf("\n hexadecimal equivalent=%x",num);
    system("pause");
    return 0;
}

C Program to add two complex numbers

/*example of passing structure to function*/
#include<stdio.h>
#include<stdlib.h>
struct complex
{
       float real, image;
};
complex add(complex,complex);
int main()
{
    struct complex c1,c2,c3;
    printf("enter first complex number: ");
    scanf("%f%f",&c1.real,&c1.image);
    printf("enter second complex number: ");
    scanf("%f%f",&c2.real,&c2.image);
    c3=add(c1,c2);
    printf("\n sum of two complex number:%f+i%f",c3.real,c3.image);
    system ("pause");
    return 0;
}
complex add (complex c1,complex c2)
{
        struct complex c3;
        c3.real=c1.real+c2.real;
        c3.image=c1.image+c2.image;
        return c3;
}

C Program to add two complex numbers

/*example of passing structure to function*/
#include<stdio.h>
#include<stdlib.h>
struct complex
{
       float real, image;
};
complex add(complex,complex);
int main()
{
    struct complex c1,c2,c3;
    printf("enter first complex number: ");
    scanf("%f%f",&c1.real,&c1.image);
    printf("enter second complex number: ");
    scanf("%f%f",&c2.real,&c2.image);
    c3=add(c1,c2);
    printf("\n sum of two complex number:%f+i%f",c3.real,c3.image);
    system ("pause");
    return 0;
}
complex add (complex c1,complex c2)
{
        struct complex c3;
        c3.real=c1.real+c2.real;
        c3.image=c1.image+c2.image;
        return c3;
}

C Program To count age of employee above the age of 50

/* to count number of employee of age above 50*/
#include <stdio.h>
#include <stdlib.h>
int main()
{
    int i, n,age[25],count=0;
    printf("How many employee?");
    scanf("%d",&n);
    printf("enter age of %d employee:",n);
    for (i=0;i<n;i++)
        scanf("%d",&age[i]);
    for(i=0;i<n;i++)
    {
        if(age[i]>50)
            count++;
    }
    printf("\n no. of employee of age above 50=%d",count);
    system("pause");
    return 0;
}

C Program To count age of employee above the age of 50

/* to count number of employee of age above 50*/
#include <stdio.h>
#include <stdlib.h>
int main()
{
    int i, n,age[25],count=0;
    printf("How many employee?");
    scanf("%d",&n);
    printf("enter age of %d employee:",n);
    for (i=0;i<n;i++)
        scanf("%d",&age[i]);
    for(i=0;i<n;i++)
    {
        if(age[i]>50)
            count++;
    }
    printf("\n no. of employee of age above 50=%d",count);
    system("pause");
    return 0;
}

Tuesday, March 25, 2014

C++ Program to generate random numbers of certain maximum value

//to genetare random numbers
#include<iostream>
#include<cstdlib>
using namespace std;
int main()
{
   int n, max, num, c;
   cout << "Enter the number of random numbers you want ";
   cin >> n;
   cout << "Enter the maximum value of random number ";
   cin >> max;  
   cout << "random numbers from 0 to " << max << " are :-" << endl;
   for ( c = 1 ; c <= n ; c++ )
   {
      num = rand( );
      num%=max;
      cout << num << endl;
   }
   system("pause");        
   return 0;
}

C++ Program to generate random numbers of certain maximum value

//to genetare random numbers
#include<iostream>
#include<cstdlib>
using namespace std;
int main()
{
   int n, max, num, c;
   cout << "Enter the number of random numbers you want ";
   cin >> n;
   cout << "Enter the maximum value of random number ";
   cin >> max;  
   cout << "random numbers from 0 to " << max << " are :-" << endl;
   for ( c = 1 ; c <= n ; c++ )
   {
      num = rand( );
      num%=max;
      cout << num << endl;
   }
   system("pause");        
   return 0;
}

Monday, March 24, 2014

Add two complex numbers using friend function in C++

//adding two complex number using friend function
#include<iostream>
#include<cmath>
using namespace std;
class complex
{
    private:
        float real,imag;
        friend complex add (complex c1, complex c2);
    public:
        void getdata()
        {
            cout<<"enter the real part: ";
            cin>>real;
            cout<<"enter the imag part: ";
            cin>>imag;
        }
        void showdata()
        {
            cout<<real<<"+"<<imag"i"<<endl;
        }
};
        complex add(complex c1,complex c2)
        {
            complex c3;
            c3.real=c1.real+c2.real;
            c3.imag=c1.imag+c2.imag;
            return c3;
        }
    int main()
    {
        complex c1,c2,c3;
        cout<<"enter first complex number: "<<endl;
        c1.getdata();
        cout<<"enter second complex number: "<<endl;
        c2.getdata();
        c3=add(c1,c2);
        cout<<"sum of two complex number: ";
        c3.showdata();       
        system("pause");
        return 0;
    }

Add two complex numbers using friend function in C++

//adding two complex number using friend function
#include<iostream>
#include<cmath>
using namespace std;
class complex
{
    private:
        float real,imag;
        friend complex add (complex c1, complex c2);
    public:
        void getdata()
        {
            cout<<"enter the real part: ";
            cin>>real;
            cout<<"enter the imag part: ";
            cin>>imag;
        }
        void showdata()
        {
            cout<<real<<"+"<<imag"i"<<endl;
        }
};
        complex add(complex c1,complex c2)
        {
            complex c3;
            c3.real=c1.real+c2.real;
            c3.imag=c1.imag+c2.imag;
            return c3;
        }
    int main()
    {
        complex c1,c2,c3;
        cout<<"enter first complex number: "<<endl;
        c1.getdata();
        cout<<"enter second complex number: "<<endl;
        c2.getdata();
        c3=add(c1,c2);
        cout<<"sum of two complex number: ";
        c3.showdata();       
        system("pause");
        return 0;
    }

Increment Decrement Operators

//increment/decrement operators
#include <stdio.h>
#include <stdlib.h>
int main()
{
    int x=5;
    x++;
    printf("\n x=%d",x);
    printf("\n x=%d",x++);
    printf ("\n x=%d",x);
    ++x;
    printf("\n x=%d",x);
    printf("\n x=%d",x);
    printf("\n x=%d",x);
    system("pause");
    return 0;
}
   

Increment Decrement Operators

//increment/decrement operators
#include <stdio.h>
#include <stdlib.h>
int main()
{
    int x=5;
    x++;
    printf("\n x=%d",x);
    printf("\n x=%d",x++);
    printf ("\n x=%d",x);
    ++x;
    printf("\n x=%d",x);
    printf("\n x=%d",x);
    printf("\n x=%d",x);
    system("pause");
    return 0;
}