diff --git a/Palindrome number.c b/Palindrome number.c new file mode 100644 index 0000000..59e333c --- /dev/null +++ b/Palindrome number.c @@ -0,0 +1,31 @@ +//Palindrome Number in C + + +// C code: + +#include + +// Recursive way to find reverse of a number +int getReverse(int num, int rev){ + if(num == 0) + return rev; + + int rem = num % 10; + rev = rev * 10 + rem; + + return getReverse(num / 10, rev); +} + +int main () +{ + int num, reverse = 0; + num=12321; + printf("The number is: %d\n",num); + + // palindrome if num and reverse are equal + if (num == getReverse(num, reverse)) + printf("%d is Palindrome\n", num); + else + printf("%d is Not Palindrome\n", num); + +} diff --git a/Positive_Negetive_number.c b/Positive_Negetive_number.c new file mode 100644 index 0000000..df05841 --- /dev/null +++ b/Positive_Negetive_number.c @@ -0,0 +1,22 @@ +//Check if a Number is Positive or Negative in C +//Given an integer input, the objective is check whether the given integer is Positive or Negative + + +//C Code: + +#include +int main() +{ + int num = 23; + + //Conditions to check if the number is negative/positive or zero + if (num > 0) + printf("The number is positive"); + else if (num < 0) + printf("The number is negative"); + else + printf("Zero"); + + return 0; +} + diff --git a/Prime number within a given range.c b/Prime number within a given range.c new file mode 100644 index 0000000..15b6f4d --- /dev/null +++ b/Prime number within a given range.c @@ -0,0 +1,48 @@ +/*Prime Numbers in a Given Range in C +A number that is divisible only by itself and 1 (e.g. 2, 3, 5, 7, 11). + +The C program reduces the number of iterations within the loop. It is made to identify or calculate the prime numbers within a given range of numbers inserted by the user. + +Ex:- if the user enters a range as 40 – 50 + +In that range 41, 43, 47, these three number are prime number.*/ + +// C code: +#include + +int checkPrime(int num) +{ + // 0, 1 and negative numbers are not prime + if(num < 2){ + return 0; + } + else{ + // no need to run loop till num-1 as for any number x the numbers in + // the range(num/2 + 1, num) won't be divisible anyways. + // Example 36 wont be divisible by anything b/w 19-35 + int x = num/2; + for(int i = 2; i < x; i++) + { + if(num % i == 0) + { + return 0; + } + } + } + // the number would be prime if we reach here + return 1; +} +int main() +{ + int a=10, b=20; + + + for(int i=a; i <= b; i++){ + if(checkPrime(i)) + printf("%d ",i); + } + + return 0; +} +//Time Complexity: O(N^2) +//Space Complexity O(1) diff --git a/Prime number.c b/Prime number.c new file mode 100644 index 0000000..e52fabb --- /dev/null +++ b/Prime number.c @@ -0,0 +1,33 @@ +/*Check Whether a Number is Prime Number or Not in C +Given an integer input, the objective is to check whether or not the input integer is a prime or not. + In order to do so we check if the number is divisible by 2, if so it’s not a prime. + We also divide the numbers with the input until square root of the input, if any of them divides the number perfectly, it’s not a prime. + Here are some of the methods to Check Whether a Number is Prime or Not in C*/ + +// C code: + +#include < stdio.h> + +int main() +{ + int i, count = 0; + int num = 19; + + // checking number of divisors b/w 1 & num + for(i = 1; i <= num; i++) { + if(num % i == 0) + count += 1; + } + // 0 & 1 are not prime number + if(num == 0 || num == 1) + printf("%d is not prime", num); + //if number of divisors are > 2 then not prime else prime + else if(count > 2) + printf("%d is not prime", num); + else + printf("%d is prime", num); + + return 0; +} +// Time complexity O(N) +// Space complexity O(1) diff --git a/Sum of digits of a number.c b/Sum of digits of a number.c new file mode 100644 index 0000000..699d53b --- /dev/null +++ b/Sum of digits of a number.c @@ -0,0 +1,32 @@ +/*In this C program, we will code Sum of Digits of a Number in C we will allow the user to enter any number and then we will divide the number into individual digits and add those individuals (sum=sum+digit) digits using While Loop. + +Ex:- number is 231456 + + 2 + 3 + 1 + 4 + 5 + 6 = 21 + +Sum of digit of a given number is 21*/ + +// C code: +#include + +int main () +{ + int num, sum = 0; + + num = 1234; + printf("The number is = %d\n",num); + + //loop to find sum of digits + while(num!=0){ + sum += num % 10; + num = num / 10; + } + + //output + printf("Sum: %d\n",sum); + + return 0; + +} +// Time complexity : O(N) +// Space complexity : O(1) diff --git a/Sum of first n natural numbers.c b/Sum of first n natural numbers.c new file mode 100644 index 0000000..2b7bb00 --- /dev/null +++ b/Sum of first n natural numbers.c @@ -0,0 +1,31 @@ +//Sum of First N Natural Numbers in C +//In this article, we will see C Program to Find the Sum of First N Natural Numbers. A Natural number is the same as a Counting number. They are used to Count the number of real physical objects. Natural numbers start from 1 and go on infinite. + +//The positive numbers 1,2,3 ... are known as natural numbers. + + +//Formula for Sum of First n natural numbers is : n(n+1)/2. +//If you want to add first 5 Natural number +//then we find the Sum of 1+2+3+4+5 =15. + +// C code: +#include + +int main() +{ + int n; + scanf("%d",&n); + + int sum = 0; + + for(int i=1;i<=n;i++) + // is same as writing sum = sum + i + sum += i; + + printf("Sum is %d",sum); + + return 0; +} + +// Time complexity : O(n) +// Space complexity : O(1) diff --git a/Suvani.Leap year or not.c b/Suvani.Leap year or not.c new file mode 100644 index 0000000..db98753 --- /dev/null +++ b/Suvani.Leap year or not.c @@ -0,0 +1,25 @@ +//Check if the Year is a Leap Year or Not in C +//Given an integer input for the year, the objective is to check if the given year input is a leap year or not. To do so we check whether the year is divisible by 400 or not, if true then it’s a leap year, it’s not a leap year otherwise. Therefore we write a program to Check if the Year in a Leap Year or Not in C. + +//Example +//Input : 2020 +//Output : It's a Leap Year + +// C code: +#include +int main () +{ + int year; + year=2000; + + if(year % 400 == 0) + printf("%d is a Leap Year",year); + + else if(year % 4 == 0 && year % 100 != 0) + printf("%d is a Leap Year",year); + + else + printf("%d is not a Leap Year",year); + + return 0; +} diff --git a/Suvani.Reverse of a number.c b/Suvani.Reverse of a number.c new file mode 100644 index 0000000..80687f0 --- /dev/null +++ b/Suvani.Reverse of a number.c @@ -0,0 +1,27 @@ +//Program to Reverse a Number in C + +// C code: +#include + +//main program +int main () +{ + //variables initialization + int num, reverse = 0, rem; + num=1234; + printf("The number is: %d\n",num); + + + //loop to find reverse number + while(num != 0) + { + rem = num % 10; + reverse = reverse * 10 + rem; + num /= 10; + }; + + //output + printf("Reverse: %d\n",reverse); + + return 0; +} diff --git a/Suvani_Even_odd_number.c b/Suvani_Even_odd_number.c new file mode 100644 index 0000000..cb0d0f9 --- /dev/null +++ b/Suvani_Even_odd_number.c @@ -0,0 +1,42 @@ +//The number is Even or Odd Program in C +//We can determine whether a number is Even or Odd program in C. This can be tested using different methods. The test can be done using simple methods such as +// Testing the number’s divisibility by 2. +// If the remainder is zero, the number is even. +// If the remainder is not zero, then the number is odd. + +//C code: + +//Method 1: + +#include + +int main () +{ + int number; + printf ("Insert a number \n"); + scanf ("%d", &number); + + //Checking if the number is divisible by 2 + if (number % 2 == 0) + printf ("Even"); + else + printf ("Odd"); + + return 0; +} + +// Method 2: + +#include +int main () +{ + int number; + printf ("Insert a number \n"); + scanf ("%d", &number); + + //Checking if the number is divisible by 2 + number % 2 == 0? printf ("Even"):printf ("Odd"); + + return 0; +} + diff --git a/Suvani_Greatest of 3 numbers.c b/Suvani_Greatest of 3 numbers.c new file mode 100644 index 0000000..5122856 --- /dev/null +++ b/Suvani_Greatest of 3 numbers.c @@ -0,0 +1,33 @@ +//Find the Greatest of Three Numbers in C +//Given three integers num1, num2 and num3 as inputs. The objective is to write a code to Find the Greatest of the Three Numbers in C Language. To do so we’ll check the numbers with each other and print the largest of them all. + +//Example +//Input : num1 = 2 num2 = 9 num3 = 4 +//Output : 9 + + + + +// C code: + +#include +int main () +{ + int num1, num2, num3; + + num1=12,num2=17,num3=19; + + //checking if num1 is greatest + if (num1 >= num2 && num1 >= num3) + printf("%d is the greatest", num1); + + //checking if num2 is greatest + else if(num2 >= num1 && num2 >= num3) + printf("%d is the greatest", num2); + + //checking if num2 is greatest + else if(num3 >= num1 && num3 >= num2) + printf("%d is the greatest", num3); + + return 0; +} diff --git a/Suvani_greatest_of_two_numbers.c b/Suvani_greatest_of_two_numbers.c new file mode 100644 index 0000000..c1c85ce --- /dev/null +++ b/Suvani_greatest_of_two_numbers.c @@ -0,0 +1,24 @@ +//Find the Greatest of the two numbers in C +//Given two integer inputs num1 and num2, the objective if to write a code to Find the Greatest of the Two Numbers in C. + +//Example +//Input : num1 = 5 and num2 = 6 +//Output : 6 + +//C code: +#include + +int main () +{ + int num1, num2; + num1=12,num2=13; + + if (num1 == num2) + printf("both are equal"); + else if (num1 > num2) + printf("%d is greater", num1); + else + printf("%d is greater", num2); + + return 0; +} diff --git a/Suvani_sum of number in range.c b/Suvani_sum of number in range.c new file mode 100644 index 0000000..32b41ba --- /dev/null +++ b/Suvani_sum of number in range.c @@ -0,0 +1,28 @@ +//Find the Sum of Numbers in a Given Range in C +//Given two integer inputs num1 and num2, the objective is to write a code to Find the Sum of Numbers in a Given Range in C. To do so we’ll keep iterating and adding the numbers from num1 until num2 to the Sum variable. + +//Example +//Input : 2 4 +//Output : 2 + 3 + 4 = 9 + + + +// C code +#include + +int main() +{ + int a = 5; + int b = 10; + + int sum = 0; + + for (int i = a; i <= b; i++) + sum = sum + i; + + printf("%d",sum); + + return 0; +} +// Time complexity : O(N) +// Space complexity : O(1)