Pages

Friday, 14 May 2021

Programs in turboc3 : Strong ,Armstrong,Perfect ,Magic, Palindrome Number including min and max ranges &Reverse,Fibonacci series

/ * Strong Number * /

Definition :A number is called strong number if sum of it's factorials of it's digits is equal to number itself. 

Example : 145

           = 1! + 4! + 5! 

           = 1+24+120 

           =145

Program :
 #include<stdio.h>
 #include<conio.h>
  void main( ) 
  {
      int n, i, f, r, sum=0, tmp;
      clrscr( ) ;
      printf(" Enter any number ") ;
      scanf("%d", &n) ;
       tmp=n;
       while(n) or while (n! =0) 
        {
            i=1 ;
            f=1 ;
            r=n%10 ;
             while (i<=r) 
              {
                  f=f * i ;
                   i ++ ;
              }
                sum=sum+f ;
                   n=n/10 ;
         }
            if(sum==tmp) 
              printf("\n%d is Strong number ", tmp) ;
            else
               printf(“\n%d is not a strong number", tmp) ;
     getch ( );
   }


/ * Strong number range in between min &max */ 

#include<stdio.h>
#include<conio.h>
 void main( )
 {
    long int min, max, n, i, f, r, sum,tmp;
    clrscr( ) ;
    printf(" \nEnter minimum number ") ;
    scanf("%ld",&min) ;
    printf("\n Enter maximum number ") ;
    scanf("%ld",&max) ;
    printf("\nStrong numbers are:") ;
      for(n=min;n<=max;n++)
{
tmp=n;
sum=0;
while (tmp)
   {
     i=1 ;
     f=1 ;
     r=tmp%10 ;
     while (i<=r)
       {
  f=f * i ;
   i ++ ;
       }
    sum=sum+f ;
    tmp=tmp/10 ;
  }
if(sum==n)
  printf("\n%ld is Strong number", n);
       }
       getch ( );
   }

Output:

             1        2      145      40585


/*   Armstrong Number */

Definition:- Those number whose sum of its digits to power of number of its digits is equal to that number is known as Armstrong number.

      For example : 153,  370, 371

      Total  Digits in 153 is  3

      And 13 +53+33 =1+125+27=153

      For example : 1634

      Total Digits in 1634 is 4

       And 14+64+34+44=1+1296+81+256 =1634

Program :- 
    #include<stdio.h>
    #include<conio.h>
    void main( )
    {
       int  n,r, sum=0,tmp ;
       clrscr( ) ;
       printf(“Enter any number:”) ;
       scanf(“%d”,&n) ;
       tmp=n ;
       while(n)
        {
          r=n%10 ;
          n=n/10 ;
         sum=sum+(r*r*r*) ;
        }
      if(sum==tmp) 
        printf(“\n%d  is an Armstrong number”,tmp) ;
      else
         printf(“\n%d is not an Armstrong number”,tmp) ;
     getch( ) ;
   }


/* Min & Max range of an Armstrong number */
     #include<stdio.h>
     #include<conio.h>
     void main( )
     {
       int min,max,n,sum=0,r,tmp ;
       clrscr( ) ;
       printf(“\nEnter min number:”) ;
       scanf(“%d”,&min) ;
       printf(“\nEnter max number:”) ;
       scanf(“%d”,&max) ;
       printf(“\n Armstrong number is the given range :”) ;
       for( n=min ; n<=max ; n++)
        {
          tmp=n ; 
          sum=0 ;
          while(tmp!=0)
           { 
             r=tmp%10 ;
             tmp=tmp/10 ;
             sum=sum+(r*r*r) ;
           }
          if(sum==n)
            printf(“\n%d is  Armstrong number”,n) ;
       }
     getch( ) ;
  }


/*Perfect Number*/

Definition:-Perfect number is a positive number whose sum of all positive divisors excluding that number is equal to that number is known as Perfect number.

For example: 6 is a perfect number , since divisor of 6 are 1,2 and 3. Sum of it's divisor is 1+2+3= 6

28=> 1,2,4,7,14        1+2+4+7+14 = 28

496,

8128.

Program :-
#include<stdio.h>
#include<conio.h>
void main( )
{
   int  n,i=1,sum=0,tmp ;
   clrscr( ) ;
   printf(“Enter any number:”) ;
   scanf(“%d”,&n) ;
   tmp=n ;
   while(i<n)
     {
       if (n%i ==0) 
        sum=sum+i ;
      i++;
     }
   if(sum==tmp)
     printf(“\n%d  is an Perfect number”,tmp);
   else
      printf(“\n%d is not an Perfect number”,tmp);
  getch();
}


/*Min & Max  range of an Perfect number*/
#include<stdio.h>
#include<conio.h>
void main()
{
   int min,max,n,sum=0,i,tmp ;
   clrscr() ;
   printf(“\nEnter min number:”) ;
   scanf(“%d”,&min) ;
   printf(“\nEnter max number:”) ;
   scanf(“%d”,&max) ;
   printf(“\nPerfect number is the given range:”) ;
   for(n=min;n<=max;n++)
     {
       i=1 ;
      sum=0 ;
      while(i<n)
        {
          if(n%i==0) 
            sum =sum+i ;
          i++ ;
        }
      if (sum ==n) 
        printf("\n%d is Perfect number”, n) ;
    }
  getch ( ) ;
}


/*Prime number*/

Definition:- A natural number greater than one has not any other divisors except 1 and itself.In other word we can say which has only two divisors 1 and number itself.

For example:- 5 divisiors are 1 and 5.

#include<stdio.h>
#include<conio.h>
void main( )
{
  int n,i=1,count=0 ;
  clrscr( ) ;
  printf("Enter any number") ;
  scanf("%d",&n) ;
  while(i <= n)
   {
      if( (n%i==0) && (n%n==0) )
      count++ ;
      i++ ;
   }
  if(count==2)
       printf("\n%d is a Prime number",n) ;
 else
       printf("%d is not a prime number",n) ;
 getch( ) ;
}


/*Prime number range in between min and max*/
#include<stdio.h>
#include<conio.h>
void main()
{
  int min,max,n,i,count;
  clrscr( );
  printf("\nEnter min number");
  scanf("%d",&min);
  printf("\nEnter max number");
  scanf("%d",&max);
  printf("\nPrime number in the given range:");
  for(n=min ; n<=max ; n++)
    {  
       i=1;
       count=0;
       while(i<=n)
          {
            if( (n%i==0) && (n%n==0) )
             count++;
            i++;
          }
       if(count == 2)
          pf("\n%d is Prime number”,n);
    }
 getch( );
}

/*Magic number*/

Definition:-A number is said to be a magic number if the reverse of the square of the number is equal to the given number reverse and its square.


For eg: (12) square =144 reverse of 144 is 441
            (21) square= 441

             (13) square=169 reverse of 169 is 961
             (31) square=691


Program:-
#include<stdio.h>
#include<conio.h>
void main ()
{
  int , i ,j,k,n ;
  clrscr( ) ;
  printf(“Enter  any  number”) ;
  scanf(“%d”, &n) ;
  while (i>n)
   {
     i=n*n ;
     j=0 ;
    while(i>0)
      {
         j=j * 10 + i % 10 ;
         i = i / 10 ;
      }
     i=n ;
     k=0 ;
     while(i>0)
       {
         k=k*10+i%10 ;
         i=i/10 ;
       }
     if(k*k==j)
       printf (“\nMagic  number”) ;
     else
        printf (“\nNot a Magic  number” ) ;
   }
 getch();
}


/*Magic number range in between min and max*/
#include<stdio.h>
#include<conio.h>
void main ( )
{
   int i,j,k,n,min,max  ;
   clrsccr() ;
   printf (“\nEnter min number “) ;
   scanf(“%d”,&min );
   printf (“\nEnter max number” ) ;
   scanf(“%d”,&max) ;
   for (n=min;  n<=max  ; n++)
    {
      i=n*n ;
     j=0 ;
    while(i>0)
      {
         j=j * 10 + i % 10 ;
         i = i / 10 ;
      }
     i=n ;
     k=0 ;
     while(i>0)
       {
         k=k*10+i%10 ;
         i=i/10 ;
       }
     if (k * k == j)
         printf (“\n%d  is a Magic number” , n );
    } 
  getch() ;
}


/*Palindrome number */

Definition: A number is caller palindrome number if it remain some when it's digits are reversed

For Example   : 121 is palindrome number, when we will increase ln digit it will remain same number i. e  121

Program :- 
#include <stdio.h>
#include <conio.h>
void main( ) 
{
  int n, r, tmp, sum=0 ;
  clrscr( ) ;
  printf(" Enter any number ") ;
  scanf("%d ", &n) ;
  tmp=n ;
  while (tmp) 
   {
     r=n%10 ;
     r=n/10 ;  
    sum= sum * 10 +r ;
   }
  if (sum ==tmp) 
       printf("\n%d is a Palindrome number ", tmp) ;
  else
       printf("\n%d is not a Palindrome number ",) ;
  getch( ) ;
 }


/* Palindrome number range in between min and max */
#include<stdio.h>
#include<conio.h>
void main ( )

  int  min, max, n, r,sum=0, tmp ;
  clrscr( ) ;
  printf ("\nEnter min number: ") ;
  scanf(“%d”,&min) ;
  printf(“\nEnter max number:”) ;
  scanf ("%d", &max) ;
  printf (" \nPalindrome number in the given range ") ; 
  for ( n=min, n<=max; n++)
   { 
      tmp=n ;
      sum=0 ;
      while (tmp) 
        {
           r=tmp%10 ;
           tmp=tmp/10 ;
           sum=sum * 10 + r ;
         }
       if (sum==n) 
          printf ("\n%d is Palindrome number ", n) ;
    }
  getch( ) ;
 }


/* Reverse the given number & sum */
#include<stdio.h>
#include<conio.h>
void main( )
{
  int n,r,tmp,rev=0,sum=0;
  clrscr( );
  printf("Enter any number");
  scanf("%d",&n);
  tmp=n;
  while(n)
     {
        r=n%10;
        n=n/10;
        rev=rev*10+r;
        sum=sum+r;
      }
  printf("\n%d Reverse number is %d:",tmp,rev);
  printf("\n sum of the reverse number is %d:",sum);
  getch( );
}


/*Factorial of a given number*/
 #include<stdio.h>
 #include<conio.h>
 void main( )
 {
   int n,f=1,tmp;
   clrscr( );
   printf("Take any number");
   scanf("%d",&n);
    tmp=n;
    while(n)
      {
        f=f*n;
        n--;
      }
      printf("\n%d factorial is %d",tmp,f);
   getch( );
  }

output :- Enter any number : 4

              4 factorial is      24 


/*Write a ‘C’ program to illustrate Fibonacci series*/
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
  int n;i,past=0,present=1,future;
  clrscr();
  printf(“enter any number: “);
  scanf(“%d”,&n);
  printf(“%d%d”,past,present);
  future=past+present;
  printf(“%d”,future);
  for(i=0;i<n-3;i++)
    {
       past=present;
       present=future;
       future = past + present;
       printf("\t %d", future );
    }
  getch();
}


Input: n=5


Output:        0          1         1

                 Past      present      future

                  0                 1           1               2

                           Past       present      future

                   0               1              1         2           3

                                                       Past      present     future


Saturday, 1 May 2021

CONTROL STRUCTURES --Case control structures

1)”Break” Statement:

