Thursday 26 December 2013

C program using pointers to read in an array of integers and print its elements in reverse order.

#include<stdio.h>
#include<conio.h>
#define MAX 30

void main()
{
int size,i,arr[MAX];
int *ptr;
clrscr();

ptr=&arr[0];

printf("Enter the size of array : ");
scanf("%d",&size);

printf("nEnter %d integers into array:n",size);
   for(i=0;i<size;i++)
   {
   scanf("%d",ptr);
   ptr++;
   }

ptr=&arr[size-1];

printf("nElements of array in reverse order are:n");

 for(i=size-1;i>=0;i--)
   {
   printf("nElement%d is %d :",i,*ptr);
   ptr--;
   }

getch();
}
Output :

Enter the size of array : 5
Enter 5 integers into array : 11 22 33 44 55
Elements of array in reverse order are :
Element 4 is : 55
Element 4 is : 44
Element 4 is : 33
Element 4 is : 22
Element 4 is : 11

C Program to Implement Stack Operations Using Array

#include<stdio.h>
#define M 50
struct state
{
    char name[50];
    long int population;
    float literacyRate;
    float income;
}st[M];     /* array of structure */
int main()
{
int i,n,ml,mi,maximumLiteracyRate, maximumIncome;
float rate;
ml = mi =-1;
maximumLiteracyRate = maximumIncome = 0;

printf("Enter how many states:");
scanf("%d",&n);
for(i=0;i<n;i++)
  {
    printf("\nEnter state %d details :",i);
    printf("\n\nEnter state name : ");
    scanf("%s",&st[i].name);
    printf("\nEnter total population : ");
    scanf("%ld",&st[i].population);
    printf("\nEnter total literary rate : ");
    scanf("%f",&rate);
    st[i].literacyRate = rate;
    printf("\nEnter total income : ");
    scanf("%f",&st[i].income);
  }
for(i=0;i<n;i++)
{
    if(st[i].literacyRate >= maximumLiteracyRate)
        {
        maximumLiteracyRate = st[i].literacyRate;
        ml++;
        }
    if(st[i].income > maximumIncome)
        {
        maximumIncome = st[i].income;
        mi++;
        }
}
printf("\nState with highest literary rate :%s",st[ml].name);
printf("\nState with highest income :%s",st[mi].name);
return(0);
}
Output :
Enter how many states:3

Enter state 0 details :

Enter state name : Maharashtra

Enter total population : 1000

Enter total literary rate : 90

Enter total income : 10000

Enter state 1 details :

Enter state name : Goa

Enter total population : 300

Enter total literary rate : 94

Enter total income : 5000

Enter state 2 details :

Enter state name : Gujrat

Enter total population : 900

Enter total literary rate : 78

Enter total income : 7000

The state whose literary rate is the highest : Goa
The state whose income is the highest : Maharashtra

Wednesday 18 December 2013

C Program to implement 4-Queens Problem using Backtracking

#include<stdio.h>
#include<conio.h>
#include<math.h>
char a[10][10];
int n;
void printmatrix()
{
 int i,j;
 printf("n");
 for(i=0;i < n;i++)
       {
       for(j=0;j < n;j++)
            printf("%ct",a[i][j]);
       printf("nn");
       }
}
int getmarkedcol(int row)
{
       int i,j;
       for(i=0;i < n;i++)
       if(a[row][i]=='Q')
       {
                       return(i);
                       break;
       }
}

int feasible(int row, int col)
{
 int i,tcol;
 for(i=0;i < n;i++)
       {
     tcol=getmarkedcol(i);
           if(col==tcol || abs(row-i)==abs(col-tcol))
              return 0;
       }
       return 1;
}
void nqueen(int row)
{
 int i,j;
 if(row < n)
 {
                for(i=0;i < n;i++)
 {
                                       if(feasible(row,i))
      {
                                                       a[row][i]='Q';
                                                        nqueen(row+1);
                                                       a[row][i]='.';
                                       }
                       }
       }
       else
       {
                       printf("nThe solution is:- ");
                       printmatrix();
       }
}
void main()
{
       int i,j;
       clrscr();
printf("n Enter the no. of queens:- ");
       scanf("%d",&n);

       for(i=0;i < n;i++)
                       for(j=0;j < n;j++)
                                       a[i][j]='.';
       nqueen(0);
       getch();

}

Armstrong Number Using C++

/*Armstrong Number  Using C++ Program  CHECK THE GIVEN NUMBER IS ARMSTRONG OR NOT

FOR EXAMPLE
153/1^3+5^3+3^3=153
*/
#include<iostream.h>
#include<conio.h>
void main()
{
  clrscr();
  int i,n,m,re,sum=0;
  cout<<"enter the required number";
  cin>>n;
  m=n;
  while(m>0)
   {
    re=m%10;
    m=m/10;
    sum=sum+re*re*re;
   }
  if(sum==n)
   {
    cout<<"given number"<<n<<"is armstrong";
   }
  else
   {
    cout<<"given number"<<<n<"is not armstrong";
   }
  getch();
}

Even or Odd For 1 to 100 Number Using C++


#include<iostream.h>
#include<conio.h>
void main()
{
  int  i;
  for(i=1;i<=100;i++)
  {
  if(i%2==0)
  {
                  cout<<i<<"  number is even \n “;
   }
  else
  {
                 cout<<i<<"   number is odd\n“;
  }
}
  getch();

}

Given Number Even Or Odd Using C++


#include<iostream.h>
#include<conio.h>
void main()
{
  int  num;
cout<<" Even or odd Number given by user\n “;
 cout<<" Enter number for verification  “;
 cin>>num;
if(num%2==0)
  {
  cout<<" Given number is even   “;
   }
  else
  {
      cout<<" Given number is odd“;
  }
 getch();
}

Fibonacci Series Using C++

/*Fibonacci  Series up to user Given number Using C++
   Series Limit is Defined by User
*/
#include<iostream.h>
#include<conio.h>
void main()
{
  int  n,i,fst=0,sec=1,num;
  cout<<" Enter number for Fibonacci  Series “;
  cin>>num;
  cout<<" Fibonacci Series 0 to” <<num <<”number”;
  for(i=0;i<= num;i++)
  {
  if(i<=1)
  {
    n=i;
  }
  else
  {
    n= fst+sec;
   fst=sec;
   sec=n;
  }
 Cout<<”\t”<<n;
 }
 getch();
}