Friday 20 July 2012

42) Write a program to finding the maximum element in an array

#include<conio.h>
#include<iostream.h>
#include<stdio.h>
void main()
{
      clrscr();

    int array[]={1,5,9,6,4};

  int var1=array[0];
  for(int i=1;i<=5;i++)
   {  if(var1<array[i])
       var1=array[i];
   }
  cout<<var1;
       getch();

}

Wednesday 11 April 2012

41).Write a Program To Print The Perfect Square Number Which is Less Than 1000.

#include<conio.h>
#include<iostream.h>
void main()
{
   clrscr();
   int n=1000, s=0;
   cout<<"The Squre values Which is less Than 1000 is::  " ;

   for(int i=1;s<n;i++)
    {
      s=i*i;
      if(s>n)
      break;

     cout<<s<<" ";

    }


       getch();

}

40).Write a Program to Find The Sum of The First 20 Odd Numbers

#include<conio.h>
#include<iostream.h>
void main()
{
   clrscr();
   int c=1,s=0,i=1;
   while(c<=20)
    {
      s=s+i;
      i=i+2;
      c++;

    }

     cout<<"Sum of The First 20 Odd Number is " <<s;
     getch();

}

Monday 9 April 2012

39).Write a program to explain the "Global Variable""

#include<stdio.h>
#include<iostream.h>
int count;                               /* count is global*/
void function1();
void function2();


void main()
{ clrscr();

  count=100;

  function1();

   getch();

}
void function1()
{
   int temp;
   temp=count;
   function2();
   cout<<"\n the value of temp and count ::" <<temp<<" " <<count;

}
void function2()
{
int count;
for(count=1;count<10;count++)
putchar('*\n');
}

38).Write a program to differentiate "Local variable" and "static variable"

#include<conio.h>
#include<iostream.h>
void function()
{
  int j=10;
  cout<<j<<"\n" ;
  j++;
}
void main()
{
       clrscr();
     int i=10;
     for(i=0;i<10;i++)

     {

       function();
     }

}

OUTPUT::

The following program prints the number 10 ten times.



#include<conio.h>
#include<iostream.h>
void function()
{
static int j=10;
 cout<<j<<"\n" ;
 j++;
}
void main()
{
       clrscr();
     int i=10;
     for(i=0;i<10;i++)

     {

       function();
     }

}


OUTPUT::
10
11
12
13
14
15
16
17
18
19

37).Write a Program to Explain The "Local Variable"

1).#include<conio.h>
   #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();

}


OUTPUT::  10
                  34




2)#include<conio.h>
#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;

}

OUTPUT::
Inner x : 77
Outer x: 10

write a program which reads a lowercase letter and print equivalent uppercase letter.

#include<conio.h>
#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();
}

35.write a program which reads a capital letter and print equivalent lower case letter.

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

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

34).Explain The Difference Between "static int variable" And "int variable"

#include<conio.h>
#include<iostream.h>
void function1()
{

  int x1=0;
  cout<<"The value of x1= \n" <<x1<<"\n";
  x1=x1+1;
}

void function2()
{

  static  int x2=0;
  cout<<"The value of x2= \n" <<x2<<"\n";
  x2=x2+1;
}
void main()
{
     clrscr();
  function1();
  function1();
  function1();
  function2();
  function2();
  function2();
   getch();
}


OUTPUT::
The value of x1= 0
The value of x1=0
The value of x1=0
The value of x2=0
The value of x2=1
The value of x2=2

Sunday 8 April 2012

33).Write a Program Which Explain The '++Array[i]' 'Array[i]++' And 'Array[i++]'.

#include<stdio.h>

void main()
{
    int a[5] = {5, 1, 15, 20, 25};
    int i, j, m;
    i = ++a[1];
    j = a[1]++;
    m = a[i++];
    printf("%d, %d, %d", i, j, m);
    
}
 
 
Explanation:

