# Prolog; Testing 2 variables

Page 1 of 1

## 7 Replies - 1960 Views - Last Post: 14 September 2012 - 09:04 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=291894&amp;s=f3ed568486acb2569d036a2ee437e5be&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 k3nnt0ter0

• D.I.C Head

Reputation: 0
• Posts: 181
• Joined: 16-January 10

# Prolog; Testing 2 variables

Posted 13 September 2012 - 06:50 PM

I am creating a Zodiac Sign and Chinese Zodiac calculator. I am done with the Chinese Zodiac and the only thing left is the Zodiac Sign. How do I test two variables?
Like for example,
M=Month, D=Day.
If M=1, D<=19, write("Capricorn").

Something like that.

My code was something like this,
write("Month: "), readint(M), write("Day: "), readint(D), try(M), try(D).
M=1,D<=19,write("Capricorn"); and so on...
My code isn't working yet. That was just to explain what I want.

Hope you help me:)

Is This A Good Question/Topic? 0

## Replies To: Prolog; Testing 2 variables

### #2 #define

• Duke of Err

Reputation: 1633
• Posts: 5,752
• Joined: 19-February 09

## Re: Prolog; Testing 2 variables

Posted 13 September 2012 - 08:10 PM

Hi, you can make a rule.

```write_sign(M, D) :- M=1, D <=19, write("Capricorn").

```

You might be able to use facts.

```star_sign(1, "Capricorn").
star_sign(2, "Aquarius").

write_sign(M, D) :- star_sign(M, SIGN), D<=19, write(SIGN).

```

Was This Post Helpful? 0

### #3 k3nnt0ter0

• D.I.C Head

Reputation: 0
• Posts: 181
• Joined: 16-January 10

## Re: Prolog; Testing 2 variables

Posted 14 September 2012 - 02:17 AM

Right! I'll try this! Thank you!!
Was This Post Helpful? 0

### #4 k3nnt0ter0

• D.I.C Head

Reputation: 0
• Posts: 181
• Joined: 16-January 10

## Re: Prolog; Testing 2 variables

Posted 14 September 2012 - 05:57 AM

