Got Into Trouble with simple programe.

This is a discussion on Got Into Trouble with simple programe. within the C Programming forums, part of the General Programming Boards category; Question :- Any Year Is input through the keyboard,wrtie a pgm to determine whether the year is leap year or ...

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    83

    Thumbs up Got Into Trouble with simple programe.

    Question:- Any Year Is input through the keyboard,wrtie a pgm to determine whether the year is leap year or not. Use Logical Operators. && and || ....

    Now i have solved this problem using if-else , and using conditional opertor such as ? , : . See the pgm below.

    if-else USED here !!!
    Code:
     
     
    #include <stdlib.h>
    #include <conio.h>
    #include <ctype.h>
    #include <stdio.h>
    main()
    {
    int y;
    printf("Enter The Year:");
    scanf("%d",&y);       
    clrscr();        
        if(y%4==0)
        printf("This is leap Year");
        else
        printf("Not a Leap Year");
    getch();
    }
    Conditional operators used here!!

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <conio.h>
    #include <ctype.h>
    main()
    {
    int y;
    printf("Enter The Year");
    scanf("%d",&y);
    (y%4==0 ? printf("Leap Year") : printf("Not A Leap Year"));
    getch(); 
    }

    Now i m stuck in the same pgm where it asked to use logical oeperators....
    see my programem below
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <conio.h>
    #include <ctype.h>
    main()
    {
    int y;
    printf(" Input the desired year");
    scanf("%d",&y);
    if((y%4==0)||(y%4!=0)
    printf("Leap Yr
     I m stuck here......  i do not know..how can i use logical oeprators as there is only one condition that is " IF THE YEAR IS DIVIDED BY 4 THEN IT IS LEAP YEAR" so how can we use logical operators?????
     
    Please help

  2. #2
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218

    Post

    if((y%4==0)||(y%4!=0))
    Here you are missing a closing bracket, but even then the comparison is pointless as it always results as true.

    Your first method is probably the best way to do this, but if you really need to use logical or here you could do:
    Code:
    int ym = y%4;
    if(ym == 1 || ym == 2 || ym == 3)
    {
       // do something
    }
    else
    {
       // do something else
    }

  3. #3
    Registered User
    Join Date
    Feb 2008
    Posts
    146
    here it goes,

    A leap year is a year which is divisible by 4,..but if it is evenly divisible by 100 then it is not a leap year,but if it is evenly divisible by 400 then it is a leap year..

    Code:
    if((year%4==0 && year%100!=0 ||(year%400)==0))

  4. #4
    Registered User
    Join Date
    Apr 2006
    Posts
    83
    there is some problem.... what is ym....?"?? i understand y is the INPUT YEAR but what is ym?

  5. #5
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    its just a temporary variable to save writing (y&#37;4) all the time and it also cuts out some extra work for the cpu.

  6. #6
    Registered User
    Join Date
    Apr 2006
    Posts
    83
    Quote Originally Posted by edesign View Post
    here it goes,

    A leap year is a year which is divisible by 4,..but if it is evenly divisible by 100 then it is not a leap year,but if it is evenly divisible by 400 then it is a leap year..

    Code:
    if((year%4==0 && year%100!=0 ||(year%400)==0))
    this worked
    here is the code i used ........


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <conio.h>
    #include <ctype.h>
    main()
    {
    int y;
    printf(" Input the desired year");
    scanf("%d",&y);
    
    if((y%4==0 && y%100!=0 ||(y%400)==0))
    printf("Leap Yr ");
    else
    printf("not leap yr");
    getch();
    }
    Thanks Buddy!!!!!!!!!!!!

  7. #7
    Registered User
    Join Date
    Apr 2006
    Posts
    83
    There is no reputation system otherwise i had given you !!!!! repps

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple message encryption
    By Vicious in forum C++ Programming
    Replies: 10
    Last Post: 11-07-2004, 11:48 PM
  2. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 04:00 PM
  3. Simple simple program
    By Ryback in forum C++ Programming
    Replies: 10
    Last Post: 09-09-2004, 06:48 AM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. Need help with simple DAQ program
    By canada-paul in forum C++ Programming
    Replies: 12
    Last Post: 03-15-2002, 08:52 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21