Write a C program that asks for the users age If it is equal

Write a C program that asks for the user\'s age. If it is equal to or greater than 21, print a msg to a file named \"Yes.txt\" telling the user tha the or she is allowed to drink alcohol. If the user\'s age is less than 21, print a msg to a file named \"No.txt\" telling the user how many years he or she has to wait before legally being allowed to drink.

Solution

#include <stdio.h>
int main()
{
   int age;
FILE *fp;
printf(\"Enter your age :\");
scanf(\"%d\",&age);
const char *text1 = \"You are allowe to drink alcohol\";
const char *text2 = \"Before legally being allowed To drink alcohol\";
if(age>=21)
   {
       fp = fopen(\"Yes.txt\",\"w\");
       fprintf(fp, \"Some text: %s\ \", text1);
   }
   else
   {
       int wait=21-age;
       fp = fopen(\"No.txt\",\"w\");
       fprintf(fp, \"You need to wait %d years: %s\ \",wait, text2);
   }
    fclose(fp);
}

(NOTE:In the local directory you should have a Yes.txt and No.txt);

Write a C program that asks for the user\'s age. If it is equal to or greater than 21, print a msg to a file named \

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site