Step 1: int a[5] = {5, 1, 15, 20, 25}; The variable arr is declared as an integer array with a size of 5 and it is initialized to
a[0] = 5, a[1] = 1, a[2] = 15, a[3] = 20, a[4] = 25 .
Step 2: int i, j, m; The variable i,j,m are declared as an integer type.
Step 3: i = ++a[1]; becomes i = ++1; Hence i = 2 and a[1] = 2
Step 4: j = a[1]++; becomes j = 2++; Hence j = 2 and a[1] = 3.
Step 5: m = a[i++]; becomes m = a[2]; Hence m = 15 and i is incremented by 1(i++ means 2++ so i=3)
Step 6: printf("%d, %d, %d", i, j, m); It prints the value of the variables i, j, m
Hence the output of the program is 3, 2, 15
 

32). Program To Convert Celsius Temperature To Fahrenheit Temperature.

#include<conio.h>
#include<iostream.h>


void main()
{
   clrscr();

   float num1,num2;
   cout<<"Enter Fahrenheit Temperature\n" ;
   cin>>num1;
   num2=5/9*(num1-32);
   cout<<"\nCentigrade Temperature " <<num2;


}


NOTE::
This Program happily gets compiled and runs. However ,it gives '0' as the output for any value.The reason is the value of 5/9 is '0' and '0*something ' is always '0'.



#include<conio.h>
#include<iostream.h>


void main()
{
   clrscr();

  float num1,num2,num3;


       cout<<"\nEnter Centigrade Temperature ";
        cin>>num1;
        num2=((9/5)*num1)+32;
        num3=(1.8*num1)+32;

     cout<<"\nThe Fahrenheit Temperature is "<<num2<<" "<<num3;
}


OUTPUT::
Enter Centigrade Temperatur ::25

The Fahrenheit Temperature is 57 77.

31)Write a Program Which Reads Three Student Marks in a Test and Then Print Their Average

#include<conio.h>
#include<iostream.h>


void main()
{
   clrscr();
   int num1,num2,num3;
   float average;
   cout<<"Enter Three student marks.\n" ;
   cin>>num1>>num2>>num3;
   average=(num1+num2+num3)/3;
   cout<<"Average of  " <<num1 <<" "<<num2<<" "<<num3<<"is " <<average;
}

30).Sample of Program To Swap Two Numbers Without Using Temp Variable.

#include<conio.h>
#include<iostream.h>

void main()
{
   clrscr();
   int num1,num2;
   cout<<"\nEnter Two Numbers" ;
   cin>>num1>>num2;
   cout<<"\nThe value of num1 and num2  is " <<"num1= "  <<num1
   <<" num2= "<<num2;

   num1=num1+num2;
   num2=num1-num2;
   num1=num1-num2;
   cout<<"\nNow The value of num1 and num2 is " <<"num1= " <<num1
   <<" num2= "<<num2;

}

29).Sample of Program To Swap Two Numbers Using Temporary Variables.

#include<conio.h>
#include<iostream.h>
void main()
{
   clrscr();
   int num1,num2,temp;
   cout<<"\nEnter Two Numbers" ;
   cin>>num1>>num2;
   cout<<"\nThe value of num1 and num2 before swaping is " <<"num1= "  <<num1
   <<" num2= "<<num2;

   temp=num1;
   num1=num2;
   num2=temp;
   cout<<"\nThe value of num1 and num2 After swaping is " <<"num1= " <<num1
   <<" num2= "<<num2;




    getch();

28).Write a Program to Swap a Variable by Using Pointer

#include<conio.h>
#include<iostream.h>

void main()
{
   clrscr() ;

   int *x,*y ,a=0,b=1, temp;
   x=&a;
   y=&b;
   temp=*x;
   *x=*y;
   *y=temp;
   cout<<"the value of x and y  "<<x<<" " <<y;
   cout<<"\nthe value of a  and b "<<a<<" "<<b;
   cout<<"\nthe value of *x  and *y is "<<*x<<" "<<*y;
   cout<<"\nthe value of &a  and &b is "<<&a<<" "<<&b;

   getch();

}

27).Prints The 'N'th Character of The String

#include<stdio.h>
#include<string.h>

void main()
{
    printf("%c\n", "abcdefgh"[4]);
    
}
 
Explanation:

printf("%c\n", "abcdefgh"[4]); It prints the 5 character of the string "abcdefgh".
Hence the output is 'e'.
 

