where to put statement for powerful number? - c

I have make this program that calculates number factorization such as 60 = 2^2 * 5 * 3.
How can i modify my code in order to print POWERFUL NUMBERS such as 9000 = 2^3 * 3^2 * 5^3 without using math.h library and without using arrays?
Thank you very much!!
#include<stdio.h>
#define MAX 1000
int main(){
int num;
int counter;
int number;
char factorizationOutput;
int isAchiles = 0;
int factor=2;
for(counter=2;counter<=MAX;counter++){
isAchiles = 1;
number=counter;
int factor=2;
while(factor<number){
int power=0;
if(number%factor==0){
while(number%factor==0){
number=number/factor;
power++;
}
if(power == 1){
isAchiles = 0;
}
printf("%d^%d",factor,power);
if(number!=1)
printf(" X ");
}
factor++;
}
if(number!=1)
printf("%d^1.\n",factor);
if(isAchiles == 1){
printf("factorazation of number %d is: ",counter);
}
}
}

#include<stdio.h>
int main(void)
{
int n;
scanf("%d", &n);
printf("%d = ", n);
for(int i = 1; i <= n; i++)
{
int count = 0;
for(int j = 1; j <= i; j++)
{
if(i % j == 0)
{
count++;
}
}
int l = 0;
if(count == 2)
{
while(n % i == 0)
{
l++;
n = n/i;
}
printf("%d^%d*", i, l);
}
}
}

Related

Problem with program to list primes below integer using arrays and nested loops

#include <stdio.h>
#include <math.h>
#define MAX_SIZE 10000
int main(void)
{
int a[MAX_SIZE];
int N;
int L; /* the current size of the list */
/* read in the upper limit. Keep reading until
a valid number between 3 and the maximum that
can be handled by the array is entered */
double b[10000];
int j, i;
L = 0;
printf("Enter the upper limit:\n");
do {
scanf("%d", &N);
} while (N<3 || N>MAX_SIZE+2);
int prime;
for (j = 1; j < N; j++)
{
prime = 1;
for (i = 2; i < j; i++)
{
if (j % i == 0)
{
prime = 0;
break;
}
}
if (prime)
{
a[i] = j;
L++;
}
}
/* write out the result - DO NOT CHANGE THIS */
for(i=0;i<L;i++)
printf("%d ",a[i]);
printf("\n");
return 0;
}
Program needs to take an integer, calculate primes below that integer, print that list of primes.
I think my problem is related to the loops.
The program is calculating the primes but listing 0 if the number previously there isnt prime eg a[4] is now printing as 0
Any help is appreciated.
thanks.
Is this what you were trying to implement?
#include <stdio.h>
#define MAX_SIZE 100
int main(void)
{
int primes[MAX_SIZE];
int primes_found = 0;
int limit = 0;
while (limit < 3)
{
printf("Enter the upper limit:\n");
scanf("%d", &limit);
}
for (int candidate = 2; candidate <= limit && primes_found < MAX_SIZE; candidate++)
{
int divisor = 2;
int is_prime = 1;
while(is_prime && divisor < candidate)
is_prime = candidate % divisor++ != 0;
if (is_prime)
primes[primes_found++] = candidate;
}
for (int i = 0 ; i < primes_found ; i++)
printf("%d ", primes[i]);
printf("\n");
return 0;
}

count array value bug

