Not tested:
void printPrimes(int n) {
int a = 0, b = 0;
float c = 0.0f;
bool isp = true;
int primes = 0;
while(true) {
a++;
do {
b++;
if(b > 1) {
c = (float)a/(float)b;
if(c = (int)c)
if(b != a) {
isp = false;
break;
}
else
isp=true;
}
} while(b<a);
if(isp) {
primes++;
std::cout << a << endl;
}
if(primes > n)
break;
b = 0;
c = 0;
isp = true;
}
}
void main() {
printPrimes(10);
}
Wap to print prime factor of a number?
import java.io.*; public class p_factor
{
public static void main(String args[])throws IOException
{
int arr[]=new int[50];
int n,i,j=0,k,f=0;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter a number");
n=Integer.parseInt(br.readLine());
for(i=2;i<n;i++)
{
if(n%i==0)
{
arr[j]=i;
j++;
}
}
for(i=0;i<j;i++)
{
for(k=2;k<arr[i];k++)
{
if(arr[i]%k==0)
{
f=1;
}
}
if(f==0)
{
System.out.println(arr[i]);
}
}
}
}
How do you convert binary number base2 to decimal base10?
Multiply the digit to the left of the "decimal" point by 2^0 = 1.
Multiply the digit to the left of it by 2^1 = 2
Multiply the digit to the left of that by 2^2 = 4 and so on.
Also
Multiply the digit to the right of the "decimal" point by 2^(-1) = 1/2.
Multiply the digit to the right of that by 2^(-2) = 1/4 and so on.
Add all these together.
Example:
Binary 1101.1011
1*2^3 = 1*8 = 8
1*2^2 = 1*4 = 4
0*2^1 = 0*2 =0
1*2^0 = 1*1 = 1
1*2^-1 = 1*1/2 = 0.5
0*2^-2 = 0*1/4 = 0
1*2^-3 = 1*1/8 = 0.125
1*2^-4 = 1*1/16 = 0.0625
Sum = 13.6875
Happiness
How can you generate a palindrome from a given number?
You write the number and then follow it with the digits in reverse order.
What is the value of 1928 series b serial g22292336a?
Please post a new question with the bill's denomination and condition. Note that the serial number is unimportant to its value.
C program to find LCMof three integers?
int LCM3 (int a, int b, int c)
{
return LCM2 (a, LCM2 (b, c));
}
int LCM2 (int a, int b)
{
return a*b/GCD2(a, b);
}
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];
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");
What is the answer to Convert the binary number 11000101 to its equivalent decimal number?
11000101 = 197
/*
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;
}
How do you print the distance between 2 points in c language?
void PrintDist (double d)
{
printf ("the distance is %g", d);
}
How do you convert a number value to hexadecimal?
Hint, read your text book. It is not that hard to do.
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.