When a break statement appears in a loop or in switch control structure. A break statement inside the body of a loop breaks completely out of the loop no more instructions in the body of the loop are executed. It can be used within for, while, switch statements. The break statement can be written as ‘ break; ’ without any expression (or) statements.

2)”Continue” Statement:

The continue statement just skips any statement after it on that iteration of the loop. The current iteration of the loop is terminated and the loop statement is executed again, as if the last instruction of the loop body has been reached. The continue statement is written as ‘continue;‘ without any expression (or) statement.



Write a ‘C’ program to illustrate “continue” statement.
#include<stdio.h>
#include<conio.h>
void main()
{
  int n,i;
  clrscr();
  printf(“enter any number: “);
  scanf(“%d”,&n);
  for(i=1;i<=10;i++)
   {
    if(n==i)
      continue;
    else
      printf(“%d”,i);
  }
getch();
}
Output- enter any number: 5
1 2 3 4 6 7 8 9 10

3)”Goto” statement:
These statements are classified into two types. They are,
 1. Forward jump.
 2. Backward jump.

Write a ‘C’ program to illustrate Forward jump.
#include<stdio.h>
#include<conio.h>
void main()
{
int n;
clrscr();
printf(“Start of the program”);
goto label;
label:
printf(“\n Entered into the inside label”);
printf(“\n End of the program”);
getch();
}
Output: 
Start of the program
Entered into the inside label
End of the program

Write a ‘C’ program to illustrate Backward jump:
#include<stdio.h>
#include<conio.h>
void main()
{
int n;
clrscr();
printf(“\nStart of the program”);
label:
printf(“\n I am inside of the label”);
goto label;
printf(“\n End of the program”);
getch();
}

Output: 
I am inside of the label will be printed infinite times 

CONTROL STRUCTURES --Loop control structures

These are also called as iterative or repetitive control structures

Loop :Some set of statements execute multiple time loops are of 2 types;

  1.  Entry controlled Loop Structures
  2.  Exit controlled Loop Structures

1. Entry controlled Loop Structure :-

These Loops are also called top tested loops. In this the condition  is checked before entering into the body of the loop & the body of the loop is executed until the condition is false



2. Exit controlled Loop Structure :-

These loops are also called bottom tested loop. In this first body of the  loop is executed, then the condition is evaluated, the body of the loop is executed until the condition is false 

 

Note:

  • While for are called entry Control Loop Structure
  • Do while is called exit Control Loop Structure

1) "While" Statement:-

Syntax:

while ( condition)

{

body of the loop

}

/* Write a program to print first 10 numbers using for statement */ 

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

2) "Do  while " Statement :-

Syntax : 

do

{

body of the loop

}

while ( condition) ;

