Wednesday, September 17, 2014

program that calculates area and perimeter of a triangle, square, and circle using macro with argument

0 comments

Problem

Write macro definitions with arguments for calculation of area and perimeter of a triangle, a square and a circle. Store these macro definitions in a file called “areaperi.h”. Include this file in your program, and call the macro definitions for calculating area and perimeter for different squares, triangles and circles.

Solution - Code

#include<stdio.h>
#include<conio.h>
#define CAREA(r) (3.14*r*r)
#define cperi(r) (2*3.14*r)
#define sarea(x) (x*x)
#define speri(x) (4*x)
#define tarea(x,y,z) (0.5*x*y)
#define yperi(x,y,z) (x+y+z)
main ()
{
 float r,s,b,a,h,car,tar,sar,cpr,tpr,spr,x;

 printf ("\n for a circle");
 printf ("\n \n enter radius:");
 scanf ("%f",&r);

 car = CAREA (r);
 cpr = cperi (r);
 printf ("area = %f", car);
 printf ("\n perimeter =%f", cpr);

 printf ("\n \n for a square:");

 printf ("\n \n enter side: ");
 scanf ("%f", &s);

 sar = sarea(s);
 spr = speri (s);

 printf ("\n area =%f", sar);
 printf ("\n perimeter =%f", spr);

 printf ("\n \n for a triangle");

 printf ("\n enter base, altitude, height:");
 scanf ("%f %f %f ", &b, &a, &h);

 tar = tarea (b, a, h);
 tpr = yperi (b, a, h);

 printf ("\n area=%f", tar);
 printf ("\n perimeter= %f", tpr);

 printf ("\n\npress any key to close.");
 getch();
}

Sample Output

use macro with argument to calculate area and peritmeter

Read More..

Defining macro's in c language

0 comments

Problem

Write down macro definitions for the following:
  1. To test whether a character entered is a small case letter or not.
  2. To test whether a character entered is a upper case letter or not.
  3. To test whether a character is an alphabet or not. Make use of the macros you defined in (1) and (2) above.
  4. To obtain the bigger of two numbers.

Solution - Code (a)

#include<stdio.h>
#include<conio.h>
#include<ctype.h>
#include<string.h>
#define LOWER(ch) (ch>=97&&ch<=122 ? printf("\nlower"):printf("\n not lower"))
main()
{
 char ch;

 printf("\n enter the character=");
 scanf("%c",&ch);

 LOWER (ch);

 printf ("\npress any key to close.");
 getch();
}

Sample Output (a)


character is lower case or not

Solution - Code (b)

#include<stdio.h>
#include<conio.h>
#include<ctype.h>
#include<string.h>
#define UPPER(ch) (ch>=65&&ch<=90 ? printf("\nUpper case"):printf("\n not upper case"))
main()
{
 char ch;

 printf("\n enter the character=");
 scanf("%c",&ch);

 UPPER (ch);

 printf ("\npress any key to close.");
 getch();
}

Sample Output (b)


character is upper case or not

Solution - Code (c)

#include<stdio.h>
#include<conio.h>
#include<ctype.h>
#include<string.h>
#define ALPHA(ch) (isalpha (ch) ? printf("\n alphabet"):printf("\n not alphabet"))
main()
{
 char ch;

 printf("\n enter the character=");
 scanf("%c",&ch);

 ALPHA (ch);

 printf ("\npress any key to close.");
 getch();
}

Sample Output (c)


character is alphabet or not

Solution - Code (d)

#include<stdio.h>
#include<conio.h>
#include<ctype.h>
#include<string.h>
#define LARGE(a,b) (a>b ? printf("\n a is greater"):printf("\n b is greater"))
main()
{
 int a,b;

 printf("\n enter the two number =");
 scanf("%d%d",&a,&b);

 LARGE (a,b);

 printf ("\npress any key to close.");
 getch();
}

Sample Output (d)


bigger of two numbers

Read More..

program that asks the user to input n number of records that he/she wants to enter by using malloc function, then display the user input values

0 comments

Problem

