Saturday 26 December 2015

Program to Calculate the Sum of Digits of a number

//Program to Calculate the Sum of Digits of a number

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

void main()
{
      int  num, sum=0, temp; /* 'sum' is  used to store the sum of digits of a number and 'temp' to store the number for calculations*/
      int rem=0; // used to store the remainder of the number

      clrscr(); // for clearing the console screen

      printf("Enter the number : ");
      scanf("%d",&num);
      temp = num; //copies the value of 'number' in 'temp'

// logic for calculating the sum of digits of number

      while(temp != 0) // checking whether 'temp' is equal to 0 or not
      {
          rem=temp%10; // extracting the last digit by dividing it with 10
     sum=sum+rem; /* calculating the sum of digits of the number - sum is initialized with zero so that no garbage value is added */
     temp=temp/10; // dividing the number by 10 to break it in parts.
      } // end of while loop

      printf("The sum of the digits of a number %d = %d",number,sum); /* printing the sum of digits of number */

      getch(); // for holding the console screen
}

Friday 25 December 2015

Program to find largest number from the three numbers entered by the user

/* Program to find largest number from the three numbers entered by the user. */


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

void main()
{
      int num1, num2, num3, max;

      clrscr(); //for clearing the console screen

//Taking three numbers as input

      printf("Enter three numbers: ");
      scanf("%d %d%d", &num1, &num2, &num3);

//Logic for finding largest numbers from three entered by user

      if (num1>=num2 && num1>=num3) // comparison of first number with second and third number
      {    
            max=num1;
      }
      else if (num2>=num1 && num2>=num3)// comparison of second number with first and third number
      {
            max=num2;
      }
      else
      {
           max=num3; /*since other two numbers are smaller then this third number so it will be largest */
      }
       printf("Largest number = %d", max);
      getch(); //for holding the console screen
}

Sunday 20 December 2015

Program to find G.C.D. (Greatest Common Divisor) / H.C.F. (Highest Common Factor) of two numbers using conditional programming.

/* Program to find G.C.D. (Greatest Common Divisor) / H.C.F. (Highest Common Factor) of two numbers using conditional programming. */
#include <stdio.h>
#include <conio.h>

void main()
{
      int num1, num2, min,i;
      clrscr(); //for clearing the console screen

//Taking input
      printf("Enter two integers: ");
      scanf("%d %d", &num1, &num2);

//Logic for finding GCD/HCF
  min=(num1>num2)?num2:num1; // minimum value is stored in variable min

      for(i=min; i>=1; i--)
      {
            if(num1%i==0 && num2%i==0)
            {
                  printf("HCF of %d and %d is %d", num1, num2,i);
                  break;
            }
      }

      getch(); //for holding the console screen
}

Program to find GCD (Greatest Common Divisor) / HCF (Highest Common Factor) of two numbers.

//Program to find G.C.D. (Greatest Common Divisor) / H.C.F. (Highest Common Factor) of two numbers.
#include <stdio.h>
#include <conio.h>

void main()
{
      int num1, num2, min,i;
      clrscr(); //for clearing the console screen

//Taking input
      printf("Enter two integers: ");
      scanf("%d %d", &num1, &num2);

//Logic for finding GCD/HCF
      if(num1>num2) //getting the minimum number
      {
            min=num2; //minimum value is stored in variable min 
      }
      else
      {
            min=num1; //minimum value is stored in variable min 
      }

      for(i=min; i>=1; i--)
      {
            if(num1%i==0 && num2%i==0)
            {
                  printf("HCF of %d and %d is %d", num1, num2,i);
                  break;
            }
      }

      getch(); //for holding the console screen
}

Saturday 19 December 2015

Program to find the Reverse of a number.

//Program to find the Reverse of a number.
#include<stdio.h>
#include<conio.h>

void main()
{
      int  number,reverse=0, temp; /* 'reverse' is  used to store the reverse of a number and 'temp' to store the number for calculations*/
      int rem=0; // used to store the reminder of the number

      clrscr(); // for clearing the console screen

      printf("Enter the number : ");
      scanf("%d",&number);
      temp = number; //copies the value of 'number' in 'temp'

// logic for finding the reverse of the number

      while(temp != 0) // checking whether 'temp' is equal to 0 or not
      {
          rem=temp%10; // extracting the last digit by dividing it with 10
     reverse=reverse*10+rem; // calculating the reverse of the number
     temp=temp/10; // dividing the number by 10 to break it in parts.
      } // end of while loop

      printf("The reverse of the number %d = %d",number,reverse); // printing the reversed number
      getch(); // for holding the console screen
}