/* Write a program to print  first  10 numbers using do while statement. */

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

3) "for" Statement:-

for (initialization; condition; Inc / dec)

{

body of the loop

}

// Write a program to print first 10 numbers using for statement.

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

/* Write a program to print first 10 numbers in reverse order using while statement.*/

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

/*Write a program to print first 10 numbers in reverse order using do while statement.*/

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

/* Write a program to print first 10 numbers in reverse  order
using for statement.*/
# include<stdio.h>
#include<conio.h>
void main ( )
int n;
clrscr( ) ;
for (n= 10; n>=1; n- - )
{
printf("%d", n) ;
}
getch( ) ;
}

// Write a 'C' program to skip a no. of given  n numbers.
#include<stdio.h>
#include<conio.h>
void main ( )
{
intn,i=1;
clrecr ( ) ;
printf(" enter any number ") ;
scanf("%d", &n) ;
while (i<=10)
{
if ( n == I)
printf ("\n") ;
else
printf ("%d", I) ;
i++
}
getch( ) ;
}

Do  while
# include<stdio.h>
#include<conio.h>
void main( )
{
int n, i=1;
clrscr( ) ;
do
{
printf (" enter any number ") ;
scanf("%d", &n) ;
}
do
{
if (n ==i)
printf ("\n")
else
printf ("%d", i);
i++ ;
}
while (i<=10) ;
getch( ) ;
}

