Wednesday, 8 January 2014

C Program to Print Alternate Using Array

# include<stdio.h>
# include<conio.h>
void main()
{
 int arr[10];
 int i,j,temp;
 clrscr();
 for(i=0;i<10;i++)
 scanf("%d",&arr[i]);
 for(i=0;i<10;i+=2)
 printf("%d ",arr[i]) ;
 getch();
}

C Program print alternate element of an array

 /*C Program to print alternate element of an array*/
# include<stdio.h>
# include<conio.h>

void main()
{
int arr[10];
int i,j,temp;
clrscr();
for(i=0;i<10;i++)
scanf("%d",&arr[i]);
for(i=0;i<10;i+=2)
printf("%d ",arr[i]) ;
getch();
}

C Program Accept 10 numbers and no. to search, print its occurrence Using Array

/* Accept 10 numbers and no. to search, print its occurence
 Write a program to accept 10 no’s  and a number to search and print how many times it occurs in array */

# include<stdio.h>
# include<conio.h>

void main()
{
 int arr[10];
 int i,cnt=0,tosearch;
 clrscr();
 /* accepting an array*/
 for(i=0;i<10;i++)
  {
   printf("\n Enter %d number-",i);
   scanf("%d",&arr[i]);
   }
 printf("\nEnter a number to be search-");
 scanf("%d",&tosearch);
 for(i=0;i<10;i++)
 {
  if(arr[i]==tosearch)
    cnt++;
 }
 printf("\n Number occurs %d times",cnt);
 getch();
}

Friend Function Using C++

#include<iostream.h>
class Sample
{
int a;
int b;
public:
void setvalue()
{
a=25;
b=40;
}
friend int avg(Sample s);
};
int avg(Sample s)
{
return(s.a+s.b)/2;
}
Void main()
{
Sample x;
x.setvalue();
cout<<avg(x);
}
************************************
Friend function
With the use of a friend function we can access the private data of a class.
class Abc
{
….
….
public:
….
friend void xyz();/declaration
};


Characteristics of friend function

>It is not in the scope of the class to which it has been declared 

>Since it is not in the scope of the class,it cannot be called using the object of that class

>It can be invoked like a normal function without the help of any object

>But we cannot access the member names directly & has to use an object name & dot operator
 (e.g. A.x)



>Usually it has the objects as arguments

Tuesday, 31 December 2013

C++ program which reads a lowercase letter and print equivalent uppercase letter

#include<iostream.h>
void main()
{

  clrscr();
  char s1,s2;
  cout<<"Enter The lower case charecter\n";
  cin>>s1;
  s2=s1-32;
  cout<<"\n upper case letter of " <<s1<<" is "<<s2;
   getch();
}

Example of Local Variable Using C++

1st example:::::::---------
#include<iostream.h>
  void function1()
 {
  int x;  
  x=10;
  cout<<"\n" <<x;
 }
void function2()
{
int x=34;
cout<<"\n" <<x;
}
void main()
{
       clrscr();
       function1();
       function2();

}

2nd example:::::::---------
#include<iostream.h>
void main()
{
   clrscr();
   int x =10        
   if(x==10)
    {
         int x; /*this x hides outer x*/
         x=77;
        cout<<"Inner x : " <<x;
     }
  cout<<"\n Outer x : "  <<x;
}

C++ Program prints the number 10 ten times Using static variable

#include<iostream.h>
void function()
{
static int k=10;
 cout<<k<<endl ;
 k++;
}
void main()
{
      clrscr();
     int i=10;
     for(i=0;i<10;i++)
     {
       function();
     }
}