Pages

Friday, 1 February 2019

PROGRAM TO FIND WHETHER THE GIVEN NUMBER IS PALINDROME OR NOT

PROGRAM TO CHECK THE GIVEN NUMBER IS PALINDROME OR NOT

// prg to illustrate given number is palindrome or not
#include<stdio.h>
void main()
{
 int n,r,rev=0,temp;
 printf("enter any number:");
 scanf("%d",&n);
 temp=n;
 while(n != 0)
  {
   r = n % 10;
   n = n / 10;
   rev=rev *10 +r;
  }
  if( temp == rev )
   printf("%d  is a palandrome \n",temp);
  else
   printf("%d  is not a palandrome \n",temp);
}   


output:


PRIME NUMBERS IN BETWEEN MIN AND MAX VALUES

PRIME NUMBER RANGE IN BETWEEN MIN AND MAX VALUES :

// program to illustrate prime numbers range in between min and max values
#include<stdio.h>
#define pf printf
#define sf scanf
void main()
{
 int min,max,n,i,count;
 pf("\nEnter min number:");
 sf("%d",&min);
 pf("\nEnter max number:");
 sf("%d",&max);
 pf("\n Prime numbers are:");
 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 a prime number",n);
 }
}

output:

PRIME NUMBER IN C PROGRAM

PRIME NUMBER:
                   A NATURAL NUMBER GREATER THAN ONE HAS ANY OTHER DIVISORS EXCEPT 1 AND ITSELF IN OTHER WORDS WE CAN SAY WHICH HAS ONLY TWO DIVISORS 1 AND NUMBER ITSELF 

FOR EXAMPLE: 5 DIVISORS ARE 1 AND 5 

FOR EXAMPLE : 7 DIVISORS ARE 1 AND 7

PROGRAM:
// program to illustrate prime number
#include<stdio.h> // header section
int count; // global section
void main()
{
 int n,i=1;
 printf("Enter any number:");
 scanf("%d",&n);
 while( i <= n )
  {
   if((n % i == 0) && (n % n == 0))
     count++;
   i++;
  }
 if (count == 2)
   printf("%d is a prime number",n);
 else
   printf("%d is not a prime number",n);
}


output:

PERFECT NUMBERS IN BETWEEN MIN AND MAX VALUES :


PERFECT NUMBERS RANGE IN BETWEEN MIN AND MAX  : 

// program to illustrate min and max range of perfect number
#include<stdio.h>  // header section
#define pf printf // define section
#define sf scanf // define section
void main()
{
 int sum,min,max,n,i,temp;
 pf("\nEnter min number:");
 sf("%d",&min);
 pf("\nEnter max number:");
 sf("%d",&max);
 pf("\n Perfect number are :");
 for(n=min;n<=max;n++)
  {
    i=1,sum=0;
    while(i < n)
     {
      if( n % i == 0 )
        sum=sum + i;
      i++;
     }
    if(sum == n)
      pf("\n%d",n);
  }
}

output:


PERFECT NUMBER PROGRAM IN C

PERFECT NUMBER :  
PERFECT NUMBER IS A POSITIVE NUMBER WHOSE SUM OF ALL POSITIVE DIVISORS EXCLUDING THAT NUMBER IS EQUAL TO THAT NUMBER


FOR EXAMPLE : 6 IS A PERFECT NUMBER SINCE DIVISOR OF 6 ARE 1,2,3. SUM OF ITS DIVISORS IS 1+ 2 + 3 = 6


FOR EXAMPLE : 28 IS A PERFECT NUMBER SINCE DIVISOR OF 28 ARE 1,2,4,7,14. SUM OF ITS DIVISORS IS 1+ 2 + 4 + 7 + 14 = 28

FOR EXAMPLE : 496 IS A PERFECT NUMBER  

FOR EXAMPLE : 8128 IS A PERFECT NUMBER 

PROGRAM:

// program to illustrate perfect number
#include<stdio.h>
void main()
{
 int n,i=1,sum=0,temp;
 printf("Enter any number:");
 scanf("%d",&n);
 temp=n;
 while (i < n)
  {
   if( n % i == 0 )
     sum =sum + i;
   i++;
  }
 if (sum == temp)
   printf("%d is a perfect number",temp);
 else
   printf("%d is not a perfect number",temp);
}

output:

  

Thursday, 24 January 2019

C PROGRAM TO SUBSTRACT TWO NUMBERS WITHOUT USING SUBSTRACTION SYMBOL

#include<stdio.h>

void main()
{

int a=8,b=7,r;

r = a + ~b + 1;

printf(" result is %d",r);


}

HOW TO DEBUG C PROGRAM USING - gdb

Debug c code program using gdb

A debugger is a program that runs other programs, allowing the user to exercise control over these programs, and to examine variables when problems arise.
GNU Debugger, which is also called gdb, is the most popular debugger for UNIX systems to debug C and C++ programs.

GNU Debugger helps you in getting information about the following:
  • If a core dump happened, then what statement or expression did the program crash on?
  • If an error occurs while executing a function, what line of the program contains the call to that function, and what are the parameters?
  • What are the values of program variables at a particular point during execution of the program?
  • What is the result of a particular expression in a program?

     
    GDB Debugs
  • GDB allows you to run the program up to a certain point, then stop and print out the values of certain variables at that point, or step through the program one line at a time and print out the values of each variable after executing each line
     
  • GDB uses a simple command line interface.

  • Even though GDB can help you in finding out memory leakage related bugs, but it is not a tool to detect memory leakages.
  • GDB cannot be used for programs that compile with errors and it does not help in fixing those errors.
compilation : 
step 1: The basic way of compiling garbage.c into an executable file called "garbage" is:
      gcc -o garbage garbage.c
  • If the program is compiled without errors, you can execute the program by typing "./garbage 
    gcc -g -o garbage garbage.c 
   
  • If you are interested about how the assembly code of garbage.c look like, you can also generate the assembly code by replacing the "-o garbage" option with "-S" as:   
     gcc -S garbage.c 

    Example  Program 1:
  •  // program to print n values using while loop
    #include<stdio.h>
    void main()
    {
    int i=1,n;
    printf("\n enter any value ");
    scanf("%d",&n);
    while(i<n)
    {
    printf("\n output : %d ",i);
    n++;
    }
    } 


    output :




  •  
 
 Example Program  2:
// Program to illustrate fibonocci series 
#include<stdio.h>
void main()
{
 int n,i,past=0,present=1,future;
 printf("enter any number:");
 scanf("%d",&n);
 printf("\n\t%d\t%d",past,present);
 future=past+present;
 printf("\t%d",future);
 for(i=0;i<n-3;i++)
  {
   past=present;
   present=future;
   future=past+present;
   printf("\t%d",future);
  }
}
 
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...