For
#include<stdio.h>
#include<conio.h>
void main (  )
{
int n, i=1;
clrscr( ) ;
pf("enter any number") ;
sf("%d", &n) ;
for(i=1, i<10, i++) 
 {
if(n==i) 
pf("\n") 
 }
else
 {
pf("%d", n);
 }
getch ();
}

Write a program to print 4 th table
#include<stdio.h>
#include<conio.h>
void man () 
{
int n,i=1, x;
pf("enter any number ") ;
sf("%d", & n) ;
while (I<=10) 
 {
 x=n*i;
pf("\n%d*%d=%d", n, i, x) ;
i++;
 }
getch () ;
}

Do while
#include <stdio.h>
#include<conio.h>
Void main()
{
int n, i=1, x;
Clrscr() ;
pf(" enter any number") 
sf("%d", &n) ;
do
  {
  x=n*i;
pf("\n%d*%d=%d", n, i, x) ;
i++; 
}while (i<=10) ;
getch () ;
}

for:
#include <stdio.h>
#include<conio.h>
void main () 
{
int n, i=1.x;
Clrscr() ;
pf("enter any number ") ;
sf("%d", &n) ;
for(i=1, i<=10;i+++) 
  {
  x=n*i; 
pf("\n%d*%d=%d", n, i,x);
  }
getch () ;
}

Write a 'c' program to check whether the given no is a palindrome Or not
#include<stdio.h>
#include<conio.h>
void main () 
{
int n, tmp, r, sum=0
Clrscr
pf(" enter any number ") ;
sf("%d", &n) ;
tmp=n;
while (n) 
{
  r=n%10;
  n=n/10;
  Sum=sum*10+r;
  }
if(sum=tmp) 
  {
pf("%d is a palindrome", tmp) ;
  }
else
  {
pf("%d is not a palindrome", tmp) ;
  }
getch () ;
}

Write a 'c' program to find the factorial of a given number
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main () 
{
int n, f=1;
clrscr() ;
pf("enter any number ") ;
sf("%d", &n) ;
while (n) 
  {
  f=f*n;
n--;
  }
pf("%d factorial is%d", n, f) ;
getch () ;
}

Do while
#include <stdio.h>
#include <conio.h>
#include <math.h>
void main () 
{
int n, f=1;
clrscr() ;
pf(" enter any number ") ;
sf("%d" &n) ;
do
  {
  n=f*n;
  n--;
  }while(n);
pf("%d factorial is%d", n, f) ;
getch () ;
}

For
#include <stdio.h>
#include <conio.h>
#include <math.h>
void main () 
int n, f=1;
clrscr () ;
pf("enter any number ") ;
sf("%d, &n) ;
for(" n! =0, n--") 
  {
  f=f*n;
  } 
pf("%d factorial is%d", n, f) ;
getch () ;
}

