Posts

Program to find sum of array elements using loops

Image
  In this program we are using for loop to find the sum of array elements. #include <stdio.h> int main () { int arr [ 100 ], size , sum = 0 ; printf ( "Enter size of the array: " ); scanf ( "%d" ,& size ); printf ( "Enter the elements of the array: " ); for ( int i = 0 ; i < size ; i ++) { scanf ( "%d" ,& arr [ i ]); } //calculating sum of entered array elements for ( int i = 0 ; i < size ; i ++) { sum += arr [ i ]; } printf ( "Sum of array elements is: %d" , sum ); return 0 ; } Output:

check palindrome using recursion

Image
  #include <stdio.h> int check_palindrome ( int num ){ static int reverse_num = 0 , rem ; if ( num != 0 ){ rem = num % 10 ; reverse_num = reverse_num * 10 + rem ; check_palindrome ( num / 10 ); } return reverse_num ; } int main (){ int num , reverse_num ; printf ( "Enter a number: " ); scanf ( "%d" ,& num ); reverse_num = check_palindrome ( num ); if ( num == reverse_num ) printf ( "%d is a palindrome number" , num ); else printf ( "%d is not a palindrome number" , num ); return 0 ; } Output:

check palindrome using while loop

Image
  #include <stdio.h> int main () { int num , reverse_num = 0 , remainder , temp ; printf ( "Enter an integer: " ); scanf ( "%d" , & num ); /* Here we are generating a new number (reverse_num) * by reversing the digits of original input number */ temp = num ; while ( temp != 0 ) { remainder = temp % 10 ; reverse_num = reverse_num * 10 + remainder ; temp /= 10 ; } /* If the original input number (num) is equal to * to its reverse (reverse_num) then its palindrome * else it is not. */ if ( reverse_num == num ) printf ( "%d is a palindrome number" , num ); else printf ( "%d is not a palindrome number" , num ); return 0 ; } Output:

C Program to find factorial of number using Recursion

  #include <stdio.h> int find_factorial ( int ); int main () { int num , fact ; //Ask user for the input and store it in num printf ( "\nEnter any integer number:" ); scanf ( "%d" ,& num ); //Calling our user defined function fact = find_factorial ( num ); //Displaying factorial of input number printf ( "\nfactorial of %d is: %d" , num , fact ); return 0 ; } int find_factorial ( int n ) { //Factorial of 0 is 1 if ( n == 0 ) return ( 1 ); //Function calling itself: recursion return ( n * find_factorial ( n - 1 )); } Output: Enter any integer number : 4 factorial of 4 is : 24

C Program to check if number is even or odd

Image
  #include <stdio.h> int main () { // This variable is to store the input number int num ; printf ( "Enter an integer: " ); scanf ( "%d" ,& num ); // Modulus (%) returns remainder if ( num % 2 == 0 ) printf ( "%d is an even number" , num ); else printf ( "%d is an odd number" , num ); return 0 ; } Output:

Program to add two integers using function

  #include <stdio.h> /* User defined function sum that has two int * parameters. The function adds these numbers and * return the result of addition. */ int sum ( int a , int b ){ return a + b ; } int main () { int num1 , num2 , num3 ; printf ( "Enter first number: " ); scanf ( "%d" , & num1 ); printf ( "Enter second number: " ); scanf ( "%d" , & num2 ); //Calling the function num3 = sum ( num1 , num2 ); printf ( "Sum of the entered numbers: %d" , num3 ); return 0 ; } Output: Enter first number : 22 Enter second number : 7 Sum of the entered numbers : 29

Program to add two integer numbers

  #include <stdio.h> int main () { int num1 , num2 , sum ; printf ( "Enter two integers: " ); //Storing user input into variable num1 & num2 scanf ( "%d %d" , & num1 , & num2 ); // Adding two input numbers sum = num1 + num2 ; printf ( "Sum of %d and %d is: %d" , num1 , num2 , sum ); return 0 ; } Output:  User enters the numbers 25 and 35 and program prints sum of these two numbers 60 as an output.