26)Write The Memory Address of A String as well as String.

#include<stdio.h>

int main()
{
    printf("%u %s\n", &"Hello1", &"Hello2");
    return 0;
}
 
 
Explanation:

In printf("%u %s\n", &"Hello", &"Hello");.
The %u format specifier tells the compiler to print the memory address of the "Hello1".
The %s format specifier tells the compiler to print the string "Hello2".
Hence the output of the program is "1022 Hello2".
 

25)."cba" will be the output of the program If characters 'a', 'b' and 'c' enter are supplied as input.

#include<stdio.h>

int main()
{
    void fun();
    fun();
    printf("\n");
    return 0;
}
void fun()
{
    char c;
    if((c = getchar())!= '\n')
        fun();
    printf("%c", c);
}
 

24).Explain "printf(Number+"strings")".

#include<stdio.h>

int main()
{
    printf(5+"Good Morning\n");
    return 0;
}
 
Explanation:

printf(5+"Good Morning\n"); It skips the 5 characters and prints the given string.
Hence the output is "Morning".
 

23).Print HELLO WORD by using String Functions.

#include<stdio.h>
#include<string.h>

int main()
{
    char str1[20] = "Hello", str2[20] = " World";
    printf("%s\n", strcpy(str2, strcat(str1, str2)));
    return 0;
}
 
 
Explanation:

Step 1: char str1[20] = "Hello", str2[20] = " World"; The variable str1 and str2 is declared as an array of characters and initialized with value "Hello" and " World" respectively.

Step 2: printf("%s\n", strcpy(str2, strcat(str1, str2)));
=> strcat(str1, str2)) it append the string str2 to str1. The result will be stored in str1. Therefore str1 contains "Hello World".

=> strcpy(str2, "Hello World") it copies the "Hello World" to the variable str2.

Hence it prints "Hello World".
 

22)Difference Between The " extern int variable" And "int variable".

#include<stdio.h>
int main()
{
    extern int a;
    printf("%d\n", a);
    return 0;
}
int a=20;
 
OutPut: 20 

21).11Write a C program to print all permutations of a given string

#include<conio.h>
#include<stdio.h>
#include<iostream.h>
#include<string.h>
#include<math.h>
void permute(char*string,int i,int n) ;
void main()
{
   clrscr();
   char string[]="ABC";
   cout<<string;

    permute(string,0,2);

    getchar();
    getch();

}
void swap(char*x,char*y)
{
 char temp;
 temp=*x;
 *x=*y;
 *y=temp;
}


void permute(char*string,int i,int n)
{
  if(i==n)
  {
    cout<<"\n"<< string;

  }
  else
  {
    for(int j=i;j<=n;j++)
    {
        swap((string+i),(string+j)) ;
        permute(string,i+1,n);
        swap((string+i),(string+j)) ;
    }
  }

}

20).Find The Length of a String Without Using Function

#include<conio.h>
#include<stdio.h>
#include<iostream.h>
#include<string.h>
#define MAX 80
void main()
{
   clrscr() ;
   int count=0;
   char string1[MAX+1] ;
   puts("Enter The String \n");

   gets(string1);
   puts(string1);
   for(int i=0;string1[i]!='\0';i++)
   {
     count++;
   }
   cout<<"The Length of String is " <<count<<"\n";
  getch();

}

19.Program to Accept a String and display in Reverse

#include<conio.h>
#include<stdio.h>
#include<iostream.h>
#include<string.h>
#define MAX 80
void main()
{  int length;
   clrscr() ;
   char string1[MAX+1] ;
   puts("Enter the string \n");
   gets(string1);
   puts(string1);
   length=strlen(string1);
   cout<<"Length of string is "<<length<<"\n";
   for(int i=length-1; i>=0;i--)
   {
     cout<<string1[i];
   }


   getch();

}

18).Program to Accept a String and display .

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

void main()
{
 clrscr();

   char string1[21];
   puts("enter a String") ;
   gets(string1);
   puts(string1);

    getch();

}

17).Example of fmod() Function.

#include<conio.h>
#include<stdio.h>
#include<math.h>
void main()
{
    clrscr();
    {
      printf("fmod of 3.14/2.1 is%lf \n", fmod(3.14,2.1));
    }


       getch();

}