Write a c program to check whether the given number is prime or not
#include <stdio.h>
#include <math.h>
#include <conio.h>
void main () 
{
int i=1, n, a=0;
Clrscr();
pf("enter any number ") ;
sf("%d", &n);
while (i<=n) 
 {
if( (n%i) ==0&&(n%n==0) ) 
a++;
i++;
  }
if (a==2) 
  {
pf(%d is a prime number", n) ;
  }
else
  { 
pf(" %d is not a prime number ", n) ;
  }
getch () ;
}

Do while
#include <stdio.h>
#include <math.h>
#include <conio.h>
void main()
{
int i=1, n,a=0;
clrscr() ;
pf(" enter any number ") ;
sf("%d", &n) ;
do
  {
if( (n%i==0) &&(n%n==o) ) 
a++;
i++;
}while (i<=n); 
if (a=2) 
  {
pf(%d is a prime number", n) ;
  }
else
  {
pf(%d is not a prime number ;n) ;
  }
getch () ;
}

For
#include <stdio.h>
#include <conio.h>
#include <math.h>
void main
{
int n, i=1, a=0;
Clrscr;
pf("enter any number ") ;
sf("%d", &n) ;
for(i=1, i<=n, i++) 
  {
if ( (n%i==0) &&(n%n==0)) 
    {
    a++
    }
  }
if(a==2) 
  {
pf(" %d is a prime number ", n) ;
  }
else
  {
pf("%d is not a prime number", n) ;
  }
getch () ;
}

Write a c program to check whether the given number is an arm strong number Or not
#include <stdio.h>
#include <conio.h>
#include <math.h>
voidmain () 
{
int n, tmp, r, sum=0;
clrscr() ;
pf(" enter any number ") ;
sf("%d" , &n) ;
tmp=n
while (n) or (n!=0) 
  {
  r=n%10
  n=n/10
sum=sum+(r*r*r);
  }
if(sum=tmp) 
  {
pf("%d is an arm strong number", tmp) ;
  }
else
  {
getch () ;
}

Write a ‘C’ program to check whether the entered number is a perfect or not.

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int i=1,n,sum=0,tmp;
clrscr();
printf("enter any number:");
scanf("%d",&n);
tmp=n;
while(i<n)
{
if(n%i == 0)
{
sum =sum+i;
}
i++;
}
if(tmp == sum)
{
printf("%d is a perfect number",tmp);
}
else
{
printf("%d is not a perfect number",tmp);
}
getch();
}

Write a ‘C’ program to find the sum of two given numbers without using ‘+’ sign.
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int a,b,r;
clrscr();
printf(“enter two numbers: “);
scanf(“%d%d”,&a&b);
r=a - ~ b – 1;
printf(“sum is %d”,r);
getch();
}

Write a ‘C’ program to find the difference between two numbers without using ‘ – ‘ sign.
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int a,b,r;
clrscr();
printf(“enter any two numbers: “);
scanf(“%d”,&n);
r = a + ~ b + 1;
printf(“Difference is %d”,r);
getch();
}

Write a ‘C’ program to check whether the entered number is strong number or not.
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int n,f,r,I,tmp,sum = 0;
clrscr();
printf(“enter any number: “);
scanf(“%d”,&n);
tmp = n;
while(n) or (n!=0)
 {  
  f=1,i=1;
  r=n%10;
while(i<=r)
   {
    f=f*I;
i++;
   }
sum=sum+f;
   n=n/10;
  }
if(sum==tmp)
printf(“%d is a strong number“,tmp);
else
printf(“%d is not a strong number”,tmp);
getch();
}

Write a ‘C’ program to illustrate Fibonacci series.

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
intn;i,past=0,present=1,future;
clrscr();
printf(“enter any number: “);
scanf(“%d”,&n);
printf(“%d%d”,past,present);
future=past+present;
printf(“%d”,future);
for(i=0;i<n-3;i++)
 {
past=present;
present=future;
future=past+present;
printf(“%d”,future);
 }
getch();
}

Input: n=5
Output: 0 1 1
          Past           present          future
             0 1            1 2
                   Past           present          future
0 1    1               2             3
                                              Past           present          future

CONTROL STRUCTURES --Decision control structures

Till now we have been dealing with sequential execution i.e. every program in the statement could be executed sequentially ( i.e. A statement can be only executed only after the execution of its previous statements )

However many times we need to control the sequence of execution of instructions for that purpose we have to use control structures.

Control structures in ' c ' are classified into:          


DECISION CONTROL STRUCTURES: 
1)" If " statement :
Syntax 1: if ( condition )        
                    Statement ;                single statement body     

Syntax 2: if ( condition )          
                    {                                           
                    Statement ;
                       .......                    compound statement body
                    Statement ;
                     } 
