Friday 2 August 2019

Questions on Conditional Statements in C:


Library fine:
https://www.hackerrank.com/challenges/library-fine/problem

#include<stdio.h>
int main()
{
int dr,mr,yr,de,me,ye,fine=0;
scanf("%d%d%d",&dr,&mr,&yr);
scanf("%d%d%d",&de,&me,&ye);
if(yr == ye)
{
if(mr > me)
fine =(mr-me)*500;
else if(mr == me)
{
if(dr>de)
fine = (dr-de)*15;
}
}
else if(yr>ye)
fine =10000;
printf("%d",fine);
return 0;
}


Given an integer, , perform the following conditional actions:
  • If  is odd, print Weird
  • If  is even and in the inclusive range of  to , print Not Weird
  • If  is even and in the inclusive range of  to , print Weird
  • If  is even and greater than , print Not Weird
The above statements can be converted to 

 if the number is from 6 to 20 and the number is odd then  print "Weird" and if the number is even also print "Weird" and so we conclude if the number is even or odd and is from 6 to 20 then print "Weird"
so the condition becomes

if( n %2 != 0 || (n>=6 && n<= 20))
                   printf("Weird");
else
                  printf("Not Weird");




No comments:

Post a Comment