Hi I need help please i need to fix this code so it count the value only once
for exmaple
input:
25
38 25 36 4 1 1 10 37 45 21 37 42 21 1 50 9 50 42 6 39 10 14 17 11 20
10
36 42 2 15 28 42 3 23 8 50
output:
4
the answer here should be 4 not 7.
#include <stdio.h>
int main()
{
int n, m, count = 0;
int array[1000];
int subarray[1000];
scanf("%d", &n);
for (int i = 0; i < n; i++)
{
scanf("%d", &array[i]);
}
scanf("%d", &m);
for (int i = 0; i < m; i++)
{
scanf("%d", &subarray[i]);
}
for (int i = 0; i < n; i++)
{
for (int j = 0; j < i; j++)
{
if (array[i] == subarray[j])
count++;
}
}
printf("%d\n", count);
}
Possible solution, using functions and qsort.
(untested code)
#include <stdio.h>
int binarySearch(int arr[], int l, int r, int x) {
if (r >= l) {
int mid = l + (r - l) / 2;
if (arr[mid] == x)
return mid;
if (arr[mid] > x)
return binarySearch(arr, l, mid - 1, x);
return binarySearch(arr, mid + 1, r, x);
}
return -1;
}
int cmpfunc (const void * a, const void * b) {
return (*(int*)a > *(int*)b) - (*(int*)a < *(int*)b);
}
int main() {
int n, m, count = 0;
int array[1000];
int subarray[1000];
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d", &array[i]);
}
qsort(array, n, sizeof(int), cmpfunc); // O(n lg n)
scanf("%d", &m);
for (int i = 0; i < m; i++) {
scanf("%d", &subarray[i]);
int result = binarySearch(arr, 0, n - 1, x); // O(lg n)
if (result != -1)
count++;
} // O(m lg n)
printf("%d\n", count);
}
You need to keep track of the matched values in third array as following and
check if the new value is found before or not
#include <stdio.h>
int main()
{
int n, m, count = 0;
int array[1000];
int subarray[1000];
int result[1000];
scanf("%d", &n);
for (int i = 0; i < n; i++)
{
scanf("%d", &array[i]);
}
scanf("%d", &m);
for (int i = 0; i < m; i++)
{
scanf("%d", &subarray[i]);
}
for (int i = 0; i < n; i++)
{
for (int j = 0; j < i; j++)
{
if (array[i] == subarray[j]){
int isFound=0;
for(int k=0;k<count;k++){
if(result[k]==array[i])
isFound=1;
}
if(isFound==0){
result[count]==array[i];
count++;
}
}
}
}
printf("%d\n", count);
}
I use a boolean variable to check if the elements of array 1 exist in the array 2 ,if not ,i will copied it to array 3
#include <stdio.h>
#include <stdbool.h>//header how found the booleen variable
int main()
{
int n,m;
do
{
printf("Give me the length of array 1 :");
scanf("%d",&n);
}while(n<1);
int T1[n];//declaration of Array 1
do
{
printf("Give me the length of array 2 :");
scanf("%d",&m);
}while(m<1);
int T2[m];//declaration of Array 2
printf("The fill of Array 1:\n\n");
for(int i=0;i<n;i++)
{
scanf("%d",&T1[i]);
}
printf("The fill of Array 2:\n\n");
for(int j=0;j<m;j++)
{
scanf("%d",&T2[j]);
}
int T3[n+m];//declaration of Array 3
bool found=false;//declaration of booleen variable
int k=0;
for(int i=0;i<n;i++)
{found=false;
for(int j=0;j<m;j++)
{
if(T1[i]==T2[j])
{
found=true;
}
}
if(found==true)
{
T3[k]=T1[i];
k++;
}
}
printf("\nThe number of elment duplicate is :%d\n",k);
}
so it count the value only once
With minimal impact to OP's code and without re-ordering input:
Check if the subarray[] value already occurred within itself.
When subarray[]/array[] values match, quit looking.
for (int j = 0; j < m; j++) {
bool unique_subarray_value = true;
for (int earlier = 0; earlier < j; earlier++) {
if (subarray[earlier] == subarray[j])
unique_subarray_value = false;
break;
}
}
if (unique_subarray_value) {
for (int i = 0; i < n; i++) {
if (array[i] == subarray[j]) {
count++;
break;
}
}
}
}
A fast approach would sort the arrays and then walk them
// Pseudo code
sort array[n] with qsort()
sort subarray[m] with qsort()
// walk the arrays looking for matches
i = 0;
j = 0;
while (i < n && j < m) {
if (array[i] == subarray[j]) {
count++;
while (i + 1 < n && array[i] == array[i+1]) i++;
while (j + 1 < m && subarray[j] == subarray[j+1]) j++;
}
if (array[i] < subarray[j]) i++;
else j++;
}

Returning array in C, Sudoku Solver