Thanks to you I was able to show the zodiac sign and the chinese zodiac. Can you help me with the proper validation of dates?
Like for example, there are only 30 days in April,June,September,November and February 29 only exist every 4 years.
```predicates
go
test(integer)
try(char)
try_first
write_zodiac(integer,integer)
clauses

go:-	clearwindow(),
write("Enter Birth Month: "), readint(M),
write("Enter Birth Date: "), readint(D),
write("Enter Birth Year : "), readint(Y),
YT=2012, Age=YT-Y,
write_zodiac(M,D), test(Y),
write("\nAge : ",Age," years old\n").

write_zodiac(M, D) :-

M=1, D <=19, write("Zodiac Sign : Capricorn\n");
M=1, D >=20, write("Zodiac Sign : Aquarius\n");
M=2, D <=18, write("Zodiac Sign : Aquarius\n");
M=2, D >=19, D<=28, write("Zodiac Sign : Pisces\n");
M=3, D <=20, write("Zodiac Sign : Pisces\n");
M=3, D >=21, write("Zodiac Sign : Aries\n");
M=4, D <=19, write("Zodiac Sign : Aries\n");
M=4, D >=20, write("Zodiac Sign : Taurus\n");
M=5, D <=20, write("Zodiac Sign : Taurus\n");
M=5, D >=21, write("Zodiac Sign : Gemini\n");
M=6, D <=20, write("Zodiac Sign : Gemini\n");
M=6, D >=21, write("Zodiac Sign : Cancer\n");
M=7, D <=22, write("Zodiac Sign : Cancer\n");
M=7, D >=23, write("Zodiac Sign : Leo\n");
M=8, D <=22, write("Zodiac Sign : Leo\n");
M=8, D >=23, write("Zodiac Sign : Virgo\n");
M=9, D <=22, write("Zodiac Sign : Virgo\n");
M=9, D >=23, write("Zodiac Sign : Libra\n");
M=10, D <=22, write("Zodiac Sign : Libra\n");
M=10, D >=23, write("Zodiac Sign : Scorpio\n");
M=11, D <=21, write("Zodiac Sign : Scorpio\n");
M=11, D >=22, write("Zodiac Sign : Sagittarius\n");
M=12, D <=21, write("Zodiac Sign : Sagittarius\n");
M=12, D >=22, write("Zodiac Sign : Capricorn\n");

M=2, D>=30, write("There are only maximum of 29 days in February"),go;
M=4, D=31, write("There are only 30 days in April!"),go;
M=6, D=31, write("There are only 30 days in June!"),go;
M=9, D=31, write("There are only 30 days in September!"),go;
M=11, D=31, write("There are only 30 days in November!"),go.

test(Y):-	Y=1980, write("Chinese Zodiac : Monkey"),try_first;
Y=1992, write("Chinese Zodiac : Monkey"),try_first;
Y=2004, write("Chinese Zodiac : Monkey"),try_first;

Y=1981, write("Chinese Zodiac : Rooster"),try_first;
Y=1993, write("Chinese Zodiac : Rooster"),try_first;
Y=2005, write("Chinese Zodiac : Rooster"),try_first;

Y=1982, write("Chinese Zodiac : Dog"),try_first;
Y=1994, write("Chinese Zodiac : Dog"),try_first;
Y=2006, write("Chinese Zodiac : Dog"),try_first;

Y=1983, write("Chinese Zodiac : Boar"),try_first;
Y=1995, write("Chinese Zodiac : Boar"),try_first;
Y=2007, write("Chinese Zodiac : Boar"),try_first;

Y=1984, write("Chinese Zodiac : Rat"),try_first;
Y=1996, write("Chinese Zodiac : Rat"),try_first;
Y=2008, write("Chinese Zodiac : Rat"),try_first;

Y=1985, write("Chinese Zodiac : Ox"),try_first;
Y=1997, write("Chinese Zodiac : Ox"),try_first;
Y=2009, write("Chinese Zodiac : Ox"),try_first;

Y=1986, write("Chinese Zodiac : Tiger"),try_first;
Y=1998, write("Chinese Zodiac : Tiger"),try_first;
Y=2010, write("Chinese Zodiac : Tiger"),try_first;

Y=1987, write("Chinese Zodiac : Rabbit"),try_first;
Y=1999, write("Chinese Zodiac : Rabbit"),try_first;
Y=2011, write("Chinese Zodiac : Rabbit"),try_first;

Y=1988, write("Chinese Zodiac : Dragon"),try_first;
Y=2000, write("Chinese Zodiac : Dragon"),try_first;
Y=2012, write("Chinese Zodiac : Dragon"),try_first;

Y=1989, write("Chinese Zodiac : Snake"),try_first;
Y=2001, write("Chinese Zodiac : Snake"),try_first;

Y=1990, write("Chinese Zodiac : Horse"),try_first;
Y=2002, write("Chinese Zodiac : Horse"),try_first;

Y=1991, write("Chinese Zodiac : Ram"),try_first;
Y=2003, write("Chinese Zodiac : Ram"),try_first.

test(Y):-	YT=2012, Y>YT, write("Invalid Year "),go.
test(Y):-	Y<1980, write("Invalid Year "),go.
try_first:-

write("\nWould you like to repeat Again? [Y/N] : "),readchar(A),try(A).
try(A):-A='y',go;
A='Y',go;
A='n',write("\nThank You");
A= 'N',write("\nThank You");
A<>'Y',write("\nInvalid Entry"),try_first;
A<>'y',write("\nInvalid Entry"),try_first;
A<>'N',write("\nInvalid Entry"),try_first;
A<>'n',write("\nInvalid Entry"),try_first.

goal
go.
```

Hope you could help me. I'm just new in Prolog.

