Showing posts with label Digit. Show all posts
Showing posts with label Digit. Show all posts

C Program To Find Reverse Of Any Digit Number

How to write a C Program To Find Reverse Of Any Digit Number in C Programming Language ?

Solution For C Program :

/*C Program To Find Reverse Of Any Digit Number.*/

/*It works good up to 9-digits.*/
#include<stdio.h>
#include<conio.h>
void main()
{
long int num,rem,rev=0,num1;
clrscr();
printf("Enter any digit number for reverse===>");
scanf("%ld",&num);
num1=num;
while(num1>0)
{
rem=num1%10;
num1/=10;
rev=rev*10+rem;
}
printf("Reverse of %ld is===>%ld",num,rev);
getch();
}


You may also learn these C Program/Code :

C Program To Swap Two Numbers Without Using Third Variable

C Program To Print Sum Of n Digit Number

How to write a C Program To Print Sum Of n Digit Number in C Programming Language ?


Solution For C Program  :

/*C Program To Print Sum Of n Digit Number.*/

/*Program works good upto 9-digit.*/
#include<stdio.h>
#include<conio.h>
void main()
{
long int n,rem,sum=0;
clrscr();
printf("Enter the number==>");
scanf("%ld",&n);
while(n>0)
{
rem=n%10;
n=n/10;
sum=sum+rem;
}
printf("%ld",sum);
getch();
}


You may also learn these C Program/Code :

C Program To Swap Two Numbers Without Using Third Variable



C Program To Add Digits Of Entered Number

How to write a C Program To Add Digits Of Entered Number in C Programming Language ?


Solution:

C Program To Add Digits Of Entered Number

#include<stdio.h>

main()
{
   int n, sum = 0, remainder;

   printf("Enter an integer\n");
   scanf("%d",&n);

   while( n != 0 )
   {
      remainder = n % 10;
      sum = sum + remainder;
      n = n / 10;
   }

   printf("Sum of digits of entered number = %d\n",sum);

   return 0;
}

C Program Anagrams

How to write a C Program Anagrams in C Programming Language ?

Considering a string will only contain lower-case alphabets. It can also be done for uppercase letters , digits , special characters etc.

Solution:

  1. #include<stdio.h>
  2. #include<stdbool.h>
  3. #include<string.h>
  4. bool isAnagram(char *str1,char *str2)
  5. {
  6.     int alphabetCount[26] = {0};
  7.     int i = 0;
  8.    
  9.     if(strlen(str1) != strlen(str2))
  10.         return false;
  11.    
  12.     for(i=0;str1[i]!='\0';++i)
  13.     {
  14.         ++alphabetCount[str1[i]-'a'];
  15.         --alphabetCount[str2[i]-'a'];
  16.     }
  17.    
  18.     for(i=0;i<26;++i)
  19.     {
  20.         if(alphabetCount[i])
  21.             return false;
  22.     }
  23.    
  24.     return true;
  25. }
  26.  
  27. int main(void)
  28. {
  29.    char string1[1001],string2[1001];
  30.    
  31.    /*
  32.      considering a string will only contain lower-case alphabets
  33.     It can also be done for uppercase letters , digits , special characters etc.
  34.     */
  35.    
  36.    gets(string1);
  37.    gets(string2);
  38.    
  39.    if(isAnagram(string1,string2))
  40.        printf("yes");
  41.    else
  42.        printf("no");
  43.    
  44.     return 0;
  45. }

C Program to Sum of The First and Last Digit Of 'n' Digit Number

How to write a C Program to Sum of The First and Last Digit Of  'n' Digit Number  in C Programming Language ?


  1. int main()
  2. {
  3. int num,len,last;
  4. printf("\nEnter n digit number \n");
  5. scanf("%d",&num);
  6. last=num%10;
  7. while(num>0)
  8. {
  9. num=num/10;
  10. len=len+1;
  11. }
  12. num=num/(pow(10,len-1));
  13. num=num%10;
  14. printf("the result is %d",last+num);
  15. getch();
  16. return 0;
  17.  
  18. }

C Program to Sum of First and Last Digits of a Four-Digit number

How to write a C Program to Sum of First and Last Digits of a Four-Digit number in C Programming Language ?


Solution:

This program is based on the "sum of digits" program discussed previously.
This program also has some usage of the Modulus Operator.

If a four-digit number is input through the keyboard,
write a program to obtain the sum of the first and the last digit of this number.
/*HINT: If a number is divided using % , then the number to the right side of the decimal point is the result. (This applies only to integers.) */

  1. #include<stdio.h>
  2.  
  3. main ()
  4.  
  5. {
  6.  
  7. int number, last_digit, first_digit, total;
  8.  
  9. printf (" Enter the number which is to be operated on: ");
  10.  
  11. scanf ("%d", &number);
  12.  
  13.  
  14.  
  15. last_digit = number % 10;
  16.  
  17. total = last_digit;
  18.  
  19.  
  20.  
  21. first_digit = (number / 1000) % 10;
  22.  
  23. total = total + first_digit;
  24.  
  25.  
  26.  
  27. printf ("The total of the first and the last digit of the entered number is: %d", total);
  28.  
  29.  
  30.  
  31. }