Monday, 6 January 2014

A PROGRAMM TO PRINT NON REPETED NUMBER IN C

#include<stdio.h>
#include<conio.h>
int main()
{
    int i,j,count;
    int a[]={12,15,11,10,16,12,17,19,18,10};
    printf("Non Repeted number are: ");
    for(i=0;i<10;i++)
    {
                     count=0;
                     for(j=0;j<10;j++)
                     {
                                      if(a[i]==a[j])
                                      {
                                                    count++;
                                      }
                     }
                     if(count==1)
                     {
                                 printf("%d,",a[i]);
                     }
    }
    getch();
    return 0;
}

A PROGRAMM TO PRINT "HISTOGRAM" IN C

#include<stdio.h>
#include<conio.h>
int main()
{
    int i,j;
    int a[10];
    for(i=0;i<10;i++)
    {
                     printf("Enter array %d element between 1-30:",i+1);
                     scanf("%d",&a[i]);
    }
    printf("Histogram\n");
    for(i=0;i<10;i++)
    {
                     printf("%d",a[i]);
                     for(j=0;j<a[i];j++)
                     {
                                        printf("*");
                                     
                     }
                     printf("\n");
    }
    getch();
    return 0;
}

A FUNCTION TO TAKE POWER OF GIVEN NUMBER (BASE AND EXPONENT WILL BE ENTERED BY THE USER).....!!!!!

#include<stdio.h>
int power(int *,int *);
int main()
{
    int base=0,expo=0,pow=0;
    printf("Enter the base number:");
    scanf("%d",&base);
    printf("Enter the exponent:");
    scanf("%d",&expo);
    pow=power(&base,&expo);
    printf("Power of given base is %d",pow);
    getch();
    return 0;
}

int power(int *base,int *expo)
{
    int x,size;
    size=*base;
    for(x=1;x<*expo;x++)
    {
      *base=*base * size;
    }
    return *base;
}

A PROGRAM TO SEARCH A NUMBER FROM ARRAY AND TO SHOW HOW MANY TIMES IT APPEARED....!!!!

#include<stdio.h>
# define size 15
void search(int arr[],int key );

int main()
{
    int array[15]={0};
    int x=0,key;
    printf("Enter the elements of array;");
    for(x=0;x<15;x++)
    {
       scanf("%d",&array[x]);
    }
    printf("Enter the number to be searched:");
    scanf("%d",&key);
   
    search(array,key);             
getch();
return 0;
}
void search(int arr[],int key)
{
     int x=0,count=0;
     for(x=0;x<size;x++)
     {
        if(key==arr[x])
        {
           count++;
        }
     }
     if (count>0)
     printf("The number was found %d times",count);
     else
     printf("The number was not found");
}

PROGRAM TO PRINT HOLLOW DAIMOND....!!!!!

#include<stdio.h>
int main()
{
    int x,y,z;
    int size=10;
    for(x=0;x<10;x++)
    {
       for(z=0;z<size;z++)
       printf(" ");
       size--;
       for(y=0;y<=2*x;y++)
       {
           if(x>0&&x<=9&&y>0&&y<2*x)
           printf(" ");
           else
           printf("*");
       }
       printf("\n");
    }
    size++;
    //for lower half of empty daimond;
      for(x=8;x>=0;x--)
    {
       size++;
       for(z=0;z<size;z++)
       printf(" ");
      
       for(y=0;y<=2*x;y++)
       {
           if(x<=8&&x>0&&y>0&&y<2*x)
           printf(" ");
           else
           printf("*");
       }
       printf("\n");
    }
   
    getch();
    return 0;
}

EXPECTED OUTPUT:

 

HOW TO PRINT A DIMOND IN C

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

   int a,b,c,i,j,k,n;
        printf("Enter the (half)size of dimond:\n");
   scanf("%d",&n);

//for the first half of diamond

   for(i=n;i>0;i--)
        {
             for(j=1;j<i;j++)
             {
            printf(" ");
             }
                 for(k=j-1;k<n;k++)
                 {
                    printf("*");
                    printf(" ");
                 }
          printf("\n");
        }

