Showing posts with label C++. Show all posts
Showing posts with label C++. Show all posts

Saturday, 15 September 2018

Program to do binary search of element in C

What is searching ?

It is defined as find data of a element. We use different types of searching techniques for searching an element.
               Here we use binary search techniques in C.

Program to do binary search of element in C 

#include<conio.h>
void main ()
{
int data[10],beg,mid,end,item,lb,ub,i;
clrscr ();
printf ("\n Enter 5 element in selected order:");
scanf ("%d", &item);
beg=0;
end=4;
mid=(beg+end)/2;
while(beg<=end && data[mid]!=item)
{
      if (item>data[mid])
     {
      beg=mid+1;
      }
      else
       {
      end=mid-1;
       }
      mid=(beg+end)/2;
}
if(beg<end)
{
   printf("\n location = %d",mid+1);
}
else
{
   printf("\n unsuccessful:");
}
getch ();
}

OUTPUT ::  Enter 5 element in selected order = 25,45,65,75,85

                 ITEM SEARCHED ; 45

Monday, 10 September 2018

Program for sorting of array element using bubble sort in C

What is sorting ?

The term sorting means arrangement of element in particular order i.e in ascending or descending  order is known as sorting.

Example:: 45,55,25,35,65
Sorted data :: 25,35,45,55,65

How bubble sort work ?

Bubble sort method works on method of comparing two consecutive digits.

45,55,25,35,65

working::
45,55,25,35,65
45,25,55,35,65
25,45,55,35,65
25,45,35,55,65
25,35,45,55,65   ( sorted data )

Program for bubble sort in c 

