Monday, November 13, 2017

Add/Sum two numbers using pointers in C/C++ Language

1 comments

Problem

Take two integer inputs from user. Create Sum function which should receive two integers using pointers. Calculate the sum and display the result in main function.

Code

#include "stdafx.h"
#include <iostream>
#include<conio.h>
using namespace std;


int sum (int *, int *);

int main()
{
int a,b,c;

cout << "Enter 1st number: ";
cin >> a;
cout<<"enter 2nd number: ";
cin >> b;

c = sum (&a,&b);

cout << "Sum = "<< c << endl;

cout<<"Press any key to close.";

getch();

return 0;
}

int sum (int *a, int *b)
{
int c;
c = (*a + *b);
return c;
}

Output

Add/Sum two numbers using pointers in C/C++ Language
Add/Sum two numbers using pointers in C/C++ Language

Read More..

Saturday, November 4, 2017

Calculate Square of 3 input integers using function and pointers

0 comments

Problem

Write a program to calculate square of an integer, input by the user. Declare a function with the name of square and should receive 3 integers. The function should then calculate the square of these three integers and return the square. Display the result.

Code

#include "stdafx.h"
#include <iostream>
#include<conio.h>
using namespace std;

void square (int *, int *, int *);

int main()
{
int a,b,c;

cout << "Enter 1st number: ";
cin >> a;

cout << "Enter 2nd number: ";
cin >> b;

cout << "Enter 3rd number: ";
cin >> c;

square (&a, &b, &c);

cout << "Square of Ist number = "<< a;
cout << "\nSquare of 2nd number = "<< b;
cout << "\nSquare of 3rd number = "<< c << endl ;


cout<<"Press any key to close.";

getch();

return 0;
}

void square (int *a, int *b, int *c)
{
*a = (*a * *a);
*b = (*b * *b);
*c = (*c * *c);
}

Output

Calculate Square of 3 input integers using function and pointers
Calculate Square of 3 input integers using function and pointers

Read More..

Sunday, October 15, 2017

Display Add/Sum of two integers in C Language

0 comments

Problem

Write a program in C language which takes two integers as input from user. Write a function to add these two integers and store the sum in another integer. Display the result.

Code

// lab02..cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include<conio.h>
using namespace std;


int sum (int, int);

int main()
{
int a,b,c;

cout << "Enter 1st number: ";
cin >> a;
cout<<"enter 2nd number: ";
cin >> b;

c = sum (a,b);

cout << "Sum = "<< c << endl;

cout<<"Press any key to close.";

getch();

return 0;
}

int sum (int a, int b)
{
int c;
c = a+b;
return c;
}

Output

Display Add/Sum of two integers in C Language
Display Add/Sum of two integers in C Language

Read More..

Saturday, October 7, 2017

Date Sum Using Structure in C/C++ Language

0 comments

Problem

Create a struct called data. User should be able to input day, month, and year. Take two date inputs and add them. Display the result.

Code

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

//struct decleration
struct data {
int years;
int months;
int days;
}d1,d2;

//function protorypes
void add_data();
void add_dates();
void display(int ,int ,int);

int main()
{
    //function calls
    add_data();
    add_dates();
   
printf ("\n\nPress any key to exit.");
getch();
return 0;
}

//adding record
void add_data()
{
     //1st date
     printf ("Enter 1st date.");
   
     printf ("\n\nEnter year: ");
     scanf ("%d",&d1.years);
   
     printf ("\nEnter month: ");
     scanf ("%d",&d1.months);
   
     printf ("\nEnter day: ");
     scanf ("%d",&d1.days);
   
     display(d1.days,d1.months,d1.years);
     //printf ("\n\nThe 1st date is %d/%d/%d",d1.days,d1.months,d1.years);
   
     //Second time
     printf ("\n\n\nNow, please type the 2nd date.");
   
     printf ("\nEnter year: ");
     scanf ("%d",&d2.years);
   
     printf ("\nEnter month: ");
     scanf ("%d",&d2.months);
   
     printf ("\nEnter day: ");
     scanf ("%d",&d2.days);
   
     display(d2.days,d2.months,d2.years);
}

void display (int a,int b,int c)
{
     printf("\nThe date is %d/%d/%d \n\n", a,b,c);
}   

void add_dates()
{
     int y,m,d;
   
     y = d1.years + d2.years;
     m = d1.months + d2.months;
     d = d1.days + d2.days;
   
     printf ("\n\n\nSum of the date's is %d/%d/%d",d,m,y);
}

Output

Date Sum Using Structure in C/C++ Language
Date Sum Using Structure in C/C++ Language

