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

Finding Factorial of a number | C programming

  Q.6      WAP in C/ C++/ java to find the factorial of the number. Solution :  #include <stdio.h> void   main () {      int   n ;      printf ( " \n Enter any positive number : " );      scanf ( " %d " ,  & n );      if  ( n < 0 )     {          printf ( " \n Invalid input! Enter the value again -->" );          main ();  // calls the main() function again     }      else     {          int   fact = 1 ;          for ( int   i = 1 ;  i <= n ;  i ++ )         {  ...

Calculating Sum of digits & total number of digits | C programming

  Q.7       WAP in C/ C++/ java to find the sum of the digits of the number and also count   the number of digits present.          Example,                      Input : 123,                       Output : Sum of digits = 6             [ expl. 1+2+3]                                          Total digits = 3 Solutions : #include <stdio.h> int   digits ( int  x)  // function to count total no. of digits {      int   c = 0 ;      while  (x > 0 )     {       ...

Neon Number | C programming

 Q.8     WAP in C/ C++/ java to check whether the number is a Neon number or not. Input: 9 Output: Neon Number Explanation: the square is 9*9 = 81 and the sum of the digits of the square ( 8+1) is 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 ; } int   main () {      int   n ;         printf ( " \n Enter any positive number : " );      scanf ( " %d " ,  & n );      if  ( n < 0 )     { ...