Tuesday, August 17, 2021

Basic Question and Answer on C using for, while, and do-while loop

Basic Question and Answer on C using for, while, and do-while loop



Here we will solve questions on if-else and switch statement

Q1.)  Write a program to find reverse of a number.

Ans.)

#include<stdio.h> 

 int main()   

{   

int n,r=0, rem;   

printf("Enter the value of a: ");   

  scanf("%d", &n);   

  while(n!=0)   

  {   

     rem=n%10;   

     r=r*10+rem;   

     n/=10;   

  }   

  printf("Reversed Number is %d",r);   

return 0; 

}

OUTPUT:



Q2.) Write a program to check the number is palindrome or not.

Ans.)

#include<stdio.h> 

int main()   

{   

int a,b,n=0,remaining;   

printf("Enter the value of a\n");   

scanf("%d",&a);   

b=a;   

while(a>0)   

{   

remaining=a%10;   

n=(n*10)+remaining;   

a=a/10;   

}   

if(b==n)   

printf("It is a palindrome number ");   

else   

printf("It is not a palindrome number");  

return 0; 

}

OUTPUT:



Q3.) Write a program to check the number is Armstrong or not.

Ans.)

#include<stdio.h>

int main(){

    int a,n,r=0,rem;

    printf("Enter the value of a\n");

    scanf("%d",&a);

    n=a;

    while(n!=0)

    {

        rem=n%10;

        r+=rem*rem*rem;

        n/=10;

    }

    if (r==a)

    {

        printf("%d is an Armstrong number”, a);

    }

    else

    {

        printf("%d is not an Armstrong number”, a);

    }

    return 0;

}

OUTPUT:



  Q4.) Write a program to check the number is Prime or not in a limit.

  Ans.)

#include<stdio.h>

int main(){

    int a,i,prime=1;

    printf("Enter the value of a\n");

    scanf("%d",&a);

    for ( i = 2; i < a; i++)

    {

        if (a%i==0)

        {

            prime=0;

            break;

        } 

    }

    if (prime==0){

        printf("This is not a prime number");

    }

    else{

        printf("This is a prime number");

    }

return 0;

}

OUTPUT:



Q5.) Write a program to print the Fibonacci Series to nth term.

Ans.)

#include<stdio.h>

int main(){

    long int i,n,a=0,b=1,c=a+b;

    printf("Enter the number of terms: ");

    scanf("%d", &n);

    printf("Fibonacci Series: %d, %d, ", a, b);

 

    for (i = 1; i <= n; ++i) {

        printf("%d, ", c);

        a = b;

        b = c;

        c = a + b;

    }

 

    return 0;

}

OUTPUT:


Q6.)  Write a program to find factorial of a number.

Ans.)

#include<stdio.h> 

int main()   

{   

 int i,fact=1,number;   

 printf("Enter a number: ");   

  scanf("%d",&number);   

    for(i=1;i<=number;i++){   

      fact*=i;   

  }   

  printf("Factorial of %d is: %d",number,fact);   

return 0; 

}

OUTPUT:


Q7.) Write a program to print 1+11+111+……+nth term.

Ans.)

#include <stdio.h>

 

int main()

{

  int n,i,t=1,sum=0;

  printf("Input the number of terms : ");

  scanf("%d",&n);

  for(i=1;i<=n;i++)

  {

     printf("%d",t);

      if (i<n)

      {

          printf("+");  

      }

     sum=sum+t;

     t=(t*10)+1;

  }

  printf("\nThe Sum is : %d\n",sum);

  return 0;

}

OUTPUT:


Q8.) Write a program to find sum of n number of natural numbers.

Ans.)

#include <stdio.h>

 

int main()

{

    int a=0, n, i;

    printf("Enter the number:\n");

    scanf("%d", &n);

    for (i = 1; i <= n; i++)

    {

        a += i;

    }

 

    printf("Sum of %d natural number is %d",n,a);

    return 0;

}

OUTPUT:


For more such questions, have a regular visit on our website messwithage
Thank you..............

No comments:

Post a Comment

Please do not enter any spam link in the comment box

Best of Our Website

Popular Posts