Read More..

Sunday, September 24, 2017

Input Length and Breadth. Calculate Area and Perimeter Using Struct in C Language

0 comments

Problem:

Write a program which takes length and breadth as input. Than calculate area and perimeter of the room.

Code

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

struct distance{
int length;
int width;
};
struct room{
struct distance a;
}r;

void myftn();

int main()
{
myftn();


printf ("\n\nPress any key to exit.");
getch();
return 0;
}

void myftn ()
{
int p,s;

printf ("Enter lengtgh: ");
scanf ("%d",&r.a.length);
printf ("Enter widtgh: ");
scanf ("%d",&r.a.width);

p = 2 * (r.a.length * r.a.width);
s = (r.a.length * r.a.width);

printf ("\n\nArea = %d",s);
printf ("\nPerimeter = %d",p);
}

Output

Input Length and Breadth. Calculate Area and Perimeter Using Struct in C Language
Input Length and Breadth. Calculate Area and Perimeter Using Struct in C Language



Read More..

Sunday, September 17, 2017

Convert Fahrenheit to Celsius | Convert Celsius to Fahrenheit

0 comments

Problem

Write two functions in a program of C language to convert:

  • Function 1: Fahrenheit to Celsius
  • Function 2: Celsius to Fahrenheit

Code


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

void ftoc();
void ctof();

int main()
{
ftoc();
        ctof();
    
printf ("\n\nPress any key to exit.");
getch();
return 0;
}

void ftoc()
{
float f,c;
printf ("Enter temperature in Fahrenheit: ");
scanf ("%f",&f);

c = 5.0 / 9.0 * (f-32.0);

printf ("Temperature in celsius: %f",c);
}

void ctof()
{
float f,c;
printf ("\n\nEnter temperature in celsius: ");
scanf ("%f",&c);

f = ((9.0 / 5.0 * c) + 32.0);

printf (" Temperature in celsius: %f",f);
}

Output

Convert Fahrenheit to Celsius | Convert Celsius to Fahrenheit
Convert Fahrenheit to Celsius | Convert Celsius to Fahrenheit

Read More..

Friday, March 10, 2017

find index number of the largest element using class

0 comments

Problem

Start with a program that allows the user to input a number of integers, and then stores them in an int array. Write a function called maxint() that goes through the array, element by element, looking for the largest one. The function should take as arguments the address of the array and the number of elements in it, and return the index number of the largest element. The program should call this function and then display the largest element and its index number.

Code

// p[4].cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include<conio.h>
#include<iostream>
#include<string.h>
using namespace std;

class intgrs
{
private:
int a[5];
int i,j,z;

public:
intgrs():j(0),i(0),z(0)
{ }

void maxint()
{
cout<<"Enter Integers: ";
for(i=0; i<5; i++)
{
cin>>a[i];
}
for(i=0;i<5;i++)
{
if(a[i]>j)
{
j = a[i];
z=i;
}
}
cout<<"\n\nLargest Element = "<<j;
cout<<"\n\nIndex Number = "<<(z+1)<<endl;
}
};

int main()
{
intgrs i;

i.maxint();

cout<<"\n\nPress any key to close.";
getch();
return 0;
}

Output

find index number of the largest element using class

Read More..

Wednesday, September 17, 2014

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..

Sunday, September 14, 2014

create a menu driven library management program using structure and functions

0 comments

Problem

Write a menu driven program that depicts the working of a library. The menu options should be:
  1. Add book information
  2. Display book information
  3. List all books of given author
  4. List the title of specified book
  5. List the count of books in the library
  6. List the books in the order of accession number
  7. Exit
Create a structure called library to hold accession number, title of the book, author name, price of the book, and flag indicating whether book is issued or not. Create an array of size 25 of type library. Input record from user and check all the options of the program.

Solution - Code

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

void add_book();
void disp_book();
void book_auth();
void count_book();