#include<stdio.h>
#include<conio.h>
void main ()
{
int i,j,n,t,a[100];
clrscr ();
printf ("\n Enter size of array:");
scanf ("%d", &n);
printf ("\n Enter element in array:");
for (i=0;i<n;i++)
{
scanf ("%d", &a[i]);
}
for (i=o;i<n-1;i++)
{
for (j=0;j<n-i-1;j++)
{
if (a[j]>a[j+1])
{
t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}
}
}
printf (sorted array=");
for (i=0;i<n;i++)
{
printf ("\t %d",a[i]);
}
getch ();
}



Tuesday, 4 September 2018

Program to calculate area of different shape in C using switch statement

#include<stdio.h>
void main ()
{
int choice,r,l,b,base,h,area1,area2,area3;
clrscr();
printf ("\n you can perform following operation:");
printf ("\n 1. Area of circle:");
printf ("\n 2. Area of rectangle:");
printf ("\n 3. Area of parallelogram;");
printf ("\n Enter your choice:");
scanf ("%d", &choice);
switch ( choice )
{
       case 1:
       printf ("\n Enter value of radius:");
       scanf ("%d", &r);
       area1 = 3.14*(r*r);
       printf ("%d", area);
break;
       case 2:
       printf ("\n Enter value of length:");
       scanf ("%d", &l);
       printf ("\n Enter value of breadth:");
       scanf ("%d", &b);
       area2 = l*b;
       printf ("%d", area2);
break;
       case 3:
       printf ("\n Enter value of base:");
       scanf ("%d", &base);
       printf ("\n Enter value of height:");
       scanf ("%d", &h);
       area3 = base*h;
       printf ("%d", area3);
break;
       default :
       printf ("\n Invalid number:");
}
getch ();
}
      








Thursday, 30 August 2018

Program in C to print digit pattern according to input number from user

(1.)  For printing this type structure in C:

1
1 2
1 2 3 
1 2 3 4
1 2 3 4 5

Here as we seen the figure is in row and column form thus we have to declare two variable ( i and j ) for row and column respectively. Here we use for loop statement of c.

/* program to print structure */
#include<stdio.h>
void main()
{
int i,j,n ;
clrscr () ;
printf ("\n Enter number of rows:") ;
scanf ("%d", &n) ;
for (i=1;i<=n;i++)
{
    for (j=1;j<=i;j++)
    {
    printf ("%d", j) ;
    }
printf ("\n") ;
}
getch () ;
}

Output screen :: 

Enter number of rows :: 5

output :: 

1
1 2
1 2 3 
1 2 3 4
1 2 3 4 5

 (2.)   For printing this type structure in C:

1
2 2
3 3 3 
4 4 4 4
5 5 5 5 5

Here as we seen the figure is in row and column form thus we have to declare two variable ( i and j ) for row and column respectively. Here we use for loop statement of c.

/* program to print structure */
#include<stdio.h>
void main()
{
int i,j,n ;
clrscr () ;
printf ("\n Enter number of rows:") ;
scanf ("%d", &n) ;
for (i=1;i<=n;i++)
{
    for (j=1;j<=i;j++)
    {
    printf ("%d", i) ;
    }
printf ("\n") ;
}
getch () ;
}

Output screen :: 

Enter number of rows :: 5

output :: 

1
2 2
3 3 3 
4 4 4 4
5 5 5 5 5

 (3.)  For printing this type structure in C:

1
2 3
4 5 6
7 8 9 10
11 12 13 14 15

Here as we seen the figure is in row and column form thus we have to declare two variable ( i and j ) for row and column respectively. Here we use for loop statement of c.

/* program to print structure */
#include<stdio.h>
void main()
{
int i,j,n,number=0;
clrscr () ;
printf ("\n Enter number of rows:") ;
scanf ("%d", &n) ;
for (i=1;i<=n;i++)
{
    for (j=1;j<=i;j++)
    {
    number++;
    printf ("%d", number) ;
    }
printf ("\n") ;
}
getch () ;
}

Output screen :: 

Enter number of rows :: 5

output :

1
2 3
4 5 6
7 8 9 10
11 12 13 14 15










Saturday, 25 August 2018

Program in C to print different types of pattern

For printing this type structure in C:

* * * * *
* * * * 
* * * 
* * 


Here as we seen the figure is in row and column form thus we have to declare two variable ( i and j ) for row and column respectively. Here we use for loop statement of c.

/* program to print structure */
#include<stdio.h>
void main()
{
int i,j,n ;
clrscr () ;
printf ("\n Enter number of rows:") ;
scanf ("%d", &n) ;
for (i=1;i<=n;i++)
{
for (j=1;j<=n;j++)
{
if (i<=j)
printf ("*") ;
else 
printf ("  ") ;
}
printf ("\n") ;
}
getch () ;
}

Output screen :: 

Enter number of rows :: 5

output :: 

* * * * *
* * * * 
* * * 
* * 

        

Tuesday, 21 August 2018

Program in C to print different types of structure

For printing this type structure in C:

* * * * * 
* * * * * 
* * * * * 
* * * * * 
* * * * * 

Here as we seen the figure is in row and column form thus we have to declare two variable ( i and j ) for row and column respectively. Here we use for loop statement of c.

/* program to print structure */
#include<stdio.h>
void main()
{
int i,j,n ;
clrscr () ;
printf ("\n Enter number of rows:") ;
scanf ("%d", &n) ;
for (i=1;i<=n;i++)
{
for (j=1;j<=n;j++)
{
printf ("*") ;
printf ("  ") ;
}
printf ("\n") ;
}
getch () ;
}

Output screen :: 

Enter number of rows :: 5

output :: 
            
* * * * * 
* * * * * 
* * * * * 
* * * * * 
* * * * * 

For printing this type structure in C:


* *
* * *
* * * *
* * * * *
* * * * * *


Here as we seen the figure is in row and column form thus we have to declare two variable ( i and j ) for row and column respectively. Here we use for loop statement of c.

/* program to print structure */
#include<stdio.h>
void main()
{
int i,j,n ;
clrscr () ;
printf ("\n Enter number of rows:") ;
scanf ("%d", &n) ;
for (i=1;i<=n;i++)
{
for (j=1;j<=i;j++)
{
printf ("*") ;
printf ("  ") ;
}
printf ("\n") ;
}
getch () ;
}

Both of program have nearly same syntax but there is small change in their column for loop statement in case of first type their condition is for (j=1;j<=n;j++) and in case second we have use for (j=1;j<=i;j++).

Output Screen :: 

Enter number of rows :: 5

output ::



* *
* * *
* * * *
* * * * *












Saturday, 18 August 2018

Difference between if else statement an switch statement in C using a program

Program To Find Maximum Of Three Number Using 'If else' Statement 

/*program to find maximum of 3 number*/
#include <stdio.h>
void main ()
{
int a,b,c;
clrscr ();
printf ("/n Enter value of a :");
scanf ("%d", & a);
printf ("/n Enter value of b :");
scanf ("%d", & b);
printf ("/n Enter value of c :");
scanf ("%d", & c);
if ((a>>b)&&(a>>c))
printf ("/n a is maximum number:");
else if ((b>>a)&&(b>>c))
printf ("/n b is maximum number:");
else
printf ("/n c is maximum number:");
getch ();
}

Here we use if else statement for finding maximum of three number.

Output :: Enter value of a 
               a = 25                                                                                                                                                                                           Enter value of b
               b = 35
               Enter value of c
               c = 45 

               Maximum number is c.

Program To Displays Color According To The Enter Character From User in C

/*program to print color*/
# include <stdio.h>
void main ()
{
char ch ;
clrscr () ;
printf ("/n enter a character :") ;
scanf ("%c", & ch) ;
switch (ch)
{
case 'r' ;
printf ("\n colour is red :") ;
break ;
case 'w' ;
printf ("\n color is white :") ;
break ;
case 'y' ;
printf ("\n color is yellow :") ;
break ;
case 'g' ;
printf ("\n color is green :") ;
break ;
case 'b' ;
printf ("\n color is blue :") ;
break ;
default :
printf ("\n invalid character :") ;
}
getch () ;
}

Difference in points :

IF else statement in C

1. Here we can use more than one statement for execution.

2. Here we can use all types of variable.

3. If else statement contains two types of statement means one in if and other in else.

4. If condition written inside if is false then compiler will shift to the statement written in else statement.

5. syntax :  @  if statement 
   
1. syntax :  if (condition)
                   statement :

2. syntax :  if (condition)
                  {
                  statement 1 ;
                  statement 2 ;
                  '
                  '
                 }

@  if else statement 

1. syntax;  if (condition)
      {
      statement ;
      '
      '
      }
      else
      {
      statement ;
       '
       '
       }


Switch statement in C

 1. Here we can use only one statement for execution.

 2. Here we can use only character types of variable.

 3. switch statement contains two types of statement means one in switch and other in default statement.

4. If condition written inside switch is false then compiler will shift to the statement written in default statement.

5. @  switch statement 
    
   syntax ;   switch ( conditional variable )
                  {
                  case value 1 ;
                  statement ;
                  break ;
                  case value 2 ;
                  statement ;
                  break ;
                  '
                  '
                  '
                 default :
                 statement ;
                 }












Tuesday, 14 August 2018

Recursion used in C language

What is recursion in C ?

When a function call to itself or when a function call to a second function and in function definition of second function again a function call statement for first function is encountered, it is known as recursion and this types of function is called recursive function.

Types of recursion in C ?

1. Direct recursion :

                ABC ()
                {
                 "
                 "
                 "
                ABC () ;
                }

2. Indirect recursion :

                ABC ()
                  {
                   "
                   XYZ () ;
                   "
                 XYZ ()
                 {
                 "
                ABC () ;
                "

Program of recursive function to find factorial in C:

#include < stdio.h>
int factorial ( int ) ;
void main ()
{
int a , b ;
printf ("\n Enter a number:") ;
scanf ("%d", &a) ;
b = factorial (a) ;
printf ("%d" , b) ;
getch () ;
}
int factorial ( int a )
{
if ( a == 0 )
return 1 ;
else
return ( a * factorial ( a - 1 ) ) ;
}



Wednesday, 8 August 2018

Algorithm to find factorial of input number from user in C language

Program To Find Factorial Of Any Number in " C" Language

/* factorial of a number */
# include <stdio.h>
void main ()
{
int a ;
fact = 1;
clrscr () ;
printf ("\n enter a number:") ;
scanf ("%d", & a) ;
while ( a!=0 )
{
fact = fact*a ;
a-- ;
}
printf ("\n factorial of %d =%d", a,fact) ;
getch () ;
}

Output in c :

Enter a number : 4
factorial : 4 * 3 * 2 * 1 = 24
Enter a number : 6
factorial : 6 * 5 * 4 * 3 * 2 * 1 = 720

ALGORITHM :  TO FIND FACTORIAL OF A NUMBER INPUT FROM USER IN C

                         This is a program for calculation of factorial of a number input from user in c.

STEP 1. START.
STEP 2. SET FACT = 1.
STEP 3. INPUT A NUMBER SAY A.
STEP 4. { CHECK FOR CONDITION }
               A ! = 0.
            { HERE A ! = 0 BECAUSE FACTORIAL IS ALWAYS MULTIPLE OF MAXIMUM OF THE GIVEN NUMBER TO ONE}
STEP 5. FACT = FACT * A.
STEP 6. A = A - 1.
STEP 7. PRINT VALUE OF FACT.
STEP 8. STOP.

                     

Monday, 6 August 2018

Algorithm To Displays Color According To The Enter Character From User in C

Program To Displays Color According To The Enter Character From User in C

/*program to print color*/
# include <stdio.h>
void main ()
{
char ch ;
clrscr () ;
printf ("/n enter a character :") ;
scanf ("%c", & ch) ;
switch (ch)
{
case 'r' ;
printf ("\n colour is red :") ;
break ;
case 'w' ;
printf ("\n color is white :") ;
break ;
case 'y' ;
printf ("\n color is yellow :") ;
break ;
case 'g' ;
printf ("\n color is green :") ;
break ;
case 'b' ;
printf ("\n color is blue :") ;
break ;
default :
printf ("\n invalid character :") ;
}
getch () ;
}

Algorithm To Displays Color According To The Enter Character From User in C

ALGORITHM :: DISPLAY COLOR AFTER INPUT CHARACTER FROM USER.

This is program to input a character from user and display color.

STEP 1. START.
STEP 2. INPUT A.
STEP 3. { DISPLAY COLOR }
             
              1. IF ENTER WORD IS R.
              2. DISPLAY COLOR IS " RED ".
              3. IF ENTER WORD IS W.
              4. DISPLAY COLOR IS " WHITE " .
              5. IF ENTER WORD IS Y.
              6. DISPLAY COLOR IS " YELLOW ".
              7. IF ENTER WORD IS G.
              8. DISPLAY COLOR IS " GREEN ".
              9. IF ENTER WORD IS B.
              10. DISPLAY COLOR IS " BLUE "

OTHERWISE
11. PRINT " INVALID CHARACTER " .
STEP 4. STOP





Algorithm with the help of program to find out even or odd In C

Program to Check Number is Even or Odd

Here we are going to write program to check enter  number is even or odd . We have to know about operator used in C which is most important for applying condition in the program . On the previous blog I told you about operator used in C so please read from there . For writing program to check number is even or odd. As we know that even number are those number which are exactly divisible by 2 and odd number are those which are not divisible by 2. Now we are going to write program.

/*program to check no.is even or odd*/
#include <stdio.h>
void main()
{
int a;
clrscr ();
printf ("/n Enter a number:");
scanf ("%d", & a);
if ( a%2==0)
printf ("/n a is even number:");
else 
printf ("/n a is odd number :");
getch();
}

From written program you seen that first of all we have to enter a number from the user and then we have used condition for checking. A even number is divisible by 2 then it must gives 0 as remainder then print number is even otherwise it will we odd number. Please comment for any issue in this program for spreading information.

Algorithm to find out even or odd :

Now we are going to write algorithm for this program as we discuss earlier algorithm is step by step representation of program. Algorithm is generally out in capital letter and always start with START and end with STOP. In C programming 
language it is very important to write algorithm.

ALGORITHM : CHECK NUMBER IS EVEN OR ODD.

                           This is a program to input a number and check input number is even or odd.

STEP 1. START.
STEP 2. INPUT A NUMBER.
STEP 3. { CHECK NUMBER IS EVEN OR ODD }

1. IF A IS DIVISIBLE BY 2.
2. PRINT  " A IS EVEN ".
3. OTHERWISE.
4, PRINT " A IS ODD".

       { END OF LOOP 3 }
STEP 4. STOP.




Saturday, 4 August 2018

Algorithm for finding maximum of three number in C.

Program To Find Maximum Of Three Number Using 'If else' Statement

/*program to find maximum of 3 number*/
#include <stdio.h>
void main ()
{
int a,b,c;
clrscr ();
printf ("/n Enter value of a :");
scanf ("%d", & a);
printf ("/n Enter value of b :");
scanf ("%d", & b);
printf ("/n Enter value of c :");
scanf ("%d", & c);
if ((a>>b)&&(a>>c))
printf ("/n a is maximum number:");
else if ((b>>a)&&(b>>c))
printf ("/n b is maximum number:");
else
printf ("/n c is maximum number:");
getch ();
}

OUTPUT :

                    Enter value of a : 45
                    Enter value of b : 55
                    Enter value of c : 65

65 is the greatest number.

ALGORITHM :: MAXIMUM OF THREE NUMBER ( A , B AND C )

This algorithm is used to determined the maximum of three number say ( a , b , c )

STEP 1. START.
STEP 2. INPUT A , B AND C.
STEP 3. [ MAXIMUM OF THREE NUMBER A , B AND C ]

              1. IF A IS GREATER THAN B AND A IS ALSO GREATER THAN C.
              2. PRINT " A IS MAXIMUM ".
              3. IF B IS GREATER THAN A AND B IS ALSO GREATER THAN C.
              4. PRINT " B IS MAXIMUM ".
              5. IF C IS GREATER THAN A AND C IS ALSO GREATER THAN B.
              6. PRINT " C IS MAXIMUM ".

                [ END OF LOOP 3 ]

STEP 4. STOP






Friday, 3 August 2018

Algorithm to check Input Character Check is Vowel or Consonant in C


Program To Input a Character From User And Check Enter Character Is Vowel or Consonant

/*program character is c or v*/
#include <stdio.h>
void main ()
{
char ch ;
clrscr () ;
printf ("/n Enter a character:") ;
scanf ("%c", & ch) ;
switch (ch)
{
case 'a';
printf ("/n vowel:") ;
break ;
case 'e';
printf ("/n vowel:") ;
break ;
case 'i';
printf ("/n vowel:") ;
break ;
case 'o';
printf ("/n vowel:") ;
break ;
case 'u';
printf ("/n vowel:") ;
break ;
default :
printf ("/n character is consonant:") ;
}
getch () ;
}

Output :: Enter a character :  a

                   Character is vowel
                 
                   Enter a character :   z
     
                  character is consonant 

ALGORITHM :: 

                                CHECK INPUT CHARACTER IS VOWEL OR CONSONANT

This is a program of having to check input character is vowel or consonant .

STEP 1. START.
STEP 2. INPUT A CHARACTER.
STEP 3. [ CHECK VOWEL OR CONSONANT ]
                1. IF ENTER CHARACTER IS A , E , I . O , U ]
                2. PRINT CHARACTER IS VOWEL.
                3. OTHERWISE.
                4. PRINT CHARACTER IS CONSONANT.
             [ STEP LOOP 3 IS END ]
STEP 4. STOP.

Thursday, 2 August 2018

ALGORITHM OF CALCULATION OF ADDITION , SUB , MULTI , DIV OF A AND B IN C

Program To Input Two Number From User And Calculate Add,Sum,Multi,and Div Of The Number According To The Choice

/*program of menu driven program*//
# include <stdio.h>
void main ()
{
int a,b,c,choice ;
clrscr () ;
printf ("/n enter the first number :") ;
scanf ("%d", & a) ;
printf ("/n enter second number :");
scanf ("%d", & b) ;
printf ("/n you can perform the following operation :") ;
printf ("/n 1.addition :") ;
printf ("/n 2.subtraction :") ;
printf ("/n 3. multiplication :") ;
printf ("/n 4. division :") ;
printf ("/n enter your choice :") ;
scanf ("%d", & choice) ;
switch (choice)
{
case 1 ;
c= a+b ;
printf ("/n sum = %d", c) ;
break ;
case 2 ;
c= a-b ;
printf ("/n sub = %d", c) ;
break ;
case 3 ;
c= a*b ;
printf ("/n multi = %d", c) ;
break ;
case 4 ;
c= a\b ;
printf ("/n div = %d". c) ;
break ;
default :
printf ("/n invalid number :") ;
}
getch () ;
}
Output :: enter the first number :
                number is : 45
                enter second number 
                 number is : 35

c = 45 + 35 = 80   ( if choice is 1 )
c = 45 - 35 = 10     ( if choice is 2 )
c = 45 * 35 = 1575  ( if choice is 3 )
c = 45 / 35 = 1.2      ( if choice is 4 )

ALGORITHM : CALCULATE ADDITION , SUB , MULTI , DIV OF A AND B

This is program of having calculation according to the choice of user.

STEP 1. START.
STEP 2. INPUT A AND B AND THEN CHOICE.
STEP 3. [ CALCULATE ADDITION , SUB , MULTI , DIV OF A AND B ]
              
              1. IF ENTER CHOICE IS 1.
               2. PRINT " C = A + B ".
               3. IF ENTER CHOICE IS 2.
               4. PRINT " C = A - B ".
               5. IF ENTER CHOICE IS 3.
               6. PRINT " C = A * B ".
               7. IF ENTER CHOICE IS 4.
               8. PRINT " C = A / B ".

OTHERWISE

9. PRINT " INVALID CHOICE".
        { STEP 3 LOOP 9 IS END }
STEP 4. STOP




                











              


Wednesday, 1 August 2018

Algorithm of Fibonacci series in c



Program to print n terms of fibonacci series in c


/* program to print fibo series */
#include <stdio.h>
void main ()
{
int a=0,b=1,sum,counter=0,n ;
clrscr () ;
printf ("/n %d",a) ;
printf ("/n %d",b) ;
printf ("/n how many terms you want to print:") ;
scanf ("%d", & n) ;
while (counter<n-2)
{
sum=a+b ;
printf ("/n %d",sum) ;
a=b ;
b=sum ;
counter++ ;
}
getch () ;
}

Output :

How many terms you want to print : 5
number are : 0 , 1 , 1 , 2 , 3.

Algorithm ;;

ALGORITHM : FIBONACCI SERIES ( A , B , N , COUNTER )

This algorithm is used to print n terms of fibonacci series 

STEP 1. START.
STEP 2. SET A = 0 , B = 1 , COUNTER = 0 .
STEP 3. INPUT N.
STEP 4. WRITE A.
STEP 5. WRITE B.
STEP 6. REPEAT STEP 7 TO 11.
                  COUNTER < N -2
               [ N - 2 IS WRITTEN BECAUSE OF PRINTING FIRST TWO TERMS OUT OF LOOP ]
STEP 7. SUM : = A + B.
STEP 8. PRINT SUM.
STEP 9. SET A : = B.
STEP 10. SET B : = SUM.
STEP 11. [ INCREMENT THE VALUE OF COUNTER BY 1 ]
                  COUNTER : = COUNTER + 1.
STEP 12. STOP.



Sunday, 29 July 2018

Program to input number from user ( negative or positive ) calculate addition of only negative number

Using while loop ::

#include <stdio.h>
void main ()
{
int a ;
int sum = 0 ;
clrscr () ;
while ( a ! = 0 )
{
printf ("\n Enter a number:") ;
scanf ("%d", &a) ;
sum = sum + a ;
a-- ;
}
printf ("\n %d", sum) ;
getch () ;
}

Using for loop ::

#include <stdio.h>
void main ()
{
int a ;
int sum = 0 ;
clrscr () ;
for ( ; a != 0 : a-- )
{
printf ("\n Enter a number:") ;
scanf ("%d", &a) ;
sum = sum + a ;
}
printf ("\n %d", sum) ;
getch () ;
}

Output ::

Enter number :   5 , 6 , -7 ,-8 
sum =  -7 + -8 = -15
Enter a number : 5 , 6
output = nothing 

Program to find maximum of three number using user define function in C

A function can be define by the user as per the requirement of the user or programmer . To implement
a user define function , a programmer must contain the following three statement .

1. Function declaration or function prototype 

2. Function call statement

3. Function definition

Program of find maximum of three number using user define function in C

#include <stdio.h>
int max ( int , int , int ) ;                                                                                                                      void main () ;
{
int a , b , c ;
clrscr () ;
printf ("\n Enter value of a:") ;
scanf ("%d", &a) ;
printf ("\n Enter value of b:") ;
scanf ("%d", &b) ;
printf ("\n Enter value of c:") ;
scanf ("%d", &c) ;
d = max ( a,b,c ) ;
printf ("\n maximum number is %d",d) ;
getch () ;
}
int max ( int a , int b , int c ) 
{
if ( (a>b) && (a>c) )
return a ;
else if ( (b>a) && (b>c) )
return b ;
else
return c ;
}

This is program for finding maximum of three number in C using user define function. Here first of all we have to input three number from the user and write function call statement and print the value of maximum and outside the main function the function definition is written where the execution of program is takes place and output goes again to the function call statement where it is print on the output screen.



Saturday, 28 July 2018

Difference between while statement and do while statement used in C

While statement ::

Syntax :: (1)  while ( condition )
                 statement ;

               (2) while ( condition )
                    {
                     statement 1 :
                        "
                        "
                    }

Do while statement ::

Syntax ::   do 
                 {
                 statement 1 :
                     "
                     "
                     "
                  } while ( condition ) ;

What is difference between while and do while?

As per syntax property , in case of while statement first of condition is checked and and if the condition is true then execution of number of statement written within pair of { } takes place. If the condition is false the execution of number of statement written under the condition of while loop is not possible.
                     On the other hand in case of do while statement first of all execution of number of statement takes place and after executing once the condition is checked further if condition is true then execution of statement within do while is possible otherwise execution of statement is not possible.
              It means in case of while statement the execution of statement or number of statement is always condition dependent . 
                                                In case of do while statement the first execution is always condition independent. it is because of the syntax property of while and do while statement. 

Uses of do while in a program ::

#include <stdio.h>
void main ()
{
int a=1 ;
clrscr () ;
do
{
printf ("%d", a) ;
a++ ;
} while ( a <= 10 ) ;
getch () ;
}

Friday, 27 July 2018

Program to check strong number in C

What is strong number ?

A Number is called strong number if the sum of factorial of it digits is equal to itself.

Example :  145

Program to check strong number ::

#include <stdio.h>
void main ()
{
int n , add = 0 , a , i , temp ;
clrscr () ;
printf ("\n Enter a number:") ;
scanf ("%d", &n) ;
temp = n ;
while ( n )
{
i = 1 ; 
fact = 1 ;
a = n % 10 ;
while ( i <= a )
{
fact = fact * i ;
i++ ;
}
add = add + fact ;
n = n / 10 ;
}
if (  add = temp )
{
printf ("\n it is strong number:") ;
else
printf ("\n it is not strong number:") ;
getch() ;
}


This is a program to check a given number is strong number or not . Here we have to input a number from the user and then apply condition for strong number.

Output screen ::

Enter a number :: 45
It is not strong number
Enter a number :: 145
It is strong number 


Tuesday, 24 July 2018

C Program to check given number is Armstrong number or not

What are Armstrong number ?

An armstrong number is a number which is equal to the sum of digit rise to the power of total number of digit in the number.

Example :: 3 , 370 , 153

Program to check armstrong number ::

#include <stdio.h>
#include <math.h>
void main ()
{
int n , add = 0 , rem = 0 , temp , cube , pow ;
clrscr () ;
printf ('\n  Enter a number:") ;
scanf ("%d", &n) ;
temp = n ;
while ( n != 0 )
{
rem = n % 10 ;
cube = pow ( rem , 3 ) ;
add = add + cube ;
n = n / 10 ;
}
if ( add == temp )
printf ("\n Number is armstrong:") ;
else
printf ("\n Number is not armstrong:") ;
getch ()
}

OUTPUT :: (A)  Enter a number :-
                      Enter number is 153
                      Number is armstrong

                    (B) Enter a number :-
                        Enter number is 29
                       Number is not armstrong

C program of armstrong using concept of FUNCTION ::

When a program grow in size and complexity then we break that program into small module . These module are known as the function . Function increases the understandably of a program logic using function module independent logic can be defined .

Function provides the concept of modularity in a program which is best method to divide a complex program .  
#include <stdio.h>
int root ( int , int ) ;
void main ()
{
int n , add = 0 , temp , rem , word = 0 ;
clrscr () ;
printf ("\n Enter a number:") ;
scanf ("%d", &n) ;
temp = n ;
while ( temp != 0 )
{
word++ :
temp = temp / 10 ;
}
temp = n ;
while ( temp != 0 )
{
rem = temp % 10 ;
add = add + root ( remainder , digit ) ;
temp = temp / 10 ;
}
if ( n == add )
printf ("\n armstrong number:") ;
else
printf ("\n not armstrong number:") ;
}
int root ( int n , int r ) 
{
int c , p = 1 ;
for ( c = 1 : c <= r : c++ )
p = p * n ;
return p ; 
}
getch () ;
}







                    



Featured post

Kalgudi Interview Round!!

Those who cleared the second round of kalgudi got an email from exceller to complete their Kalgudi Interview Round. So here we will discuss ...