So I'm creating a sudoku solver in C. Here's my full code as of now, I've mostly been using python and just got into C, I basically converted a lot of python functions to C to get this but I think it'll work:
#include <stdio.h>
#include <stdlib.h>
int is_empty();
int possible_v();
int solver();
int main(){
int s_array[9][9];
FILE * fpointer;
int i;
int j;
fpointer = fopen("sudoku001.txt", "r");
for (i=0; i<9; i++){
for(j = 0; j<9; j++){
fscanf(fpointer, "%d", &s_array[i][j]);
}
}
for (i=0; i<9; i++) {
if (i % 3 == 0) {
printf("------------------------------\n");
}
for (j = 0; j < 9; j++) {
printf(" %d ", s_array[i][j]);
if ((j + 1) % 3 == 0) {
printf("|");
}
}
printf("\n");
}
solver(s_array);
for (i=0; i<9; i++) {
if (i % 3 == 0) {
printf("------------------------------\n");
}
for (j = 0; j < 9; j++) {
printf(" %d ", s_array[i][j]);
if ((j + 1) % 3 == 0) {
printf("|");
}
}
printf("\n");
}
return 0;
}
int is_empty(int board[9][9]){
int i;
int j;
int is_empty= 0;
for (i=0; i<9; i++){
for(j = 0; j<9; j++){
if (board[i][j] == 0) {
is_empty = 1;
break;
}
}
if (is_empty == 1){
break;
}
}
return is_empty;
}
int possible_v(int board[9][9], int i, int j) {
int p_array[9] = {0, 0, 0, 0, 0, 0, 0, 0, 0};
int x;
int y;
int temp;
for (x = 0; x < 9; x++) {
if (board[x][j] != 0) {
temp = board[x][j];
p_array[temp - 1] = temp;
}
}
for (y = 0; y < 9; y++) {
if (board[i][y] != 0) {
temp = board[i][y];
p_array[temp - 1] = temp;
}
}
int m;
int n;
int temp1;
int temp2;
if (i>= 0 && i <= 2) {
m = 0;
}
else if (i>= 3 && i<=5) {
m = 3;
}
else{
m = 6;
}
if (j>= 0 && j <= 2) {
n = 0;
}
else if (j>= 3 && j<=5) {
n = 3;
}
else{
n = 6;
}
temp1 = m;
temp2 = n;
for (temp1; temp1<temp1+3; temp1++){
for (temp2; temp2<temp2+3; temp2++){
if (board[temp1][temp2] != 0){
p_array[board[temp1][temp2]] = 1;
}
}
}
temp1 = 1;
for (temp1; temp1<10){
if (p_array[temp1] == 0){
p_array[temp1] = temp1;
}
else{
p_array[temp1] = 0;
}
}
return p_array;
}
int solver(int board[9][9]){
int i;
int j;
int x;
int y;
int empty_check;
int p_values;
int temp;
if (is_empty(board) == 0){
printf("Board Completed");
empty_check = 0;
return empty_check;
}
else{
for (x = 0; x < 9; x++){
for (y = 0; y< 9; y++){
if (board[x][y] == 0){
i = x;
j = y;
break;
}
}
}
p_values = possible_v(board, i, j);
for (temp = 1; temp <10; temp++){
if (p_values[temp] != 0){
board[i][j] = p_values[temp];
solver(board);
}
}
board[i][j] = 0;
}
}
My main issue when compiling is getting the last two functions work with each other.
Function 'solver' calls and binds function 'possible_v'. Possible_V returns an array which I need to solve the puzzle. How can I make this work? .
You have the array locally declared, hence it cannot be passed back since it is destroyed once the function is exited. The workaround to this is to dynamically declare the array using malloc int *parray = (int*)malloc(9*sizeof(int)); and using the return type int* instead of int. But do not forget to free the allocated memory, else you will just keep allocating new memory from heap for every call you make.
As a side note, your implementation of Sudoku solver is a bit complex, and there is no need to return an array. You need to pass only the board. Here is an implementation of Sudoku Solver. This works both for 9x9 and 6X6 boards.
Edit : As advised by David Rankin, I have converted the C++ code to C.
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int n;
int issafe(int **board,int i,int j,int num){
for(int k=0;k<n;k++)
if(board[i][k] == num || board[k][j] == num)
return 0;
int cellx,celly;
if(n==6){
cellx = (i/2)*2;
celly = (j/3)*3;
for(int l=cellx;l<cellx+2;l++)
for(int m=celly;m<celly+3;m++)
if(board[l][m] == num){
return 0;
}
return 1;
}
int root = sqrt(n);
cellx = (i/(root))*root;
celly = (j/(root))*root;
for(int l=cellx;l<cellx+root;l++)
for(int m=celly;m<celly+root;m++)
if(board[l][m] == num)
return 0;
return 1;
}
int solve(int **board,int i,int j){
if(i == n)
return 1;
if(j == n){
return solve(board,i+1,0);
}
if(board[i][j] != 0)
return solve(board,i,j+1);
for(int k=1;k<n+1;k++)
if(issafe(board,i,j,k)){
board[i][j] = k;
if(solve(board,i,j+1))
return 1;
//backtrack
board[i][j] = 0;
}
return 0;
}
int main(){
do{
printf("Enter size of board(9 or 6): ");
scanf("%d",&n);
}while(n != 9 && n != 6);
int **board;
board = malloc(sizeof *board * n);
for(int i=0;i<n;i++)
board[i] = malloc(sizeof *board * n);
// input
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
scanf("%d",&board[i][j]);
if(solve(board,0,0))
for(int i=0;i<n;i++){
for(int j=0;j<n;j++)
printf("%d ",board[i][j]);
printf("\n");
}
return 0;
}

Determine the number of strikes in a random number, depending on user's input

The function "strike" has to return the number of times that the user's input are equivalent.
Assume, the random number is 1234.
if the user's input has one of the random's numbers, then strike1++.
e.g., if my input is 5152 then strike1 will be 2.
if my input is, 1112, then strike will be again 2.
I'm getting wrong output in strike1. Any ideas how to solve it?
(I don't want to solve it with arrays)
Solution:
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
int hit(int num);
int strike(int num);
int rndNum(int num);
void main()
{
int num = 0;
int chosenNum;
int saveHits, saveStrikes;
srand(time(NULL));
printf("The Random number: %d", chosenNum = rndNum(num));
printf("\nPlease enter a 4 digit number: ");
scanf("%d", &num);
saveHits = hit(num, chosenNum);
saveStrikes = strike(num, chosenNum);
printf("\nThe number of hits: %d", saveHits);
printf("\nThe number of strikes: %d", saveStrikes);
getch();
}
int rndNum(int num)
{
int rndNum = rand() % 9000 + 1000;
return rndNum;
}
int hit(int num1, int chosenNum1)
{
int i, hit1 = 0;
for (i = 0; i < 4; i++)
{
if (num1 % 10 == chosenNum1 % 10)
hit1++;
num1 /= 10;
chosenNum1 /= 10;
}
return hit1;
}
int strike(int num1, int chosenNum1)
{
int i, strike1 = 0, n = 1;
int temp = num1, temp2 = chosenNum1;
for (i = 0;i < 4;i++)
{
while (n > 0)
{
if (temp > 0)
{
if ((temp % 10 == temp2 % 10))
{
strike1++; n--;
}
else
temp /= 10;
}
else
n--;
}
temp2 /= 10;
n = 1;
temp = num1;
}
return strike1;
}
int strike(int num1, int chosenNum1)
{
int i, strike1 = 0, n = 1;
int temp = num1, temp2 = chosenNum1;
for (i = 0;i < 4;i++)
{
while (n > 0)
{
if (temp > 0)
{
if ((temp % 10 == temp2 % 10))
{
strike1++; n--;
}
else
temp /= 10;
}
else
n--;
}
temp2 /= 10;
n = 1;
temp = num1;
}
return strike1;
}

Accept a number and print its alternate digits,

int Rearrange(int a)
{
long int b,j,i=0,num=0,count=0,arr[100];
while(a>0)
{
b=a%10;a=a/10;
arr[i]=b;
i++;
count ++;
}
j=count;
for(i=0;i<=count/2;i++)
{
t=arr[i];
arr[i]=arr[count-i-1];
arr[count-i-1]=t;
count--;
}
for(i=0;i<j;i+=2)
{
num=num*10 + arr[i]%10;
}
return num;
}
I want to write a function in c rearrange which prints the alternate digits of a number it is given.
for example:
input:- 12345
output:- 135
Thank you
Why complicating a simple problem?
If you don't mind an alternative approach, please check the below code.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
int input = 0;
int len = 0;
int i = 0;
char sinput[64] = {0, };
printf("Enter the number :");
scanf("%d", &input);
sprintf(sinput, "%d", input);
len = strlen(sinput);
printf("Output : ");
for (i = 0; i < len; i+=2)
{
printf("%c\t", sinput[i]);
}
printf("\n");
return 0;
}
Sample i/o:
[sourav#braodsword temp]$ ./a.out
Enter the number :123456
Output : 1 3 5
[sourav#braodsword temp]$
Your for loops are faulty. Change those to:
for(i=0;i<=count/2;i++)
{
int t=arr[i];
arr[i]=arr[j]; /* Use j */
arr[j]=t; /* Use j */
/* count--; Dont decrement */
j--;
}
for(i=0;i<count;i+=2) /* Should be count */
{
num=num*10 + arr[i]%10;
}
Demo
There can be many alternate ways to solve, but I just want to show you how the approach in thought process can be implemented correctly.
In your code problem is with the first for loop.
Please check the below code.
int Rearrange(int a)
{
long int b = 0, j = 0, i = 0, num = 0, count = 0, arr[100];
while (a > 0)
{
b = a % 10; a = a/10;
arr[i] = b;
i++;
count++;
}
j = count;
for (i = 0; i < count/2; i++) // Condition is problematic
{
long int t = arr[i];
arr[i] = arr[count-i-1];
arr[count - i - 1] = t;
// count--; // this is problamatic.
}
for (i = 0; i < j; i += 2)
{
num = num * 10 + arr[i] % 10;
}
return num;
}
#include<stdio.h>
int main()
{
int n,arr[40];
scanf("%d",&n);
printf("%d",n);
int s=0,i=0;
while(n!=0)
{
arr[i]=n%10;
printf("%d",arr[i]);
n=n/10;
i++;
}
for(int j=i-1;j>=0;j-=2)
{
s =s*10+arr[j];
}
printf("\n%d",s);
return 0;
}
int alternatedigits(int n)
{
int a[10],i=0,count=0,sum=0;
while(n!=0)
{
a[i]=n%10;
i++;
count++;
n=n/10;
}
for(int i=count;i>=0;i++)
{
if(i%2==0)
{
sum=sum*10+a[i];
}
}
return sum;
}

Resources