Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- int main()
- {
- int n;
- printf("Enter a number: ");
- scanf("%d", &n);
- for(int i = 1; i <= n; i++)
- {
- for(int j = 1; j<=i; j++)
- {
- printf("%d", j);
- }
- printf("\n");
- }
- return 0;
- }
- /*
- Enter a number: 4
- 1
- 12
- 123
- 1234
- */
- ---------------------------------------------------
- /**
- Find if a number is prime (without function)
- **/
- #include <stdio.h>
- int main()
- {
- int n;
- printf("Enter a number: ");
- scanf("%d", &n);
- int flag = 1;
- for(int i = 2; i < n; i++)
- {
- if(n % i == 0)
- {
- printf("%d is not prime\n", n);
- flag = 0;
- break;
- }
- }
- if(flag == 1)
- {
- printf("%d is a prime\n", n);
- }
- return 0;
- }
- /*
- Enter a number: 20
- 20 is not prime
- */
- ---------------------------------------------------
- /**
- Find if a number is prime (with function)
- **/
- int isPrime(int n)
- {
- for(int i = 2; i < n; i++)
- {
- if(n % i == 0)
- {
- return 0;
- }
- }
- return 1;
- }
- #include <stdio.h>
- int main()
- {
- int n;
- printf("Enter a number: ");
- scanf("%d", &n);
- if(isPrime(n))
- {
- printf("%d is a prime\n", n);
- }
- else
- {
- printf("%d is not a prime\n", n);
- }
- return 0;
- }
- /*
- Enter a number: 5
- 5 is a prime
- */
- ---------------------------------------------------
- /**
- print primes from 2 to N (with function)
- **/
- int isPrime(int n)
- {
- for(int i = 2; i < n; i++)
- {
- if(n % i == 0)
- {
- return 0;
- }
- }
- return 1;
- }
- #include <stdio.h>
- int main()
- {
- int n;
- printf("Enter n: ");
- scanf("%d", &n);
- for(int i = 2; i<=n; i++)
- {
- if(isPrime(i))
- {
- printf("%d ", i);
- }
- }
- return 0;
- }
- /*
- Enter a number: 5
- 5 is a prime
- */
- ---------------------------------------------------
- /**
- print primes from 2 to N (without function)
- **/
- ************* H W *************
- ---------------------------------------------------
- /**
- find GCD
- **/
- #include <stdio.h>
- int main()
- {
- int a, b;
- printf("Enter two numbers: ");
- scanf("%d %d", &a, &b);
- int min, gcd;
- if(a <= b) min = a;
- else min = b;
- for(int i = 1; i<=min; i++)
- {
- if(a % i == 0 && b % i == 0)
- {
- gcd = i;
- }
- }
- printf("GCD: %d\n", gcd);
- int lcm = (a * b) / gcd;
- printf("LCM: %d\n", lcm);
- return 0;
- }
- /*
- Multiples:
- 3 = 3, 6, 9, 12, ...
- Divisors:
- 30 = 1 . 30
- = 2 . 15
- = 5 . 6
- GCD Greatest Common Divisors
- 30 20
- 30 = 1, 2, 5, 6, 10, 15, 30
- 20 = 1, 2, 5, 10, 20
- Choto number er divisor boro number er o divisor kina => true: common divisor
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement