How do you reverse a 6 digit number in C plus plus?
The number of digits is immaterial, the principal is exactly the same. The key to reversing any number is in the following user-defined function:
uint reverse(uint num)
{
uint rev=0;
while(num)
{
rev*=10;
rev+=num%10;
num/=10;
}
return(rev);
}
The following code demonstrates how to reverse a 6 digit number entered from the console.
#include<iostream>
#include<string>
#include<sstream>
typedef unsigned int uint;
uint reverse(uint num)
{
uint rev=0;
while(num)
{
rev*=10;
rev+=num%10;
num/=10;
}
return(rev);
}
int getrange(uint digits,uint& min,uint&max)
{
if(!digits)
return(-1);
min=1;
while(--digits)
min*=10;
max=min*10-1;
return(0);
}
uint inputnumber(const std::string prompt,const uint min,const uint max)
{
using namespace std;
uint result=0;
while(1)
{
cout<<prompt;
string s;
getline(cin,s);
if(s.size())
result=stoul(s,0,10);
if(result<min result>max)
cout<<"The number must be in the range "
<<min<<".."<<max<<"\n"
<<"Please enter another number.\n"<<endl;
else
break;
}
return(result);
}
uint inputnumber(const uint digitcount)
{
using namespace std;
uint min=0, max=0;
if(getrange(digitcount,min,max)==-1)
return(0);
std::stringstream prompt;
prompt<<"Enter a "<<digitcount<<" digit number: ";
return(inputnumber(prompt.str(),min,max));
}
int main()
{
using namespace std;
uint num=inputnumber(6); // 6 digits.
if(num)
{
uint rev=reverse(num);
cout<<"Reverse: "<<rev<<"\n"<<endl;
}
return(0);
}
Output:
Enter a 6 digit number: 123456
Reverse: 654321
How can we write program in c on secant method explain with example?
#include<iostream>
#include<cmath>
#include<fstream>
using namespace std;
double f(double x);
int main()
{
double dX0, dX1, dX2, dInterations = 0, dTolerance;
cout << "What is the first root guess? ";
cin >> dX0;
cout << "What is the second root guess";
cin >> dX1;
do {
dX2 = dX1 - ((f(dX1) * (dX1 -dX0)) / (f(dX1) - f(dX0)));
dX0 = dX1;
dX1 = dX2;
dTolerance = fabs(dX2 - dX1);
dInterations++;
}
while (dInterations <= 100 dTolerance >= .000001);
cout << "One of the roots is " << dX2 << endl;
}
double f(double x)
{
double dReturn;
dReturn = (3 * x * x) + (2 * x) -2;
return dReturn;
}
Declare Names[100], Salaries[100]
Set Sum = 0
Set Count1 = 0
Set K = 1
Write "Enter Employees Name and Salary."
Write "Enter *,0 when done."
Input Name, Salary
While Salary <> "0"
Set Count1 = Count1 + 1
Set Sum = Sum + Salary
Write "Enter another Employee Name and Salary (enter *,0 when done)"
Input Name, Salary
End While
Set Average = Sum / Count1
Set Count2 = 0
Set Count3 = 0
For K = 1 Step 1 to Count1
If Salary[K] > Average Then
Set Count2 = Count2 + 1
End If
If Salary[K] < Average Then
Set Count3 = Count3 + 1
End If
End For
Write "The Average Salary is: $", Average
Write "The number of Salaries Above theAverage Salary is:", Count2
Write "The number of Salaries Below theAverage Salary is:", Count3
How to create user define function of my name in c?
You need to declare it first. A function declaration is made of three parts:
The kind of data the function returns, I.E. void, int, etc.
The name of the function, I.E. helloWorld.
The list of parameters in parentheses, I.E. (A as int, B as char)
For an example, here it is:
void helloWorld();
Now, to put something inside the function, you can write the following:
void helloWorld()
{
printf("Hello world!");
}
Similarities between struct and union?
Struct and Union have following similarities:- Both are aggregated data types that allow a mixed for values to be contained in their instances.They are the way aggregation is done in C for making. The difference between them is that union allows the different items to be in it but allows access to be on one becase they are represented in overlapped manner. A Struct is also aggregation but it separates each data item contained in it and therefore anyone can be accessed anytime from its instance variable.Unions are ordinarily used because of performance and generalization ,while the structures are generally used for aggregation. For queries Rupesh Kumar Joshi ,rupesh_joshi@sify.com
Write aprogram in c to calculate the cube of given integer number using function prototype?
#include <stdio.h>
int findcube(int); // prototype
int main()
{
int a,b;
printf("Enter a number: ");
scanf("%d",&a);
b=findcube(a);
printf("The cube of %d is %d\n",a,b);
}
int findcube(int x)
{
return x*x*x;
}
State and explain any 4 predefined data types in C plus plus programming?
A char is always 1 byte long, but may be signed or unsigned depending on the implementation. Explicitly signed or unsigned char types are treated as being independent types from the implicit char type (3 separate types).
A short (or short int) is at least 2 bytes long while a long (or long int) is at least 4 bytes long. Both are implementation-dependent with regard their actual size and sign, but, like char, may be explicitly signed.
Char, short and long are used to store integers (whole numbers). A char can store signed values in the range -128 to +127, or unsigned values in the range 0 to 255.
A double is at least 8 bytes long while its shorter counterpart, a float, is at least 4 bytes long. Both are implicitly signed, floating point values.
To determine the length of any type (including predefined types) us the sizeof operator.
A double ended queue (or deque ) is a queue where insertion and deletion can be performed at both end that is front pointer can be used for insertion (apart from its usual operation i.e. deletion) and rear pointer can be used for deletion (apart from its usual operation i.e. insertion). So when we need to insert or delete at both end we need deque.
What is the source code in C to convert Celsius to Fahrenheit?
#include<io.sys>
// Temperature conversion functions:
float celcius_to_fahrenheit (double c) { return c * 9.0 / 5.0 + 32.0; }
float celcius_to_kelvin (double c) { return c + 273.150; }
float fahrenheit_to_celsius (double f) { return (f - 32.0) * 5.0 / 9.0; }
float fahrenheit_to_kelvin (double f) { return celsius_to_kelvin (fahrenheit_to_celsius (f));
float kelvin_to_celsius (double k) { return k - 273.150; }
float kelvin_to_fahrenheit (double k) { return celsius_to_fahrenheit ( kelvin_to_celsius (k));
int main (void) {
double c, f;
scanf ("Enter a temperature in Celsius: %f", c);
printf ("%f Celsius is equal to %f Fahrenheit\n", c, celcius_to_fahrenheit (c));
return 0;
}
Which type of data is used to explain a relationship?
A foreign key constraint is used to represent a relationship in a RDBMS (relational database management system).
Difference between BufferedWriter and FileWriter in java?
A newLine() method is provided, which uses the platform's own notion of line separator as defined by the system property line.separator. Not all platforms use the newline character ('\n') to terminate lines. Calling this method to terminate each output line is therefore preferred to writing a newline character directly, FileWriter doesn't have this feature. ALso BufferedWriter are fast as they write to buffer first before writing to file
Why wr are using c programming?
C/C++ is used to write system softwares. The whole of UNIX operating system is written in C language.
Substring in c without string functions?
#include
When you use Visual C plus plus which window do you use to change the attributes of a form?
There should be "Properties" window. If you do not see, you can make it visible if you go to "View"-> "Properties".
Program in c to get the sum of n numbers present in an array using one dimensional array?
#include<stdio.h>
#include<conio.h>
main()
{
int num, n,sum=0,i=0;
printf("enter the number of elements to be added ");
scanf("%d",&n);
printf("enter %d numbers",n);
while(i<n)
{
scanf("%d",&num);
sum=sum+num;
i=i+1;
}
printf("the sum of %d numbers is %d",n,sum);
getch();
}
A global declaration of a function or variable is a declaration that is at the global scope of the program, not inside another function. This means that the name will be visible within all functions in the program.
Type C gypsum board meets and exeeds the fire resistance requirements of a type x board. It has mineral additives in the gypsum core that expands when exposed to heat. This expansion holds the board together longer during fire exposure. It is referred to by the Gypsum Association as "Improved Type X" board.
write a program in C that prompts the user with the following lines:
a) Add two integers
c) Compare two integers for the larger
t) Test an integers for odd or even
q) Quit
What are 3 world events between 428 B.C. - 347 B.C.?
Plato was born in 428 B.C. and died in 347 B.C. so that's got to count for two! :P
Limitations of queue in data structure?
A queue is a particular kind of collection in which the entities in the collection are kept in order and the principal operations on the collection are the addition of entities to the rear terminal position and removal of entities from the front terminal position. This makes the queue a First-In-First-Out (FIFO) data structure. In a FIFO data structure, the first element added to the queue will be the first one to be removed.
Some Applications:
1) Serving requests of a single shared resource (printer, disk, CPU), transferring data asynchronously (data not necessarily received at same rate as sent) between two processes (IO buffers), e.g., pipes, file IO, sockets.
2) Call center phone systems will use a queue to hold people in line until a service representative is free.
3) Buffers on MP3 players and portable CD players, iPod playlist. Playlist for jukebox - add songs to the end, play from the front of the list.
4) When programming a real-time system that can be interrupted (e.g., by a mouse click or wireless connection), it is necessary to attend to the interrupts immediately, before proceeding with the current activity. If the interrupts should be handles in the same order they arrive, then a FIFO queue is the appropriate data structure.
#include<stdio.h>
int main()
{
int side,area of square;
print f("Enter the length of side\n");
scan f("%d",&side);
area of square=side*side;
print f(the area of square is %d\n"area of square);
return 0;
}
Some of ways to solve this problem is to use four nested loops for().
Something like following:
...
const int limit = 1000;
//some code here
for (int i(0); i <= limit; i++)
{
for (int j(0); j <= limit; j++)
{
for (int k(0); k <= limit; k++)
{
for (int l(0); l <= limit; l++)
{
if ((i + j + k) == l)
{
cout << endl << "i + j + k = l and equals to " << l << endl;
}
cout << "ijkl equals to " << (i*j*k*l) << endl;
}
}
}
}
...
//some code here
How do you write a c program to analysis a circuit?
There is no need to write a C program for circuit analysis; there are lots of packages out there that already do that (for example, Spice).
Can include files be nested in c?
Yes, include files can be nested in C and C++. In fact, most library implementations do just that.
What is meant by heap in c or cpp?
If you see the word "heap" in the context of C/C++ programming, it is probably referring to one of two ideas.
First, if it is written as "the heap", it is probably referring to dynamically allocated memory. We conceptualize memory as either being on "the stack" or "the heap" in main memory. Memory allocation from the heap happens when a call to malloc (or similar functions) are called in C, or when the "new" operator is used in C++. This is in contrast to statically allocated memory, which comes from the load module and is known at compile-time, or from the "stack" which is used at run-time to allocate local scope, or automatic, memory.
Another usage of the word heap is a certain data structure called a heap. It is a very common data structure for priority queues and is crucial to the famous HeapSort algorithm. You can easily find more information on this data structure e.g. by searching for HeapSort.