Posts

Showing posts from July, 2020

C Program to check whether a number is palindrome or not

Image
Palindrome is a number which remains same after reversing its digits. For example:  Consider a number 121. Reverse of 121 is 121, so it is said to be palindrome. On the other hand, number 1234 whose reverse is 4321 is not a palindrome. CODE: #include <stdio.h> int main() {     int num,rem,rev=0;     printf("Enter a number: ");     scanf("%d",&num);     int temp=num;     while(num>0){         rem=num%10;         num=num/10;         rev=(rev*10)+rem;     }     if(rev==temp){         printf("%d is a palindrome",temp);     }     else{         printf("%d is not a palindrome",temp);     }     return 0; }

C Program to reverse elements of an array

Image
int main() {     int n;        printf("Enter size of array: ");     scanf("%d",&n);     int arr[n];     printf("Enter elements: ");     for(int i=0; i<n; i++)     {         scanf("%d",&arr[i]);     }     printf("Your array: ");      for(int i=0; i<n; i++)     {         printf("%d ",arr[i]);     }     printf("\nArray elements in reversed order: ");     for(int i=n-1; i>=0; i--)     {         printf("%d ",arr[i]);     }        return 0; }     OUTPUT:

C Program to find Sum of Digits of a given number

Image
#include <stdio.h> int main() {     int n,rem,sum=0;     printf("Enter a number: ");     scanf("%d", &n);     int temp=n;     while (n>0) {         rem=n%10;         n=n/10;         sum = sum+rem;     }     printf("\nSum of digits of %d is %d",temp,sum);     return 0; } OUTPUT:

C Program to convert decimal to binary and find consecutive 1s in a binary number:

Image
#include <stdio.h> int main() {     int num, rem=0,count=0,min=0;     printf("Enter a decimal number: ");     scanf("%d",&num);     while(num>0)     {         rem=num%2;         if(rem==1){             count++;             if(count>min){                 min=count;             }         }         else{             count=0;         }          num=num/2;        }     printf("Number of consecutive 1s are %d",min);     return 0; } OUTPUT: