Ad

Chapter - 3: The Decision Control

Rewrite the following programs using conditional operators.


Ad
J
Sections
1
Exercises

(a)

#include<stdio.h>
int main()
{
	int x, min, max ;
	
	scanf ( "\n%d %d", &max, &x ) ;
	if ( x > max )
		max = x ;
	else
		min = x ; 
	
	return 0;	
}

Answer:

#include<stdio.h>
int main()
{
	int x, min, max ;
	
	scanf ( "\n%d %d", &max, &x ) ;
	x = (x > max ? max : min);
	
	return 0;	
}

(b)

#include<stdio.h>
int main()
{
	int code ;

	scanf ( "%d", &code ) ;
	if ( code > 1 )
		printf ( "\nJerusalem" ) ;
	else
		if ( code < 1 )
			printf ( "\nEddie" ) ;
		else
			printf ( "\nC Brain" ) ; 
	
	return 0;	
}

Answer:

#include<stdio.h>
int main()
{
	int code ;

	scanf ( "%d", &code ) ;
	
	code > 1 ? printf("\nJerusalem") : (code < 1 ? printf("\nEddie") : printf("\nC Brain"));
	
	return 0;	
}

(c)

#include<stdio.h>
int main()
{
	float sal ;

	printf ("Enter the salary" ) ;
	scanf ( "%f", &sal ) ;
	
	if ( sal < 40000 && sal > 25000 )
		printf ( "Manager" ) ;
	else
		if ( sal < 25000 && sal > 15000 )
			printf ( "Accountant" ) ;
		else
			printf ( "Clerk" ) ; 
	
	return 0;	
}

Answer:

#include<stdio.h>
int main()
{
	float sal ;

	printf ("Enter the salary" ) ;
	scanf ( "%f", &sal ) ;
	
	(sal < 40000 && sal > 25000)?printf("Manager"):(sal < 25000 && sal > 15000 ? printf("Accountant") : printf("Clerk"));

	return 0;	
}
Ad

© 2024 Garbage Valuegarbage value logo