NOTE:::

fmod(x,y) - Calculates x modulo y, the remainder of x/y.
This function is the same as the modulus operator. But fmod() performs floating point divisions.


 

16).2.Example for ceil() and floor() functions.

#include<conio.h>
#include<stdio.h>
#include<math.h>
void main()
{
    clrscr();
    {

      printf("\n Result :%f" ,ceil(1.44));

      printf("\n Result :%f" ,ceil(1.66));

      printf("\n Result :%f" ,floor(1.44));

      printf("\n Result :%f" ,floor(1.66));


    }


       getch();

}

15). Sum of Two Number Using Pointer.

#include<conio.h>
#include<iostream.h>
void main()
{
 clrscr();
  int *p,*q ,a,b ,sum;
  cout<<"Enter Two Numbers";
  cin>>a>>b;
  p=&a;
  q=&b;
  sum=*p+*q;
  cout<<"Sum is"<<sum;

       getch();

}

14).Check Vowel Using Switch Statement.

#include<conio.h>
#include<stdio.h>
#include<iostream.h>
void main()
{
   clrscr();
   char ch;
   cout<<"Enter a Charecter";
   cin>>ch;
   switch(ch)
   {
   case'a':
   case'A':
   case'e':
   case'E':
   case'i':
   case'I':
   case'o':
   case'O':
   case'u':
   case'U':
   cout<<"Charecter vowel";
   break;
   default:
   cout<<"Charector is consonent";
 }


       getch();

}

13).Program to Check Whether Input Alphabet is a Vowel or Not.

#include<conio.h>
#include<iostream.h>
void main()
{
 clrscr();
  char N;
  cout<<"Enter The Charecter: " ;
  cin>>N;


         if(N=='a'||N=='e'||N=='i'||N=='o'||N=='u')
         {
                cout<<"Charecter is Vowel.";
         }
         else
          cout<<"Charecter is Consonent.";
           getch();
}

12).Check Whether a Given Number is Armstrong .

#include<conio.h>
#include<iostream.h>
void main()
{
 clrscr();
  int N,temp,sum=0,remainder;
  cout<<"Enter the Number ";
  cin>>N;
  temp=N;
  while(temp!=0)
  {
   remainder=temp%10;
   sum=sum+remainder*remainder*remainder;
   temp=temp/10;

  }
  if(N==sum)
   {
    cout<<"The Number is Armstrong";
   }
   else
   cout<<"The Number is not Armstrong";



       getch();

}

11).Check Whether a Given Number is PALINDROME.

#include<conio.h>
#include<iostream.h>
void main()
{
    clrscr();
    int N ,a,rev=0;
    cout<<"Enter The Number:  " ;
    cin>>N;
    int temp;
    temp=N;
    while(temp>=1)
    {
      a=temp%10;
      rev=rev*10+a;
      temp=temp/10;

    }
      cout<< "The Reverse Number  is :  "<<rev<<"\n" ;
   if(N==rev)

      {
       cout<<"The Number is Palindrome ";

      }
     else
     {
        cout<<"The Number is not Palindrome";
     }
       getch();

}

10).Find The Reverse Of A Given Number.

#include<conio.h>
#include<iostream.h>

void main()
{
 clrscr();

  int a, r=0 ,N;
      cout<<"Enter The Number ";
      cin>>N;
      while(N>=1)
      {
          a=N%10;
          r=r*10+a;
          N=N/10;
      }
      cout<<"Reverse of The Number is "<<r;
       getch();

}

9).Check Whether A Given Number Is Prime Or Not.


#include<conio.h>
#include<iostream.h>
void main()
{
 clrscr();
    int r, N;

   cout<<"Enter the Number" ;
   cin>>N;
   for(int i=2;i<=N-1;i++)
   {
    if(N%i==0)
     r=1;
    break;
   }
   if(r==0)
        cout<<"The Number "<<N<<"is PRIME.";
   else
        cout<<"The Number is NOT PRIME.";

    getch();

}

8).Find The Factorial of a Given Number.