//for the second half of diamond

   for(a=n-1;a>=0;a--)
        {
           for(b=n;b>a;b--)
          {
          printf(" ");
            }
            for(c=0;c<a;c++)
             {
               printf("*");
               printf(" ");
           }
                printf("\n");
        }
       return 0;
}


Sunday, 5 January 2014

TAKE 2 ARRAYS (2D) AND PRINT THERE SUM IN THIRD ONE

#include<stdio.h>
int main()
{
int i,j,k,r,c;
printf("Enter the number of rows of arrys:");
scanf("%d",&r);
printf("Enter the number of colums of arrys:");
scanf("%d",&c);
int a[r][c];
int b[r][c];
printf("\nEnter values of first 2d arrays:\n");
for(i=0;i<r;i++)
{
   for(j=0;j<c;j++)
  {
printf("Enter values of array element for %d row and %d coulm:",i,j);
  scanf("%d",&a[i][j]);
  }
}
printf("\nEnter values for 2nd arry element\n");
for(i=0;i<r;i++)
{
   for(j=0;j<c;j++)
    {
  printf("Enter values of array element for %d row and %d coulm:",i,j);
    scanf("%d",&b[i][j]);
  }
}
printf("\n");
printf("First aaray elements are\n");
for(i=0;i<r;i++)
{
    for(j=0;j<c;j++)
   {
  printf("%3d",a[i][j]);
   }
  printf("\n");
}
printf("Second aaray elements are\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
   {
  printf("%3d",b[i][j]);
   }
printf("\n");
}
printf("\n\n\n");
printf("sum of arry element\n");
for(i=0;i<r;i++)
{
    for(j=0;j<c;j++)
   {
   printf("%3d",a[i][j]+b[i][j]);
   }
   printf("\n");
}
getch();
return 0;

}

Expected output of :


Enter the number of rows of arrys:2
Enter the number of colums of arrys:3

Enter values of first 2d arrays:
Enter values of array element for 0 row and 0 coulm:1
Enter values of array element for 0 row and 1 coulm:2
Enter values of array element for 0 row and 2 coulm:3
Enter values of array element for 1 row and 0 coulm:3
Enter values of array element for 1 row and 1 coulm:2
Enter values of array element for 1 row and 2 coulm:1

Enter values for 2nd arry element
Enter values of array element for 0 row and 0 coulm:1
Enter values of array element for 0 row and 1 coulm:2
Enter values of array element for 0 row and 2 coulm:3
Enter values of array element for 1 row and 0 coulm:3
Enter values of array element for 1 row and 1 coulm:2
Enter values of array element for 1 row and 2 coulm:1

First aaray elements are
  1  2  3
  3  2  1
Second aaray elements are
  1  2  3
  3  2  1



sum of arry element
  2  4  6
  6  4  2


TO FIND THE AVERAGE,BELOW AVERAGE AND ABOVE AVERAGE MARKS OF ENTIRE CLASS USING ARRAY.....!!!!

#include<stdio.h>
int main()
{
  int marks[10]={0};
    int x,sum=0,count1=0,count2=0;
    float average=0;
    printf("Enter the Marks:");
  
    for(x=0;x<10;x++)
    {
        scanf("%d",&marks[x]);
        sum=sum+marks[x];
    }
    average=(float)sum/10;
    for(x=0;x<10;x++)
    if (marks[x]>=average)
    count1++;
    else
    count2++;
    printf("Average marks of entire class is:%.2f\n",average);
    printf("Number of students obtaining average or more marks are:%d\n",count1);
    printf("Number of students obtaining below average marks are:%d",count2);
    getch();
    return 0;
}
                

TO FIND HOW MANY TIMES AND AT WHAT INDEX THE GIVEN NUMBER OCCURED IN A ARRAY....!!!!

#include<stdio.h>
int main()
{
  int array[25]={1,2,4,3,1,6,8,9,5,3,1,4,6,9,7,3,1,5,7,8,0,3,2,5,0};
    int x,count=0,index[]={0},search;
    printf("Enter the number to be searched from above array:");
    scanf("%d",&search);
    for(x=0;x<25;x++)
    {
      
       if(array[x]==search)
       {
       count++;
       printf("Given number is found at the index:%d\n",x);
       }
    }
    if (count==0)
    printf("The Given number was not found");
    else
    printf("The Given number was found %d times\n",count);
    getch();
    return 0;
}
                