This post has been edited by k3nnt0ter0: 14 September 2012 - 05:59 AM

Was This Post Helpful? 0

### #5 #define

• Duke of Err

Reputation: 1633
• Posts: 5,752
• Joined: 19-February 09

## Re: Prolog; Testing 2 variables

Posted 14 September 2012 - 07:52 PM

Hi, to test the leap year, you can use the modulo operator which gives the remainder of an integer division.

If a number, eg 2012 is divided by 4 the remainder is 0, then the number is exactly divisible by 4.

2012 mod 4 = 0

Here are some ideas :
You wish to check a date. To check the date you need to find the number of days in a month and for that, a test whether a year is a leap year is required.

```domains
days,months,years = integer

predicates
daysinmonth(months, days, years)
isleapyear(years)
check_date(months, days, years)

clauses

daysinmonth( 1, 31, _).
/* etc */

```

I'll let you have a go at writing some of the code, since that is half the fun.

Leap Year : Wikipedia

This post has been edited by #define: 14 September 2012 - 08:07 PM

Was This Post Helpful? 0

### #6 k3nnt0ter0

• D.I.C Head

Reputation: 0
• Posts: 181
• Joined: 16-January 10

## Re: Prolog; Testing 2 variables

Posted 14 September 2012 - 08:02 PM

Right. I'll give it a try Thanks!
Was This Post Helpful? 0

### #7 k3nnt0ter0

• D.I.C Head

Reputation: 0
• Posts: 181
• Joined: 16-January 10

## Re: Prolog; Testing 2 variables

Posted 14 September 2012 - 08:37 PM

Uhmm.. May I know how do I begin the test for number of days in a month?
I don't know how am I going to use the daysinmonth.
```domains

days, months, years = integer

predicates

zod
testleap(integer,integer,integer)
daysinmonth(months, days, years)
isleapyear(years)

clauses

daysinmonth(1, 31, _).
daysinmonth(2, 29, _).
daysinmonth(3, 31, _).
daysinmonth(4, 30, _).
daysinmonth(5, 31, _).
daysinmonth(6, 30, _).
daysinmonth(7, 31, _).
daysinmonth(8, 31, _).
daysinmonth(9, 30, _).
daysinmonth(10, 31, _).
daysinmonth(11, 30, _).
daysinmonth(12, 31, _).

isleapyear(1980).
isleapyear(1984).
isleapyear(1988).
isleapyear(1992).
isleapyear(1996).
isleapyear(2000).
isleapyear(2004).
isleapyear(2008).
isleapyear(2012).

zod :-	clearwindow(),
write("Enter Birth Month: "), readint(M),
write("Enter Birth Date: "), readint(D),
write("Enter Birth Year: "), readint(Y),
YT=2012, Age=YT-Y,
write("\nAge: ",Age,"years old\n"),testleap(M,D,Y).

goal
zod.
```

This post has been edited by k3nnt0ter0: 14 September 2012 - 08:41 PM

Was This Post Helpful? 0

### #8 #define

• Duke of Err

Reputation: 1633
• Posts: 5,752
• Joined: 19-February 09

## Re: Prolog; Testing 2 variables

Posted 14 September 2012 - 09:04 PM

If YEAR is a leap year then the days in the month of February is 29.

```    daysinmonth(2, 29, YEAR) :- isleapyear(YEAR), !.
daysinmonth(2, 28, _).

```

To use daysinmonth a variable is used to get the number of days.

```zod :-	clearwindow(),
write("Enter Birth Month: "), readint(M),
write("Enter Birth Date: "), readint(D),
write("Enter Birth Year: "), readint(Y),
YT=2012, Age=YT-Y,
write("\nAge: ",Age,"years old\n"),
daysinmonth(M, MAXDAYS, Y),
write("Days in the Month : ", MAXDAYS, "\n").

```

This post has been edited by #define: 14 September 2012 - 09:07 PM

Was This Post Helpful? 0

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }