Programming in C. 1. A positive integer is entered through the keyboard, write a
ID: 672175 • Letter: P
Question
Programming in C.
1. A positive integer is entered through the keyboard, write a C program with a function pfactors that reads an integer in, then calls the function pfactors to obtain the prime factors of the number and display them. The output of your program should look something like the below text.
Enter the integers for which the prime factors need to be found: 75 The prime factors for 75 are: 3 5 5 Would you like to run the program again? (enter Y for yes, any other letter for no): Y Enter the integers for which the prime factors need to be found: 48 The prime factors for 48 are: 2 2 2 2 3 Would you like to run the program again? (enter Y for yes, any other letter for no): q Thanks for using the program. Bye!
2. Write a C program that reads a decimal number in and then calls a function dec2bin to find the binary equivalent the decimal integer and display it. The binary equivalent of a number is obtained by continuously dividing a decimal number (and then the successive results from 2, retaining the remainders until the end of division) and then writing the remainders backwards, as shown in the examples. The output of your program should look something like the below text.
This program converts the decimal number supplied by you to its binary equivalent! Enter the decimal number for conversion: 156 The binary equivalent of 156 is 1 0 0 1 1 1 0 0 Would you like to run the program again? (enter Y for yes, any other letter for no): Y Enter the decimal number for conversion: 26 The binary equivalent of 26 is 1 1 0 1 0 Would you like to run the program again? (enter Y for yes, any other letter for no): q Thanks for using the program. Bye! 20x1
3. Write a C program that reads the value of angle x in radians (360 degrees = 2 radians) then calls a function sin to find the sine trigonometric function by evaluating the following series for sin(x) to n terms (starting with x in the formula there, 4 terms have been shown, the others would simply follow the progression). The value of n would also be supplied by the user. sin(x) = x (x3 /3!) + (x5 /5!) (x7 / 7!) + ……. The output of your program should look something like the below text.
This program calculates the trigonometric sine for a given angle in radians! Enter the angle in radians: 0.5236 Enter the number of series terms that you would like to add up: 7 Value of sin(0.5236) = 0.5 Would you like to run the program again? (enter Y for yes, any other letter for no): Y Enter the angle in radians: 1.047 Enter the number of series terms that you would like to add up: 5 Value of sin(1.047) = .866 Would you like to run the program again? (enter Y for yes, any other letter for no): q Thanks for using the program. Bye! 20x1
4. Write a C program to read in integers from a 20 element unidimensional array inparr and assign to two different 2-D arrays, one of them outarr1, a [4]x[5] array and another outarr2, a [5]x[4] array. Print outarr1 as a ‘4 row-5 column’ array and outarr2 as a ‘5 row-4 column’ array to check and find out if the assignment was done correctly.
5. Write a C program to generate multiplication tables (2 to 5 – up to x10) – column-wise print as shown below. This program prints multiplication tables from 2 to 5! *******************************************
2 3 4 5
4 6 8 10
6 9 12 15
8 12 16 20
10 15 28 25
12 18 24 30
14 21 28 35
16 24 32 40
18 27 36 45
20 30 40 50
Explanation / Answer
Hi,
Below is the solution to your problem:
a.Program to print prime factors of a given number:
// Program to print all prime factors
# include <stdio.h>
# include <math.h>
// A function to print all prime factors of a given number n
void pFactors(int n)
{
// Print the number of 2s that divide n
while (n%2 == 0)
{
printf("%d ", 2);
n = n/2;
}
// n must be odd at this point. So we can skip one element (Note i = i +2)
for (int i = 3; i <= sqrt(n); i = i+2)
{
// While i divides n, print i and divide n
while (n%i == 0)
{
printf("%d ", i);
n = n/i;
}
}
// This condition is to handle the case whien n is a prime number
// greater than 2
if (n > 2)
printf ("%d ", n);
}
/* Driver program to test above function */
int main()
{
printf("Enter the number for which you want to find the prime factors ");
scanf("%d",&n);
pFactors(n);
printf("Do you wish to continue !!!!!!!!!! ");
return 0;
}
2.C program to convert a decimal number to binary.
#include<stdio.h>
int main(){
long int decimalNumber,remainder,quotient;
int binaryNumber[100],i=1,j;
printf("Enter Y or N for either running the program or not ");
while(getchar=='y'){
printf("Enter any decimal number: ");
scanf("%ld",&decimalNumber);
quotient = decimalNumber;
while(quotient!=0){
binaryNumber[i++]= quotient % 2;
quotient = quotient / 2;
}
printf("Equivalent binary value of decimal number %d: ",decimalNumber);
for(j = i -1 ;j> 0;j--)
printf("%d",binaryNumber[j]);
}
else{
printf("Exiting from the program ");
}
return 0;
}
3.C program that reads the value of angle x in radians (360 degrees = 2 radians) then calls a function sin to find the sine trigonometric function by evaluating the following series for sin(x) to n terms.
#include <stdio.h>
4.C program to take array of 20 integers and convert it to matrix of 4*5 and 5*4
5.C program to print multiplication table:
#include<stdio.h>
int main(){
int i,j,k;
for(i=2;i<=5;i++){
for(j=1;j<=10;j++)
printf("%d*%d=%d ",i,j,i*j);
printf(" ");
}
return 0;
}
Hope that solves your problem.HAPPY ANSWERING!!!!!!!
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.