Example :
/* Program to illustrate If statement */
#include<stdio.h>
#include<conio.h>
void main(  )
{
 int a,b,c,big ;
 clrscr (  )
 printf(" enter a,b,c values ");
 scanf(" %d%d%d",&a,&b,&c );
 big = a;
 if ( b > big )
 big = b;
 if ( c > big )
 big = c;
 printf("The largest of 3 numbers is %d ",big );
 getch( )
}

2)" If Else " statement :
syntax 1: if (condition)            
                  Statement;                Single statement
                   else                                           body
                  Statement;
syntax 2: if (condition)
                  { 
                   Statement 1;                  Compound statement 
                   Statement 2;.                               body 
                  }
                  else
                  {
                   Statement 3;
                   Statement 4;
                  }
Example :
/*Program to illustrate if else statement*/
#include<stdio.h>
#include<conio.h>
void main( )
{
int n;
clrscr( );
printf(" enter any number ");
scanf("%d",n);
     if (n%2==0)
     {
      printf(" the entered num is even number ");
      }
      else 
      {
      printf(" the entered num is odd number ");
      }
       getch( );
  }

Example :
---> Write a ' c ' program to illustrate whether the given number is zero or non-zero. 
/*Program to illustrate if else statement*/
#include<stdio.h>
#include<conio.h>
void main( )
{
int n;
clrscr( );
printf("enter any number");
scanf("%d",&n);
   if ( n == 0 )
   {
    printf(" the entered number is zero ");
   }
    else 
   {
     printf("the entered number is non-zero");
    }
 getch( );
 }

Example :
--> Write a ' c ' program to check whether the entered number is +ve or -ve.
/*Program to illustrate if else statement*/
#include<stdio.h>
#include<conio.h>
void main( )
{
int n;
clrscr( );
printf(" enter any number ");
scanf(" %d",& n);
   if (n>0)
   {
     printf(" entered num is +ve ");
    }

      else
     {
       pf(" entered num is -ve ");
     } 
 getch( )
 }

3) "Nested If Else" statement :
syntax:  if ( condition 1 )
              { - open
                 if ( condition 2 )
                  { - open 1
                   statement ;       1
                   statement ;
                   } - close 1
                 else
                  { - open 2
                   statement ;        2
                   statement ;
                  } - close 2
              } - close
          else 
                  { - open
                   statement ;        3
                   statement ;
                } - close
          }

Example :
Write a Program to illustrate nested if else
#include<stdio.h>
#include<conio.h>
void main()
{
int age,exp;
clrscr();
printf("enter age");
scanf("%d",&age);
printf("enter experience");
scanf("%d",&exp);
     if((age>=23) && (age < 30))
     {
if (exp>=2)
{
printf("candidate is with 2 years experience");
}
else
{
printf("candidate is not having 2 years experience");
}
     }
    else
    {
      printf("age limit should be in the specified limit");
    }
getch();
}

Example :
Write a Program to illustrate nested if else
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr( );
printf("enter values for a,b,c");
scanf("%d%d%d",&a,&b,&c);
 if((a>b) && (a>c))
  {
    if(a>c)
      printf("%d is the largest number", a);
    else
      printf("%d is the largest number", c);
  }
  else
  {
    if (b>c)
      printf("%d is the largest number", b);
    else
      printf("%d is the largest number", c);
  }
 getch();
 }


4)"Else If Ladder" statement:
 Syntax:  if ( condition 1)
                   Statement 1;
               else if ( condition 2)
                   Statement 2;
               else if ( condition 3)
                   Statement 3;
                   ......
                   Default 
                   Statement n;


Example :
Write a program to illustrate else if ladder statement.
/*Program to illustrate else if ladder statement*/
#include<stdio.h>
#include<conio.h>
void main( )
{
int m;
clrscr( );
printf("enter the marks :");
scanf("%d",&m);
if((m>90)&&(m<100))
printf("mark grade is O");
else if((m>80)&&(m<90))
printf("mark grade is A");
else if((m>70)&&(m<80))
printf("mark grade is B");
else if((m>60)&&(m<70))
printf("mark grade is C");
else if((m>50)&&(m<60))
printf("mark grade is D");
else if((m>40)&&(m<50))
printf("mark grade is E");
else
printf(" FAIL " );
getch( );
}