Student structure contains the following contents:
Roll number (int), Name (string), Department (string), Course (string), Year of joining (string), cgpa (float)
Write a program that first prompts the user to input the n number of student records that he/she wants to input in that program. Then use malloc function to dynamically allocate memory that has n number of elements (type of each element is struct student). Then input records from user and display them on screen.
Hint:  p = (struct student*) malloc(sizeof (struct student) * n);

Solution - Code

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
struct student
{
 char name[30];
 int roll_no;
 char department[30];
 char course[30];
 int year;
 float cgpa;
};
main()
{
 struct student *s;
 int i,how_many;

 printf ("Number of student data you want to enter");
 scanf ("%d",&how_many);

 s = (struct student*) malloc(sizeof(struct student) * how_many);

 for (i=0;i<how_many;i++)
 {
  fflush(stdin);
  printf ("enter name: ");
  gets(&s[i].name);

  printf ("Enter Roll no: ");
  scanf ("%d",&s[i].roll_no);

  fflush(stdin);
  printf ("enter department: ");
  gets (&s[i].department);

  fflush (stdin);
  printf ("enter course: ");
  gets (&s[i].course);

  printf ("enter year: ");
  scanf ("%d",&s[i].year);

  printf ("enter cgpa: ");
  scanf ("%f",&s[i].cgpa);
 }

 printf("\nAnd here is whhat you have entered\n\n");
 for (i=0;i<how_many;i++)
  printf("Name = %s\n\nRoll No. = %d\n\nDepartment = %s\n\nCopurse = %s\n\nYear = %d\n\n",s[i].name,s[i].roll_no,s[i].department,s[i].course,s[i].year);

 system("pause");
}

Sample Output


using malloc function

Read More..

write a structure for student data, also define functions to display and search that data

0 comments

Problem

Student structure contains the following contents
Roll number (int), Name (string), Department (string), Course (string), Year of joining (string), cgpa (float)
Write the definition of the functions whose prototype is given below:
  1. void display(struct strudent s);
    This function display the content of struct student s
  2. void display(struct strudent *p);
    This function displays the content of a structure pointed by pointer *p
  3. void search(struct strudent *p, int roll_no);
    The first parameter is the base address of an array of type struct student and second parameter is the roll no of the structure. This function search's the record from array of structure (first parameter) whose Roll number is same as roll_no (second parameter)

Solution - Code

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
struct student
{
 char name[30];
 int roll_no;
 char department[30];
 char course[30];
 int year;
}s[5];
int i;
void searchstudent (struct student *, int );
void disp();
void display (struct student *);
main()
{
 int rl,p;

 for (i=0;i<5;i++)
 {
  printf ("Enter name, ID, department, course, year : ");
  scanf("%s%d%s%s%d",&s[i].name,&s[i].roll_no,&s[i].department,&s[i].course,&s[i].year);
 }

 disp();
 display(s);

 printf ("Enter number you want to search : ");
 scanf("%d",&rl);

 searchstudent (s ,rl);

 system("pause");
}
//************
void disp()
{
 for (i=0;i<5;i++)
 {
  printf("And here is whhat you have entered.\n\n");
  printf("Name = %s\n\nRoll No. = %d\n\nDepartment = %s\n\nCopurse = %s\n\nYear = %d\n\n",s[i].name,s[i].roll_no,s[i].department,s[i].course,s[i].year);
 }
}
//************************
void display (struct student *p)
{
 printf("Name = %s\n\nRoll No. = %d\n\nDepartment = %s\n\nCopurse = %s\n\nYear = %d\n\n",p->name,p->roll_no,p->department,p->course,p->year);
}
void searchstudent (struct student *p, int roll_no)
{
 int i,found = 0;
 for(i=0;i<5;i++)
 {
  if(p->roll_no == roll_no)
  {
   printf("record found");
   printf("Name = %s\n\nRoll No. = %d\n\nDepartment = %s\n\nCopurse = %s\n\nYear = %d\n\n",p->name,p->roll_no,p->department,p->course,p->year);
   found = 1;
  }
 }
 if (found == 0)
  printf("Not found");
}

Sample Output


display & search data using structure

Read More..

Copyright 2017. All Rights Reserved. Privacy Policy / Terms And Conditions / Sitemap / Contact