LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 07-11-2012, 01:27 AM   #1
Andy Alkaline
Member
 
Registered: Jun 2004
Location: Minnesota, USA
Distribution: Slackware32-stable, Debian-wheezy-amd64, LFS 7.1
Posts: 352

Rep: Reputation: 33
C - check if a number is divisible by another number


A program I threw together recently. It works, but I'm posting it here because I always find it interesting to see comments and suggestions by other users.

Code:
/*
 * isdivis.c
 * check if a number is divisible by another number
 * 
 * Copyright 2012 Andy Alt
 * 	<andyqwerty at users dot sourceforge dot net>
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA 02110-1301, USA.
 */

#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <string.h>

#define DATE 20120708
#define VER 1

void print_usage(char *argv);

int main(int argc, char **argv)
{
  
  if (argc == 2) {
    if (strcmp (argv[1], "--version") == 0) {
      printf ("isdivis %d-%d\n", VER, DATE);
      exit(0);
    }
    else {
      print_usage(argv[0]);
      exit(1);
    }
  }
  
  if (argc != 3) {
    print_usage(argv[0]);
    exit (2);
  }
 
        
  int dividend = atoi (argv[1]);
  int divisor = atoi (argv[2]);
  
  if ( (dividend > 0 && dividend < INT_MAX) 
          && (divisor > 0 && divisor < INT_MAX) ) {
      
    double quotient;
    quotient = ( (double) dividend) / divisor;
    
    if ( ( (int) quotient) == quotient )
      return 0;
    else
      return 1;
  }
  else {
    print_usage(argv[0]);
    exit (2);
  }
      
}

void print_usage(char *argv) {
  printf("usage: %s <dividend> <divisor>\n", argv);
  printf("       %s --version\n\n", argv);
  printf("notes: dividend and divisor must be greater than 0\n");
  printf("       and less than %d\n\n", INT_MAX);
  printf("       If arg1 is divisible by arg2, exit code will be 0\n");
  
}
 
Old 07-11-2012, 01:50 AM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,414

Rep: Reputation: 1966Reputation: 1966Reputation: 1966Reputation: 1966Reputation: 1966Reputation: 1966Reputation: 1966Reputation: 1966Reputation: 1966Reputation: 1966Reputation: 1966
Why not just do a modulus? Don't reinvent the wheel.

# echo $((9%3))
0
# echo $((9%4))
1
 
2 members found this post helpful.
Old 07-11-2012, 02:06 PM   #3
Andy Alkaline
Member
 
Registered: Jun 2004
Location: Minnesota, USA
Distribution: Slackware32-stable, Debian-wheezy-amd64, LFS 7.1
Posts: 352

Original Poster
Rep: Reputation: 33
Thanks for pointing out that method. Much better!
 
  


Reply

Tags
division, math, programming, snippet


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP + MYSQL: Invalid parameter number: number of bound variables does not match... OrangeGrover Linux - Software 6 05-08-2013 05:36 PM
how to change the complex number display mode. ... real number sandeep002gupta Programming 1 05-09-2011 06:15 AM
Identify and explain the major number, minor number, and revision number in Linux... turbomen Linux - Newbie 1 11-16-2010 02:48 AM
how can i check that $1 is number... DoME69 Programming 10 03-09-2009 02:08 PM
why there is a need for minor number and major number for monitor/keyboard in pc? tripathi Solaris / OpenSolaris 1 11-07-2003 09:36 AM


All times are GMT -5. The time now is 10:36 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration