#include <stdio.h> // Algoritmo de Euclides para verificar se dois números são coprimos int coPrimos(int num1, int num2) { int resto; while (num2 != 0) { resto = num1 % num2; num1 = num2; num2 = resto; } return (num1 == 1); // Retorna 1 se forem coprimos, 0 caso contrário } // Verifica as condições do Teorema Chinês int condicoes(int *vetor, int nEq) { int num1, num2; // Condição 1: verificar se A e MOD são coprimos for (int i = 0; i < nEq; i++) { num1 = vetor[i * 3 + 1]; num2 = vetor[i * 3 + 2]; if (!coPrimos(num1, num2)) { return 0; } } // Condição 2: verificar se os MODs são coprimos par a par for (int i = 0; i < nEq; i++) { for (int j = i + 1; j < nEq; j++) { num1 = vetor[i * 3 + 2]; num2 = vetor[j * 3 + 2]; if (!coPrimos(num1, num2)) { return 0; } } } return 1; // Retorna 1 se todas as condições forem atendidas } // Resolve o sistema pelo Teorema Chinês dos Restos int teoremaChines(int *vetor, int nEq) { int x = 0; int produto = 1; // Calcula o produto de todos os MODs for (int i = 0; i < nEq; i++) { produto *= vetor[i * 3 + 2]; } // Aplica a fórmula da solução geral for (int i = 0; i < nEq; i++) { int a = vetor[i * 3]; // Coeficiente do sistema int b = vetor[i * 3 + 1]; // Valor de A int mod = vetor[i * 3 + 2]; // Valor do módulo int n = produto / mod; // Calcula o inverso modular de n módulo mod int inverso = 0; for (int j = 1; j < mod; j++) { if ((n * j) % mod == 1) { inverso = j; break; } } // Soma parcial para o valor de X x += a * b * n * inverso; } // Ajusta X para o menor valor positivo x %= produto; if (x < 0) { x += produto; } return x; } int main() { int nEq; printf("Digite a quantidade de equações que o seu sistema possuirá. Deve ser maior que 1.\n"); scanf("%d", &nEq); if (nEq < 2) { printf("O número de equações deve ser maior que 1.\n"); return 1; } int vetor[nEq * 3]; printf("Realize os inputs na seguinte ordem: coeficiente de X, valor de A e valor do módulo.\n"); // Preenchimento do vetor com as equações for (int i = 0; i < nEq * 3; i++) { printf("Input número %d: ", i + 1); scanf("%d", &vetor[i]); } // Checa as condições antes de aplicar o Teorema Chinês if (!condicoes(vetor, nEq)) { printf("Insira equações que obedeçam às condições do Teorema Chinês.\n"); return 1; } int resposta = teoremaChines(vetor, nEq); printf("O valor de X é: %d\n", resposta); return 0; }
Write, Run & Share C Language code online using OneCompiler's C online compiler for free. It's one of the robust, feature-rich online compilers for C language, running the latest C version which is C18. Getting started with the OneCompiler's C editor is really simple and pretty fast. The editor shows sample boilerplate code when you choose language as 'C' and start coding!
OneCompiler's C online editor supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample C program which takes name as input and print your name with hello.
#include <stdio.h>
int main()
{
char name[50];
printf("Enter name:");
scanf("%s", name);
printf("Hello %s \n" , name );
return 0;
}
C language is one of the most popular general-purpose programming language developed by Dennis Ritchie at Bell laboratories for UNIX operating system. The initial release of C Language was in the year 1972. Most of the desktop operating systems are written in C Language.
When ever you want to perform a set of operations based on a condition if-else
is used.
if(conditional-expression) {
// code
} else {
// code
}
You can also use if-else for nested Ifs and if-else-if ladder when multiple conditions are to be performed on a single variable.
Switch is an alternative to if-else-if ladder.
switch(conditional-expression) {
case value1:
// code
break; // optional
case value2:
// code
break; // optional
...
default:
// code to be executed when all the above cases are not matched;
}
For loop is used to iterate a set of statements based on a condition.
for(Initialization; Condition; Increment/decrement){
// code
}
While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.
while(condition) {
// code
}
Do-while is also used to iterate a set of statements based on a condition. It is mostly used when you need to execute the statements atleast once.
do {
// code
} while (condition);
Array is a collection of similar data which is stored in continuous memory addresses. Array values can be fetched using index. Index starts from 0 to size-1.
data-type array-name[size];
data-type array-name[size][size];
Function is a sub-routine which contains set of statements. Usually functions are written when multiple calls are required to same set of statements which increases re-usuability and modularity.
Two types of functions are present in C
Library functions are the in-built functions which are declared in header files like printf(),scanf(),puts(),gets() etc.,
User defined functions are the ones which are written by the programmer based on the requirement.
return_type function_name(parameters);
function_name (parameters)
return_type function_name(parameters) {
//code
}