TO FIND THE 2ND MINIMUM NUMBER FROM ARRAY WITHOUT SORTING...!!!!

#include<stdio.h>
int main()
{
    int arr[10],count[10]={0};
    int x,y;
    for(x=0;x<10;x++)
    scanf("%d",&arr[x]);
    int max=0;
    for(y=0;y<10;y++)
    {
       for(x=0;x<10;x++)
       {
       if(arr[y]>arr[x])
       count[y]++;
       }
    }
     for(x=0;x<10;x++)
     {
      if (count[x]==1)
      printf("2nd Minimum number is:%d",arr[x]);
     }
    getch();
    return 0;
}

TO FIND THE 2ND MAXIMUM NUMBER FROM ARRAY WITHOUT SORTING....!!!!

#include<stdio.h>
int main()
{
    int arr[10],count[10]={0};
    int x,y;
   printf("Enter the elements of array:");
    printf("Enter the elements of array:");
    for(x=0;x<10;x++)
    scanf("%d",&arr[x]);
    int max=0;
    for(y=0;y<10;y++)
    {
       for(x=0;x<10;x++)
       {
       if(arr[y]<arr[x])
       count[y]++;
       }
    }
     for(x=0;x<10;x++)
     {
      if (count[x]==1)
      printf("2nd Maximum number is:%d",arr[x]);
     }
    getch();
    return 0;
}

Saturday, 4 January 2014

A PROGRAMM TO ROLL A DICE 1000 TIMES AND THEN CHECK HOW MANY TIMES EACH FACE OCCUR

#include<stdio.h>
#include<math.h>
int main()
{
int count,i,x;
int a[1000];
for(i=0;i<1000;i++)
{
a[i]= 1+rand()%6;
}
for(x=1;x<=6;x++)
{
count=0;
for(i=0;i<1000;i++)
{
if(x==a[i])
{
count++;
}
}
printf("Face %d occur %d times\n",x,count);
}
getch();
return 0;
}


Expected output :
Face 1 occur 156 times
Face 2 occur 162 times
Face 3 occur 180 times
Face 4 occur 168 times
Face 5 occur 157 times
Face 6 occur 177 times

Friday, 3 January 2014

SIMPLE OUTPUT PROGRAM (To get the concept about working of a pointer)

#include<stdio.h>
int main ()
{
int x,y,*z;
x=5;
y=10;
z=&x;
printf("Value of x is :%d\n",x);
printf("Address  of x is:%d\n",&x);
printf("Value of y is:%d\n",y);
printf("Address  of y is:%d\n",&y);
printf("Value of *z is:%d\n",*z);
printf("Address  of x is:%d\n",z);
getch();
return 0;
}

HOW TO CHECK THE SIZE OF DATA TYPE..!!!(In Byte)

#include<stdio.h>
int main()
{
printf("%d\n",sizeof(int));
printf("%d\n",sizeof(float));
printf("%d\n",sizeof(double));
printf("%d",sizeof(long double));
    return 0;
}

EXPECTED OUTPUT: (Might depend on your operating system)


4
4
8
12


IN C PRINTF FUNCTION FOLLOWS CDECL PARAMETER PASSING SCHEME

#include<stdio.h>

int main(){
 int a=10;
 printf("%d\t %d\t %d\t",a,a++,++a);
 return 0;
}

EXPECTED OUTPUT:

12     11    12

Thursday, 2 January 2014

A PROGRAM TO CHECK WHETHER A NUMBER IS PRIME OR NOT( Different Logic From Previous Code)

#include<stdio.h>
int main()
{
int num,i,j;
printf("Enter number to check prime:");
scanf("%d",&num);
for(i=1;i<num;i++)
{
int count=0;
for(j=1;j<i;j++)
{
if(i%j==0)
{
count++;
}
}
if(count==0||count==1)
{
printf("%d is prime number\n",i);
}
else
{
printf("%d is not prime number\n",i);
}
}
getch();
return 0;
}

