Skip to main content

Program to find the suffix of a number (between 1-31) | C programming

 Q.3      WAP in C/ C++/ java to find the suffix of the given number.

            Inputs              Outputs

              1                          st

              2                          nd

              3                          rd

              11                        th

              21                        st

   22                         nd

              23                        rd


Solution : 

#include<stdio.h>
// char* is a return type for string values
char* suffix(int x) // function to return suffix of number in string form
{
    if (x==1 || x==21 || x==31)
        return "st";
    else if (x==2 || x==22)
        return "nd";
    else if(x==3 || x==23)
        return "rd";
    else
        return "th";
    
}
void main()
{
    int n;
    printf("\nEnter any positive number : ");
    scanf("%d"&n);

    if (n<0 || n>31)
    {
        printf("\nInvalid input! Enter the value again -->");
        main(); // calls the main() function again
    }
    else
    {
        printf("Your output is : %d"n);
        puts(suffix(n)); // shows the suffix of the number
        
    }
// end of main()

Comments

Popular posts from this blog

Harshad Number | C programming

  Q.10     WAP in C/ C++/ java to check whether a number is a  Harshad Number  or not.  In recreational mathematics, a Harshad number in a given number base is an integer that is divisible by the sum of its digits when written in that base.         Example: Number 200 is a Harshad Number because the sum of digits 2 and 0 and 0 is 2(2+0+0) and 200 is divisible by 2. Number 171 is a Harshad Number because the sum of digits 1 and 7 and 1 is 9(1+7+1) and 171 is divisible by 9.     Solution :  #include <stdio.h> int   sumofdigits ( int  x)  // function to compute sum of digits {      int   sum = 0 ;      while (x > 0 )     {          sum += x % 10 ;         x = x / 10 ;     }      return   sum ; } void   main () {      int   n ,  sod ;      printf ( " \n Enter any positive number : " );      scanf ( " %d " ,  & n );      if  ( n < 0 )     {          printf ( " \n Invalid input! Enter the value again -->" );          main ();  // calls the m

Happy Number Solution | Recursion

  /**  * Write a Java program to find and print the Happy numbers between m and n. Happy number: Starting with any positive integer, replace the number by the sum of the squares of its digits,  and repeat the process until the number equals 1, or it loops endlessly in a cycle which does not include 1. Example: 19 is a happy number 1^2 + 9^2=82 8^2 + 2^2=68 6^2 + 8^2=100 1^2 + 0^2 + 02=1 The first few happy numbers are 1, 7, 10, 13, 19, 23, 28, 31, 32, 44, 49, 68, 70, 79, 82, 86, 91, 94, 97, 100, ...  */ import java.util.*; public class HappyNum {     public static int digitSq( int x) // recursive function to find square of digits of the number     {         if(x<10)         return x*x;         else         return (x%10)*(x%10) + digitSq(x/10);     } // end of digitSq()          public static boolean isHappy(int num, int i)  // recursive function to check whether the no. is happy or not     {         //System.out.println(num);         if(num==1)         return true;         else i