5) SWITCH Statement:



Example :
Write a program to illustrate Switch case statement. 
/* Program to illustrate Switch case statement */
#include<stdio.h>
void main ( ) 
 {
int n;
clrscr ( ) ;
printf("enter the number") ;
scanf("%d", &n) ;
switch (n) 
 {
case 1: printf (" Monday ");
break;
case 2 : printf (" Tuesday ");
break;
case 3 : printf (" Wednesday ");
break;
case 4: printf (" Thursday ");
break;
case 5 : printf (" Friday ");
break;
case 6: printf (" Saturday ");
break;
case 7 : printf (" Sunday");
break;
default :printf("the entered number is not in specified limit") ;
break;
 }
getch( ) ;
}

Example:
=> Write a program to illustrate switch case statement 
/* Program to illustrate switch case statement*/
#include<stdio.h>
void main ( ) 
{
int n;
clrscr( ) ;
printf (" enter the alphabet") ;
scanf(" %d", &n) ;
switch (n) 
{
case 1: printf ("A") ;
break;
case 2: printf("E") ;
break;
case 3: printf("I") ;
break;
case 4: printf ("O") ;
break;
case 5 :printf("U") ;
break;
default :printf (" the entered no is constant ") ;
 }
getch( );
}

Wednesday, 31 March 2021

Basic structure of a “C” program:-

Documentation Section

/* Program for addition of 2 numbers */

Link Section (or) 
Preprocessor Directives
#include<stdio.h>
#include<conio.h>

Definition section

#define pf printf

Global Declaration Section

 

Main() Function Section

          

           main()

           {

              Declaration part;

              Execution part;

           }

Main() Function Section

 

    main()

     {

       int a,b,c;

       clrscr();

       printf(“Enter a and b values:”);

       scanf(“%d%d”,&a,&b);

       c=a+b;

       printf(“sum is %d”,c);

    }

Sub Program Section (or)

User-defined function Section

Function 1

Function 2

-----------------

Function n

 


Write a “C” program to print a message.

Method 1:-
/*print a message 
  of your choice */
#include<stdio.h>
#define pf printf
main()
{
pf(“welcome to “C”);
}
Method 2:-
//print a message
#include<stdio.h>
#define pf printf
Void main()
{
pf (“welcome to “C”);
}
Method 3:-
//print a message
#include<stdio.h>
#define pf printf
int main()
{
Pf(“welcome to “C”);
return 0;
}


/* Program to illustrate the different data types and there number of bytes occupied */
#include<stdio.h>
#include<conio.h>
void main()
{
 int a=22;
 char b[]="raj kumar";
 char c='f';
 float d=143.5;
 double e=1234.54;
 clrscr();
 printf("\n the string is : %s",b);
 printf("\n the character is :%c",c);
 printf("\n the number of bytes occupied by char is :%d",sizeof(c));
 printf("\n");
 printf("\n the integer is %d",a);
 printf("\n the number of bytes occupied by int is :%d",sizeof(a));
 printf("\n");
 printf("\n the float value is %f",d);
 printf("\n the number of bytes occupied by flaot is :%d",sizeof(d));
 printf("\n");
 printf("\n the double value is %lf",e);
 printf("\n the number of bytes occupied by double is :%d",sizeof(e));
 getch();
}

output:


//Program to illustrate the Local and Global variables 

#include<stdio.h>
#include<conio.h>
int global;
void main()
{
 int local;
 clrscr();
 printf("\n the local variable is : %d",local);
 printf("\n the global variable is : %d",global);
 getch();
}

output:

Programs in turboc3 : Files

File Handling in C File Handling concept in C language is used for store a data permanently in computer. Using this concept we can store our...