Thread: Something wrong in my code to this If-else question???

  1. #1
    Registered User
    Join Date
    Jun 2017
    Posts
    3

    Question Something wrong in my code to this If-else question???

    Ques.

    An Insurance company follows following rules to calculate premium.

    (1) If a person’s health is excellent and the person is between 25 and 35 years of age and lives in a city and is a male then the premium is Rs. 4 per thousand and his policy amount cannot exceed Rs. 2 lakhs.
    (2) If a person satisfies all the above conditions except that the sex is female then the premium is Rs. 3 per thousand and her policy amount cannot exceed Rs. 1 lakh.
    (3) If a person’s health is poor and the person is between 25 and 35 years of age and lives in a village and is a male, then the premium is Rs. 6 per thousand and his policy cannot exceed Rs. 10,000.
    (4) In all other cases the person is not insured.

    Write a program to output whether the person should be insured or not, his/her premium rate and maximum amount for which he/she can be insured.



    Ans. insert
    Code:
    #include <stdio.h>
    #include <conio.h>
    
    void main()
    {
        int age;
        char health[10], gen[6], loc[7];
    
        printf("Health: \nAge: \nGender: \nLocation: \n");
        scanf("%s%d%s%s",&health,&age,&gen,&loc);
    
        printf("Health: %s \nAge: %d \nGender: %s \nLocation: %s \n",health,age,gen,loc);
    
        if(age>=25 && age<=35)
        {
            if(health=="excellent")
            {
                if(loc=="city")
                {
                    if(gen=="male")
                    {
                        printf("The premium is Rs. 4 per thousand and his policy amount cannot exceed Rs. 2 lakhs");
                    }
                    else if(gen=="female")
                    {
                        printf("The premium is Rs. 3 per thousand and his policy amount cannot exceed Rs. 1 lakhs");
                    }
                    else
                    {
                        printf("The person is not insured");
                    }
                }
                else
                {
                    printf("The person is not insured");
                }
    
            }
            else if(health=="poor")
            {
                if(loc=="village")
                {
                    if(gen=="male")
                    {
                        printf("The premium is Rs. 6 per thousand and his policy cannot exceed Rs. 10,000");
                    }
                    else
                    {
                        printf("The person is not insured");
                    }
                }
    
                else
                {
                    printf("The person is not insured");
                }
            }
            else
            {
                printf("The person is not insured");
            }
    
        }
    
        else
        {
            printf("The person is not insured");
        }
    
        getch();
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    1. conio.h is obsolete (has been for 25+ years)
    2. main returns int, not void -> FAQ > main() / void main() / int main() / int main(void) / int main(int argc, char *argv[]) - Cprogramming.com
    3. You need to use strcmp() to compare two strings, not ==
    4. You don't need &var when using %s and char arrays, in your scanf call.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jun 2017
    Posts
    3
    This question is from the programming book for absolute beginners (Let Us C). So, this question has to be done (1) without using strcmp() and (2) using &var in scanf call.

    Can you improvise my code for the answer?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    Buying a better book would be an improvement.

    I guess you'll just have to write your own strcmp function then.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can someone please tell me what is wrong with the code
    By spirit75 in forum C Programming
    Replies: 7
    Last Post: 11-07-2010, 05:50 AM
  2. What is wrong with this code
    By FORMTA in forum C Programming
    Replies: 7
    Last Post: 10-29-2010, 07:33 AM
  3. Can someone please tell me what is wrong with this code?
    By cloudstrife910 in forum C++ Programming
    Replies: 7
    Last Post: 10-23-2010, 04:17 AM
  4. what's wrong with this c++ code, help!!!
    By lsctt in forum C++ Programming
    Replies: 17
    Last Post: 06-02-2006, 07:47 PM
  5. Anyone see what is wrong with this code?
    By Wise1 in forum C Programming
    Replies: 2
    Last Post: 02-13-2002, 02:01 PM

Tags for this Thread