1. Question:How to check if a number is a palindrome using for loop? 

    Answer
    #include<stdio.h>
    int main(){
        int num,r,sum=0,temp;
    
        printf("Enter a number: ");
        scanf("%d",&num);
    
        for(temp=num;num!=0;num=num/10){
             r=num%10;
             sum=sum*10+r;
        }
        if(temp==sum)
             printf("%d is a palindrome",temp);
        else
             printf("%d is not a palindrome",temp);
    
        return 0;
    }
    Sample output: Enter a number: 1221 1221 is a palindrome

    1. Report
  2. Question:C program to check if a number is palindrome using recursion 

    Answer
    #include<stdio.h>
    
    int checkPalindrome(int);
    int main(){
        int num,sum;
    
        printf("Enter a number: ");
        scanf("%d",&num);
    
        sum = checkPalindrome(num);
    
        if(num==sum)
             printf("%d is a palindrome",num);
        else
        printf("%d is not a palindrome",num);
    
        return 0;
    }
    
    int checkPalindrome(int num){
    
        static int sum=0,r;
    
        if(num!=0){
             r=num%10;
             sum=sum*10+r;
             checkPalindrome(num/10);
        }
    
        return sum;
    }
    Sample output: Enter a number: 25 25 is not a palindrome

    1. Report
  3. Question:C program to calculate roots of a quadratic equation 

    Answer
    #include<stdio.h>
    #include<math.h>
    
    int main(){
      float a,b,c;
      float d,root1,root2;  
    
     
      printf("Enter a, b and c of quadratic equation: ");
      scanf("%f%f%f",&a,&b,&c);
       
      d = b * b - 4 * a * c;
      
      if(d < 0){
        printf("Roots are complex number.\n");
    
        printf("Roots of quadratic equation are: ");
        printf("%.3f%+.3fi",-b/(2*a),sqrt(-d)/(2*a));
        printf(", %.3f%+.3fi",-b/(2*a),-sqrt(-d)/(2*a));
      
        return 0; 
      }
      else if(d==0){
       printf("Both roots are equal.\n");
    
       root1 = -b /(2* a);
       printf("Root of quadratic equation is: %.3f ",root1);
    
       return 0;
      }
      else{
       printf("Roots are real numbers.\n");
      
       root1 = ( -b + sqrt(d)) / (2* a);
       root2 = ( -b - sqrt(d)) / (2* a);
       printf("Roots of quadratic equation are: %.3f , %.3f",root1,root2);
      }
    
      return 0;
    }
    Sample output: Enter a, b and c of quadratic equation: 2 4 1 Roots are real numbers. Roots of quadratic equation are: -0.293, -1.707

    1. Report
  4. Question:How to find a b and c in a quadratic equation in the format of ax^2+bx+c: 2x^2+4x+-1. 

    Answer
    #include<stdio.h>
    #include<math.h>
    
    int main(){
      float a,b,c;
      float d,root1,root2;  
    
      printf("Enter quadratic equation in the format ax^2+bx+c: ");
      scanf("%fx^2%fx%f",&a,&b,&c);
       
      d = b * b - 4 * a * c;
      
      if(d < 0){
        printf("Roots are complex number.\n");
       
        return 0;
      }
     
       root1 = ( -b + sqrt(d)) / (2* a);
       root2 = ( -b - sqrt(d)) / (2* a);
       printf("Roots of quadratic equation are: %.3f , %.3f",root1,root2);
    
      return 0;
    }
    Sample output: Enter quadratic equation in the format ax^2+bx+c: 2x^2+4x+-1 Roots of quadratic equation are: 0.000, -2.000

    1. Report
  5. Question:Write a c program to check given string is palindrome number or not 

    Answer
    #include<string.h>
    #include<stdio.h>
    int main(){
      char *str,*rev;
      int i,j;
      printf("\nEnter a string:");
      scanf("%s",str);
      for(i=strlen(str)-1,j=0;i>=0;i--,j++)
          rev[j]=str[i];
          rev[j]='\0';
      if(strcmp(rev,str))
          printf("\nThe string is not a palindrome");
      else
          printf("\nThe string is a palindrome");
      return 0;
    }

    1. Report
  6. Question:Write a program to generate the Fibonacci series in c 

    Answer
    #include<stdio.h>
    int main(){
        int k,r;
        long int i=0l,j=1,f;
    
        //Taking maximum numbers form user
        printf("Enter the number range:");
        scanf("%d",&r);
    
        printf("FIBONACCI SERIES: ");
        printf("%ld %ld",i,j); //printing firts two values.
    
        for(k=2;k<r;k++){
             f=i+j;
             i=j;
             j=f;
             printf(" %ld",j);
        }
      
        return 0;
    }
    Sample output: Enter the number range: 15 FIBONACCI SERIES: 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377

    1. Report
  7. Question:Fibonacci series using array in c 

    Answer
    #include<stdio.h>
    int main(){
    
        int i,range;
        long int arr[40];
    
        printf("Enter the number range: ");
        scanf("%d",&range);
    
        arr[0]=0;
        arr[1]=1;
    
        for(i=2;i<range;i++){
             arr[i] = arr[i-1] + arr[i-2];
        }
    
        printf("Fibonacci series is: ");
        for(i=0;i<range;i++)
             printf("%ld ",arr[i]);
      
        return 0;
    }
    Sample output: Enter the number range: 20 Fibonacci series is: 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181

    1. Report
  8. Question:Fibonacci series in c using while loop 

    Answer
    #include<stdio.h>
    int main(){
        int k=2,r;
        long int i=0l,j=1,f;
    
        printf("Enter the number range:");
        scanf("%d",&r);
    
        printf("Fibonacci series is: %ld %ld",i,j);
    
        while(k<r){
             f=i+j;
             i=j;
             j=f;
             printf(" %ld",j);
              k++;
        }
      
        return 0;
    }
    Sample output: Enter the number range: 10 Fibonacci series is: 0 1 1 2 3 5 8 13 21 34

    1. Report
  9. Question:Sum of Fibonacci series in c 

    Answer
    #include<stdio.h>
    int main(){
        int k,r;
        long int i=0,j=1,f;
        long int sum = 1;
    
        printf("Enter the number range: ");
        scanf("%d",&r);
    
        for(k=2;k<r;k++){
             f=i+j;
             i=j;
             j=f;
             sum = sum + j;
        }
    
        printf("Sum of Fibonacci series is: %ld",sum);
      
        return 0;
    }
    Sample output: Enter the number range: 4 Sum of Fibonacci series is: 4

    1. Report
  10. Question:C program to find the factorial of a given number 

    Answer
    #include<stdio.h>
    int main(){
      int i=1,f=1,num;
    
      printf("Enter a number: ");
      scanf("%d",&num);
    
      while(i<=num){
          f=f*i;
          i++;
      }
    
      printf("Factorial of %d is: %d",num,f);
      return 0;
    }
    Sample output: Enter a number: 5 Factorial of 5 is: 120

    1. Report
Copyright © 2024. Powered by Intellect Software Ltd