answersLogoWhite

0

📱

C Programming

Questions related to the C Computer Programming Language. This ranges all the way from K&R to the most recent ANSI incarnations. C has become one of the most popular languages today, and has been used to write all sorts of things for nearly all of the modern operating systems and applications. It it a good compromise between speed, power, and complexity.

9,649 Questions

Write a program to find median of n numbers?

import java.util.Arrays;

class Solution {

public int median(int[] A) {

int[] b = new int[A.length];

System.arraycopy(A, 0, b, 0, b.length);

Arrays.sort(b);

if (A.length % 2 == 0) {

return b[b.length / 2];

} else {

return (int) ((b[(b.length / 2) - 1] + b[b.length / 2]) / 2.0);

}

}

}

Write a program to find the largest of three numbers?

This is best done with an array; that way, you can easily extend to more than three numbers. In Java, it would be something like this:

int myNumbers[] = {1, 5, 3};

int max = myNumbers[1];

for (int i = 1; i < myNumbers.length(); i++)
if (myNumbers[i] > max) max = myNumbers[i];



This is best done with an array; that way, you can easily extend to more than three numbers. In Java, it would be something like this:

int myNumbers[] = {1, 5, 3};

int max = myNumbers[1];

for (int i = 1; i < myNumbers.length(); i++)
if (myNumbers[i] > max) max = myNumbers[i];



This is best done with an array; that way, you can easily extend to more than three numbers. In Java, it would be something like this:

int myNumbers[] = {1, 5, 3};

int max = myNumbers[1];

for (int i = 1; i < myNumbers.length(); i++)
if (myNumbers[i] > max) max = myNumbers[i];



This is best done with an array; that way, you can easily extend to more than three numbers. In Java, it would be something like this:

int myNumbers[] = {1, 5, 3};

int max = myNumbers[1];

for (int i = 1; i < myNumbers.length(); i++)
if (myNumbers[i] > max) max = myNumbers[i];

C program that will ask the radius of a circle in inches ant then computes the area and circumstances of the circle?

This looks suspiciously like homework to me, so I'll give you a few pointers:

The formula for the circumference of a circle is 2{pi}r, and the area is {pi}r2.

As you'll be using floating point and mathematical functions, you'll need to link with the math library (eg cc -lm -o circles circles.c)

I don't think there's a built in pi (having done very little programming in c with trignometric functions I can't be sure), so I use:

double pi = acos(-1.0);

as cos({pi}) = -1.

Your main loop will entail printing a suitable message, getting the input, checking an exit condition, converting the input to a double and then passing that to a function to calculate and print the circumference and areas. The function itself could do extra checking, eg for zero or negative radius and return an error code.

Don't forget to include the necessary header files.

How do you write a algorithm for finding maximum of 2 numbers in c?

You can use the ternary operator, in an expression such as:

result = a > b ? a : b;

This is equivalent to:

if (a > b)

result = a;

else

result = b;

What is the Program to find sum of first 10 natural numbers in c?

/* Obvious method: */

#include <stdio.h>

int main(int argc, char *argv[]){

int n, tally = 0;

for(n = 1; n <= 10; n++){

tally += n;

}

printf("the total is %i.\n", tally);

return 0;

}

/* Better method: */

#include <stdio.h>

int main(int argc, char *argv[]){

int numyears = 10;

printf("the total is %i.\n", numyears * (numyears + 1) >> 1);

return 0;

}

Name any uses for dot product or cross product in video games?

The cross product is used to find surface normals of triangles (the building blocks of objects in 3D games). Those surface normals can then be used in dot product tests with the camera to test if the normal is facing the camera or not. If the dot product angle is positive then the normal is facing the same direction as the camera, so the triangle does not need to be drawn (because it cannot be seen). If the angle is negative, then the two vectors are pointing at each other and the triangle may need to be rendered. These methods not only apply to camera viewing, but also to lighting and physics calculations as well.

What is c code to check a number is real or not?

#include

double complex complex_value;

if (cimag (complex_value)==0.0)) puts ("Real");

Write a program which takes any 4 numbers from users the program should display the sum of squares of these numbers?

/*

I'm using four spaces instead of tab because I'm writing this on an iPhone.

*/

#include

#include

int main() {

int num1, num2, num3, num4, sum;

printf("Please enter first number: ");

scanf("%d", num1);

printf("Please enter second number: ");

scanf("%d", num2);

printf("Please enter third number: ");

scanf("%d", num3);

printf("Please enter fourth number: ");

scanf("%d", num4);

sum = pow(num1, 2) + pow(num2, 2) + pow(num3, 2) + pow(num4, 2);

printf("The answer is %d", sum);

return 0;

}

Why does array index begins with 0 not with 1?

It is because array name implies its address and if you want to access first element of it pointer address logic is as below:

Arrays first element address = array base address + 0

Arrays second element address = array base address + 1

How do you write the program to find absolute value using if else in c language?

I'll help you with some pseudo-code (NOT ACTUAL CODE, DO NOT TRY TO COMPILE):

int absvalue(int x)

if x is below 0

return x * -1

else

return x

Also, I suggest you do your homework yourself.

Write a program in C to find the complex number of a given number?

There is no such thing; you seem to have misunderstood something.

Any real number can be regarded as a complex number with zero imaginary part, eg.: 5 = 5+0i

How do you find the LCM in C?

Problem Statement for LCM:

Enter Two digits by a user and get its LCM

Answer:

#include

#include

void main()

{

int x,y,i,j;

clrscr();

printf("Enter Two Digits");

scanf("d",&x,&y);

for(i=1;i<=x;i++)

{

for(j=1;j<=y;j++)

{

if(y*i==x*j)

{

printf("LCM is %d\n",y*i);

i=i+x;

}

}

}

getch();

}

OR

there will be another easy way to solve it...

Answer:

#include

#include

void main()

{

int x,y,i,;

clrscr();

printf("Enter Two Digits = ");

scanf("d",&x,&y);

for(i=1;i<=x*y;i++) {

if(i%x==0&&i%y==0)

{

printf(LCM is %d",i);

break;

}

}

getch();

}

Write a program to number the lines in a file?

You can number the lines in a file using a simple Python program. Here's an example:

with open('input.txt', 'r') as infile, open('output.txt', 'w') as outfile:
    for line_number, line in enumerate(infile, start=1):
        outfile.write(f"{line_number}: {line}")

This code reads from input.txt, numbers each line, and writes the numbered lines to output.txt. The enumerate function is used to keep track of the line numbers, starting from 1.