Smallest among two Numbers

 Smallest among two Numbers


C Program
1.Using if else Statement
#include <stdio.h>
#include <stdlib.h>
int main()
{
    int a,b;
    printf("Enter First Number:");
    scanf("%d",&a);
    printf("Enter Second Number:");
    scanf("%d",&b);
    if(a>b)
        printf("min is: %d",b);
    else
        printf("min is: %d",a);
}

2.Using Ternary Operator
#include <stdio.h>
#include <stdlib.h>
int main()
{
    int a,b;
    printf("Enter First Number:");
    scanf("%d",&a);
    printf("Enter Second Number:");
    scanf("%d",&b);
    (a>b)? printf("min is %d",b) : printf("min is %d",a);
}
Java Program
1.Using if else Statement
import java.util.Scanner;

public class Min {

	public static void main(String[] args) 
	{
		 Scanner  s = new Scanner(System.in);
	     System.out.print("Enter First Number:");
	     int a = s.nextInt();
	     System.out.print("Enter Second Number:");
	     int b = s.nextInt();
	     if(a>b)
	    	 System.out.print("Min is: " + b);
	     else
	    	 System.out.print("Min is: " + a);
	    
	}

 }

2.Using Ternary Operator
import java.util.Scanner;

public class Min {

	public static void main(String[] args)
	{
		Scanner  s = new Scanner(System.in);
		System.out.print("Enter First Number:");
		int a = s.nextInt();
		System.out.print("Enter Second Number:");
	    int b = s.nextInt();
	    int min = (a>b)? b:a;
	    System.out.print("Min is: " + min);
	}
		   
}

0 Comments:

Post a Comment