#include<conio.h>
#include<iostream.h>
void main()
{
 clrscr();
 int N,fact;

   cout<<"Enter a Number ' N' ";
   cin>>N;
   fact=N;
 for(int i=1;i<5;i++)
    {
       fact=fact*(N-i) ;
    }
      cout<<"The Factorial of "<<N<<"is : " <<fact;

     getch();
}

7).Check Where The Number is ' +ve ' and ' -ve '

#include<conio.h>
#include<iostream.h>
void main()
{
 clrscr();
 int N;
   cout<<"Enter a Number N ";
   cin>>N;
   if (N>=0)
    {
      cout<<"The Number is +ve";

    }
    else if(N<0)

     {
       cout<<"The Number is -ve";
     }

     getch();
}

6).Write a Program to Generate and Print First ' N ' FIBONACCI Number.

#include<conio.h>
#include<iostream.h>
#include<math.h>

void main()
{
 clrscr();

  int num1=0,num2=1,N,num3;

  cout<<" ENTER THE NUMBERS  ";
  cin>>N;
  cout<<" "<<num1<<" "<<num2;
   while(N<=100)
   {
       num3=num1+num2;
       cout<<" "<<num3;
       num1=num2;
       num2=num3;
       N++;
   }

       getch();

}

5).Find The Biggest of Three Numbers

#include<conio.h>
 #include<iostream.h>
#include<math.h>

void main()
{
 clrscr();

  int a,b,c;

  cout<<" ENTER THE THREE NUMBERS  ";
  cin>>a>>b>>c;

  if( a>b && a>c)
   {
    cout<<"The Greates Number is "<<a;
   }

  else
   if(b>a && b>c)
     {
      cout<<"The Greates Number is"<<b;

     }
   else
   if(c>a && c>b)
   {
    cout<<"The Geatest Number is"<<c;
   }
       getch();

4).Write a Program to Check Whether a Given Integer is ODD or EVEN.

#include<conio.h>
#include<iostream.h>
#include<math.h>

void main()
{
 clrscr();

  int N;

  cout<<" ENTER THE NUMBER  ";
  cin>>N;
  if(N%2==0)
   {
    cout<<"The Number is EVEN.";
   }

  else

   cout<<"The Number is ODD";
       getch();

}

Write a Program to Find The Area of a Circle,Given The Radius

#include<conio.h>
#include<iostream.h>
#include<math.h>

void main()
{
 clrscr();

  int r;
  float P=3.14,area;
  cout<<" enter the radious of a circle  ";
  cin>>r;
  area=P*r*r;

  cout<<"The Area of Circle is"<<"= "<<area;
       getch();

}

Find The Area of Triangle if Three Sides are Given::

#include<conio.h>
#include<stdio.h>
#include<iostream.h>
#include<math.h>
void main()
{
 clrscr();

  int a,b,c,s,sum,area;
  cout<<"/n enter the three sides of a triangle  ";
  cin>>a>>b>>c;
  sum=a+b+c;
  s= sum/2;
  area=s*(s-a)*(s-b)*(s-c);
  area=sqrt(area);

  cout<<"the area of triangle is"<<"= "<<area;
       getch();

}

Friday 6 April 2012

Find sum of 'N' natural numbers.

#include<conio.h>
#include<iostream.h>
void main()
{
  clrscr(); 
  int sum=0,i,num;
  cout<<"Enter a number";
  cin>>num;
  for(i=1;i<=num;i++)
  {
      sum=sum+i;
  }
  cout<<"Sum of "<<num<<"natural numbers = " <<sum; 
  getch();
}

About this blog

Hello World,

I have started this blog so that student/job-seekers may find a comprehensive list of C/C++ programs at a single place to prepare for their interviews or just hone up their programming skills. This blog will contain most frequently asked C/C++ programs. I strongly recommend all the readers to please try out these programs before appearing for any interview.

I also encourage readers to post comments in case they have any doubt. I will try to respond at the earliest.

Happy Coding!!!

Write a program which reads a set of integers into an integer array and then prints "YES"if all of them are same otherwise print "NO".

#include<stdio.h> int main() { int a[10],M=0,i,n; printf("enter a value for n\n"); scanf("%d",&n); fo...