struct lib
{
 int acc_no;
 char book_title[20];
 char author[20];
 int cost;
}b[100];
int count;
main()
{
 int ch;
 while(1)
 {
  printf("\n 1:enter the book info\n");
  printf("\n 2:display book info\n");
  printf("\n 3:book author\n");
  printf("\n 4:count book\n");
  printf("\n 5:exit\n");
  printf("\n enter the choice \n");
  scanf("%d",&ch);
  switch(ch)
  {
   case 1:
    add_book();
    getch();
    break;
   case 2:
    disp_book();
    getch();
    break;
   case 3:
    book_auth();
    getch();
    break;
   case 4:
    count_book();
    getch();
    break;
   case 5:
    exit(0);
  }
 }
}
//-----------------------------------
void add_book()
{
 if(count==9)
 {
  printf("\n no more space\n");
  return;
 }
 printf("\n enter the detail of book \n");
 printf("\n enter accession number of book =");
 scanf("%d",&b[count].acc_no);
 fflush(stdin);
 printf("\n enter the book title=");
 gets(b[count].book_title);
 fflush(stdin);
 printf("\n enter the name of author=");
 gets(b[count].author);
 fflush(stdin);
 printf("\n enter the cost of book=");
 scanf("%d",&b[count].cost);
 count++;
}
//***********************
void disp_book()
{
 int i;
 printf("\n detail of %d booksin library",count);
 for(i=0;i<count;i++)
 {
  printf("\n %d\n%s\n%s\n%d",b[i].acc_no,b[i].book_title,b[i].author,b[i].cost);
 }
}
//*********************
void book_auth()
{
 int i,cnt=0;
 char name[20];
 printf("\n enter the name of author=");
 gets(name);
 for(i=0;i<count;i++)
 {
  if(strcmp(name,b[i].author)==0)
  {
   cnt++;
   printf("\n %d\n%s\n%s\n%d",b[i].acc_no,b[i].book_title,b[i].author,b[i].cost);
  }
 }
 if(cnt==0)
  printf("\n no such book \n");
}
//*******************************
void count_book()
{
 printf("\n total no of book in library =%d",count);
}

Sample Output

add book record c language

display library books

display total library books in c language

Read More..

write a program that allows the user to input data and search

0 comments

Problem

Create a structure to specify data on students given below:
Roll number, Name, Department, Course, Year of joining
Create an array of structure. Use a loop to input values and then display those values. Also write a function:
void searchstudent (int roll_no);
This function will search a student record from that array whose Roll number is same as roll_no and display that record otherwise it displays record not found.

Solution - Code

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
void searchstudent (int roll_no);
struct book
{
 int roll;
 char name[20];
 char department[20];
 char course[20];
 int year;
}b[2];
int main ()
{
 int i, roll_no;

 for (i=0;i<2;i++)
 {
  printf ("Enter roll no. : ");
  scanf ("%d",&b[i].roll);

  fflush (stdin);
  printf ("Enter name: ");
  gets (b[i].name);

  fflush (stdin);
  printf ("Enter department: ");
  gets (b[i].department);

  fflush (stdin);
  printf ("Enter course: ");
  gets (b[i].course);

  printf ("Enter year: ");
  scanf ("%d",&b[i].year);
 }

 for (i=0;i<2;i++)
  printf("\n\nRoll number: %d\nName: %s\nDepartment: %s\nCourse: %s\nYear: %d\n",b[i].roll,b[i].name,b[i].department,b[i].course,b[i].year);

 printf ("SEARCH DATA(enter roll number): ");
 scanf ("%d",&roll_no);

 searchstudent(roll_no);

 printf ("\npress any key to close.");
 getch();
}
void searchstudent (int roll_no)
{
 int i;

 for (i=0;i<2;i++)
 {
  if (roll_no == b[i].roll)
   printf("\n\nRoll number: %d\nName: %s\nDepartment: %s\nCourse: %s\nYear: %d\n",b[i].roll,b[i].name,b[i].department,b[i].course,b[i].year);
 }
}

Sample Output

search of a student using structure

Read More..

Tuesday, September 9, 2014

search for a user (student) input data by using an array of structure, add user roll number, name, department, course, and year of joining by declaring a function name searchstudent(int)

0 comments

Problem

Create a structure to specify data on students given below:
  • Roll number
  • Name
  • Department
  • Course
  • Year of joining
Create above program by declaring an array of structure., input some value from user and then display those values. Also write a function:
void searchstudent (int roll_no);
This function will search a student record from that array whose Roll number is same as roll_no and display that record otherwise it displays record not found.

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;
};
void searchstudent (struct student *, int );

int main()
{

 struct student s[5];
 int i,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);
 }

 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);
 }

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

 searchstudent (s ,rl);

 system("pause");
}
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

search user roll# name using array of structure

Read More..

Sunday, September 7, 2014

find how many times a character appears in a string

0 comments

Problem

write a c program that will ask the user to input a character and then the program will check whether the character (how many times) exists in the following string or not.

Note: use pointers to find the character in the string character by character.

Solution - Code

