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, October 1, 2017

Time Sum Using Structure in C/C++ Language

1 comments

Problem

Create a structure called time. Its three members, all type int, should be called hours, minutes, and seconds. Write a program that prompts the user to enter a time value in hours, minutes, and seconds. This can be in 12:59:59 format, or each number can be entered at a separate prompt (“Enter hours:”, and so forth). The program should then store the time in a variable of type struct time, and finally print out the total number of seconds represented by this time value:
long totalsecs = t1.hours*3600 + t1.minutes*60 + t1.seconds

Code

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

struct time {
int hours;
int minutes;
int seconds;
};

//function prototype
void add (int,int,int,int,int,int);

int main()
{
    struct time t1,t2;
   
    //1st time
    printf ("Enter 1st time.");
   
printf ("\n\nEnter Hours: ");
scanf ("%d",&t1.hours);

printf ("\nEnter Minutes: ");
scanf ("%d",&t1.minutes);

printf ("\nEnter Seconds: ");
scanf ("%d",&t1.seconds);

    printf ("\n\nThe Time is %d:%d:%d",t1.hours,t1.minutes,t1.seconds);
   
    //Second time
    printf ("\n\n\nNow, please type the 2nd time.");
   
    printf ("\n\nEnter Hours: ");
scanf ("%d",&t2.hours);

printf ("\nEnter Minutes: ");
scanf ("%d",&t2.minutes);

printf ("\nEnter Seconds: ");
scanf ("%d",&t2.seconds);

    printf ("\n\nThe Time is %d:%d:%d",t2.hours,t2.minutes,t2.seconds);
   
    //function call
    add (t1.hours,t1.minutes,t1.seconds,t2.hours,t2.minutes,t2.seconds);
   
printf ("\n\nPress any key to exit.");
getch();
return 0;
}

//function definition
void add (int x,int y,int z,int a,int b,int c)
{
     int h,m,s;
   
     h = x + a;
     m = y + b;
     s = z + c;
   
     printf ("\n\n\nSum of the two time's is %d:%d:%d",h,m,s);
}

Code

Time Sum Using Structure in C/C++ Language
Time 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..

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