Home
Diploma Practice C Online People
Certificate Contact Us

Solutions for assignments on simple functions

as01.c

#include <stdio.h>
#include <math.h>

double getSphereVolume(double radius)
{
  double volume;

  volume = (4.0/3.0) * M_PI * radius * radius * radius;
  return volume;
}

int main()
{
  double vol,rad;

  printf("Enter the radius of the sphere: ");
  scanf("%lf",&rad);

  vol = getSphereVolume(rad);
  printf("Volume of sphere with radius %lf is %lf\n",rad, vol);
}

as02.c

#include <stdio.h>
#include <math.h>

double getSphereSurfaceArea(double radius)
{
  return  (4 * M_PI * radius * radius);
}

int main()
{
  double sfcArea,rad;

  printf("Enter the radius of the sphere: ");
  scanf("%lf",&rad);

  sfcArea = getSphereVolume(rad);
  printf("Radius, surface Area:  %lf, %lf\n",rad, sfcArea);
}

as03.c

#include <stdio.h>
#include <math.h>

double getSphereWeight(double radius, double density)
{
  double vol;

  vol = (4.0/3.0) * M_PI * radius * radius * radius;
  return  (vol * density) ;
}

int main()
{
  double density,rad;

  printf("Enter the radius of the sphere: ");
  scanf("%lf",&rad);

  printf("Following are the densities of various metals\n");
  printf("  Aluminium : 2.712\n");
  printf("  Copper    : 8.94\n");
  printf("  Gold      : 19.32\n");
  printf("  Iron      : 7.85\n");
  printf("  Silver    : 10.49\n");

  printf("Enter the density of metal: ");
  scanf("%lf",&density);

  printf("Radius, density, weight:  %lf, %lf, %lf\n",
                          rad, density, getSphereWeight(rad, density));
}

as04.c

#include <stdio.h>

unsigned int setBit(unsigned int val, int bitPos)
{
  return val | (1 << bitPos);
}

int main()
{
  unsigned int val, bpos;

  printf("Enter an hex number\n");
  scanf("%x", &val);

  printf("Enter position of bit to set (0 to 31)\n");
  scanf("%d", &bpos);
 
  printf("HexNumber before setting bit %d is %x\n", bpos, val); 
  printf("HexNumber after  setting bit %d is %x\n", bpos, setBit(val,bpos)); 
}

as05.c

#include <stdio.h>

unsigned int clearBit(unsigned int val, int bitPos)
{
  unsigned newVal;

  newVal = val & ~(1 << bitPos);
  return newVal;
}

int main()
{
  unsigned int val, bpos, nval;

  printf("Enter an hex number\n");
  scanf("%x", &val);

  printf("Enter position of bit to clear (0 to 31)\n");
  scanf("%d", &bpos);
 
  nval = clearBit(val,bpos); 

  printf("HexNumber before clearing bit %d is %x\n", bpos, val); 
  printf("HexNumber after  clearing bit %d is %x\n", bpos, nval); 
}

as06.c

#include <stdio.h>

int getTotalSecondsElapsed(short hour, short min, short sec)
{
  return (hour * 60 * 60) + (min * 60) + sec;
}

int main()
{
  short hour, min, sec;
  int   totalSec;

  printf("Enter hours, minutes and seconds with spaces in between: ");
  scanf("%hd%hd%hd", &hour, &min, &sec);

  printf("Given time: %02d:%02d:%02d\n",hour,min,sec);

  totalSec = getTotalSecondsElapsed(hour, min, sec);
  printf("Number of seconds in the given time are %d\n", totalSec);
}

as07.c

#include <stdio.h>

struct Time
{
  char hour;
  char min;
  char sec;
};

int getTotalSecondsElapsed(struct Time t)
{
  return (t.hour * 60 * 60) + (t.min * 60) + t.sec;
}

int main()
{
  struct Time tm; 
  int   totalSec;

  printf("Enter hours, minutes and seconds with spaces in between: ");
  scanf("%hhd%hhd%hhd", &tm.hour, &tm.min, &tm.sec);

  printf("Given time: %02d:%02d:%02d\n",tm.hour,tm.min,tm.sec);

  totalSec = getTotalSecondsElapsed(tm);
  printf("Number of seconds in the given time are %d\n", totalSec);
}

as08.c

#include <stdio.h>

struct Time
{
  char hour;
  char min;
  char sec;
};


struct Time getTimeFrmSecondsElapsed(int totalSeconds)
{
  struct Time t;

  t.hour = totalSeconds / (60 * 60);
  totalSeconds = totalSeconds % (60 * 60);
  t.min  = totalSeconds / 60;
  t.sec  = totalSeconds % 60;
  return t;
}

int main()
{
  struct Time tm; 
  int         tSeconds;

  printf("Enter total number of seconds: "); 
  scanf("%d", &tSeconds);

  tm = getTimeFrmSecondsElapsed(tSeconds);
  printf("Given seconds: %d\n", tSeconds);
  printf("Given time   : %02d:%02d:%02d\n",tm.hour,tm.min,tm.sec);
}


#401, Sai Sushma Homes, 23/A, S.R. Nagar Main Road, Hyderabad-500038, India.
Ph: +91-40-48508764, 7702028989, depik.help@gmail.com