#include<stdio.h>
#include<conio.h>
int countchar(char *p, char a);
main()
{
 int count;
 char a;
 char p[]="OMG! its hot.";
 printf("Enter the character : ");
 scanf("%c",&a);
 count = countchar(p,a);
 printf("\n\nCharacter %c occured in the array %d times.\n",a,count);
 system("pause");
}
int countchar(char *p, char a)
{
 int count=0;
 while (*p != '\0')
 {
  if (a == *p)
   count++;
  p++;
 }
 return (count);
}

Sample Output


find character in a string

Read More..

determine the number of characters in a string

0 comments

Problem

write a program in c language that passes array index as an argument to a user defined function.The function then counts the number of characters in that string and displays it to the user.

Note: use pointers to count the string character by character.

Solution - Code

#include<stdio.h>
#include<conio.h>
int mystrlen(char *p);
main()
{
 int count;
 char p[]="Honesty is the best policy.";
 count = mystrlen(p);
 printf("\n\nNumber of characters in the string = %d\n\n",count);
 system("pause");
}
int mystrlen(char *p)
{
 printf ("\n");
 int count = 0;
 while (*p != '\0' )
 {
  printf ("%c",*p);
  p++;
  count++;
 }
 return (count);
}

Sample Output


number of characters in a string

Read More..

print a string character by character in a user defined function where the string is declared in main

0 comments

Problem

write a program in c language that passes array index as an argument to a user defined function and print the string in that function character by character.

Note: use pointers to print the string character by character.

Solution - Code

#include<stdio.h>
#include<conio.h>
void display(char *p, char end);
main()
{
 char p[]="We are gonna be the best programmers ever.";
 char end= '\0';
 display(p,end);
 printf ("\n\n",*p);
 system("pause");
}
void display(char *p, char end)
{
 printf ("\n",*p);
 while (*p != end)
 {
  printf ("%c",*p);
  p++;
 }
}

Sample Output


display string character by character

Read More..

print a string in a user defined function, where string is declared in main function

0 comments

Problem

write a program in c language that passes array index as and argument to a user defined function and print the string in that function.

Solution - Code

#include<stdio.h>
#include<conio.h>
void display(char *p);
main()
{
 char p[]="Mr.Maaz Ali is a good boy.";
 display(p);
 system("pause");
}
void display (char *p)
{
 printf("\n%s\n\n",p);
}

Sample Output

print string in a function, defined in main

Read More..

Friday, September 5, 2014

display all elements of an array, use pointers to find maximum and minimum element in an array.

0 comments

Problem

Write three user define functions:
  1. void display(int *p, int size);
    The first parameter is the base address of an array and the second parameter is the size of array
    (No of elements in an array). The function displays all the elements in the array. Call this
    function from main function.
  2. int max_value (int *p, size);
    this function returns the maximum element of the array pointed by the pointer p. second
    parameter specify the size of array.
  3. int min_value (int *p, size);
    this function returns the minimum element of the array pointed by the pointer p. second
    parameter specify the size of array.

Solution - Code

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void display(int *,int);
int max_value (int *,int);
int min_value (int *,int);
main()
{
 int max,min,size,i,x,sum=0,avg;
 int num[5];
 size = 5;
 display(num,size);
 max = max_value (num,size);
 printf ("Maximum element : %d\n",max);
 min = min_value (num,size);
 printf ("Minimum element : %d\n",min);
 system("pause");
}
void display(int *p,int size)
{
 int i,num[size],sum=0,avg;
 for (i=0;i<5;i++)
 {
  printf("Enter numbers: ");
  scanf("%d",&num[i]);
 }
 for (i=0;i<5;i++)
 {
  sum = sum + num [i];
 }
 avg =(sum/5);
 printf ("sum = %d\n",sum);
 printf ("average = %d\n",avg);
}
int max_value (int *p,int size)
{
 int num[size];
 int i,max=num[0];
 for (i=0;i<5;i++)
 {
  printf("Enter numbers: ");
  scanf("%d",&num[i]);
 }
 for (i=0;i<5;i++)
 {
  if (max < num[i])
  max=num[i];
 }
 return max;
}
int min_value (int *p,int size)
{
 int num[size];
 int i,min=num[4];
 for (i=0;i<5;i++)
 {
  printf("Enter numbers: ");
  scanf("%d",&num[i]);
 }
 for (i=5;i>=0;i--)
 {
  if (min > num[i])
  min=num[i];
 }
 return min;
}

Sample Output

find maximum and minimum element c language

Read More..

Thursday, September 4, 2014

find the greatest common divisor

0 comments

Problem

Write a function to compute the greatest common divisor given by Euclid’s algorithm, exemplified for J = 1980, K = 1617 as follows:
1980 / 1617 = 1   1980 – 1 * 1617 = 363
1617 / 363 = 4   1617 – 4 * 363 = 165
363 / 165 = 2   363 – 2 * 165 = 33
165 / 33 = 5   165 – 5 * 33 = 0
Thus, the greatest common divisor is 33.

Solution - Code

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void gcd();
int main()
{
 gcd();
 system("pause");
 return 0;
}
void gcd()
{
 int j,k,x,y,z,a,b,c,d,e ;
 printf("Enter two numbers of 4 digit : ");
 scanf("%d%d",&j,&k);
 x = j/k ;
 y = j-x*k;
 z = k/y ;
 a = k-z*y;
 b = y/a;
 c = y-b*a;
 d = a/c;
 e = a-d*c;
 printf("The greatest common divisor : %d\n",c);
}

Sample Output

greatest common divisor c language

Read More..

write functions for dividing two numbers, finding larger number, and also prints the ATM menu

0 comments

Problem

Write a function prototype and definition for the following components:
  • A function that divides two numbers and returns the remainder
  • A function that finds the larger of two numbers and returns the result
  • A function that prints an ATM menu (it receives no parameters and returns no value)
Write main function and call the above function and check if it works as required or not.

Solution - Code

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void divi();
void lrg();
void atm();
main()
{
 divi();
 lrg();
 atm();
 system("pause");
}
void divi()
{
 int r,x,y;
 printf ("Enter two numbers : ");
 scanf ("%d%d",&x,&y);
 r = x%y;
 printf("The remainder : %d\n",r);
 system("pause");
}
void lrg()
{
 int x,y;
 printf("Enter two numbers : ");
 scanf("%d%d",&x,&y);
 if (x > y)
 printf ("%d is greater than %d\n",x,y);
 else
  printf ("%d is greater than %d\n",y,x);
 system("pause");
}
void atm()
{
 printf("1.cash withdrawl\n");
 printf("2. Fast cash\n");
 printf("3. Balance inquiry\n");
 printf("4.Transictiobn receipt\n");
}

Sample Output

functions c language

Read More..

create a menu driven program to add, subtract, multiply, and divide two number using functions and switch case statement

1 comments

Problem

Write a menu driven program using switch statement that has the following options:
  1. Add numbers
  2. Subtract numbers
  3. Multiply numbers
  4. Divide numbers
  5. Exit
The prototype of the function for add, subtract, multiply and divide is as follows:
int add(int, int);
int sub(int, int);
int mul(int, int);
int div(int, int);

Solution - Code

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>

int add (int, int);
int sub (int, int);
int mul (int, int);
int divi (int, int);

int main()
{
 int i,x,y,c;
 printf("Enter two numbers : ");
 scanf ("%d %d",&x,&y);
 printf("1. addition\n");
 printf("2. subtraction\n");
 printf("3. multiplication\n");
 printf("4. division\n");
 printf("5. exit\n\n");
 printf("Which action you want to perform : ");
 scanf ("%d",&i);
 switch(i)
 {
  case 1:
   c=add(x , y);
   printf("Answer = %d\n",c);
   system("pause");
   break;
  case 2:
   c=sub(x , y);
   printf("Answer = %d\n",c);
   system("pause");
   break;
  case 3:
   c=mul(x , y);
   printf("Answer = %d\n",c);
   system("pause");
   break;
  case 4:
   c=divi(x,y);
   printf("Answer = %d\n",c);
   system("pause");
   break;
  case 5 :
   exit(0);
   break;
  default :
   printf("Enter valid number\n");
   system("pause");
 }
}
int add(int x, int y)
{
 int c;
 c = (x+y);
 return c ;
}
int sub(int x, int y)
{
 int c;
 c = (x-y);
 printf("Answer = %d\n",c);
}
int mul(int x, int y)
{
 int c;
 c = (x*y);
 printf("Answer = %d\n",c);
}
int divi(int x, int y)
{
 int c;
 c = (x/y);
 printf("Answer = %d\n",c);
}

Sample Output

switch case statement

Read More..

Write a function to obtain the prime factors of this number.

1 comments

Problem

A positive integer is entered through the keyboard. Write a function to obtain the prime factors of this number.

Solution - Code

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void pr_fact();
main()
{
 pr_fact();
 system("pause");
}
void pr_fact()
{
 int x,y,i;
 printf("Enter any integer number : ");
 scanf("%d",&x);
 printf("Prime factors of %d : \n",x);
 i = 1;
 while( i <= x )
 {
  if ( x%i==0 )
   printf("%d\n",i);
  i++;
 }
}

Sample Output


Read More..

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