A PROGRAM WITH FOR LOOP

An easy program for you...!!! Don't get panicked by difficult one's try this...!!!
Because simple programming always leads to complex one...!!!! Cheer-Up...!!! 


#include <stdio.h>
int main()
{
 int i=0;
 for(;i<=2;)
 printf(" %d\t%d\t%d\n",++i,i,i++);
return 0;
}

EXPECTED OUTPUT:

 2    2     0
 4    4     2
(Test your knowledge)

REMEMBER:  OUTPUT MIGHT VARY DEPENDING UPON YOUR COMPILER...!!! SO DON'T PANIC JUST GET THE BASIC CONCEPT OF i++ and ++i...!!! :)

Wednesday, 1 January 2014

BUBBLE SORT BY USING POINTERS AND FUNCTIONS...!!!

#include<stdio.h>
void swap(int *m, int *n)
{
int temp;
temp= *m;
*m=*n;
*n=temp;
}
int main()
{
int size,i,j;
printf("Enter size of array:");
scanf("%d",&size);
int a[size];
for(i=0;i<size;i++)
{
printf("Enter values of %d array element:",i);
scanf("%d",&a[i]);
}
for(i=0;i<size;i++)
{
for(j=i+1;j<size;j++)
{
if(a[i]>a[j])
{
swap(&a[i],&a[j]);
}
}
}
for(i=0;i<size;i++)
{
printf("%d\t",a[i]);
}
getch();
return 0;
}

PROGRAM TO FIND MODULE OF ARRAY...!!! (Module: A number which occurs most)

#include<stdio.h>
int main()
{
int array[10]={0};
int x,y,count=0,array1[10]={0},max=0,index;
printf("Enter the elements of array:");
for(x=0;x<10;x++)
{
    scanf("%d",&array[x]);
}
for(x=0;x<10;x++)
{
    count=0;
    for(y=0;y<10;y++)
    {
        if(array[x]==array[y])
        {
            array1[x]++;
        }
    }
}
    for(x=0;x<10;x++)
    {
        if(max<array1[x])
        {
        max=array1[x];
        index=x;
        }
    }
    for(y=0;y<10;y++)
    {
        if (array1[index]==array1[y] && array[index]!=array[y])
        {
            count++;
        }
    }
        if(count>0)
        {
        printf("There is no mode of this array.\n");
        printf("Because more than 1 element occurs most in array ");
        }
        else
        {
        printf("\n%d is mode of array and it is found at %d places",array[index],array1[index]);
        }
getch();
return 0;
}


EXPECTED OUTPUTS:

Note: You can change the elements of array and get results according to your choice...!!!




PROGRAM TO FIND SUM,AVERAGE,MAXIMUM AND MINIMUM NUMBER OF ARRAY....!!!!

#include<stdio.h>
int max (int [][5]);
int min (int [][5]);
int main()
{
int c[2][5];
int i,j;
float sum=0;
printf("Please enter elements of 2-D array of 2 rows and 5 columns:");
    for(i=0;i<2;i++)
    {

            for(j=0;j<5;j++)
            {
                scanf("%d",&c[i][j]);
                sum=sum+c[i][j];
            }
    }
    printf("Your elemnts are\n\n");
    for(i=0;i<2;i++)
    {
            for(j=0;j<5;j++)
            {
                printf("%8d\n",c[i][j]);
            }
    }

printf("\nSum of your array is : %.2f \n",sum);
printf("Maximum Number of your array is : %d \n",max (c));
printf("Minimum Number of your array is : %d \n",min (c));
printf("Average Number of your array is : %.2f \n",sum/10);
getch();
return 0;
}


int max (int c[][5])
{
    int i,j;
    int maxi=c[0][0];
    for (i=0;i<2;i++)
    {
       for(j=0;j<5;j++)
       {
        if (maxi<c[i][j])
            maxi =c[i][j];
       }
    }

    return maxi;
}
int min (int c[][5])
{
     int i,j;
    int mini=c[0];
    for (i=0;i<2;i++)
    {
        for(j=0;j<5;j++)
        {

        if (mini>c[i][j])
            mini =c[i][j];
        }
    }
    return mini;
}