C questions and answer with explanation




1. PERFECT NUMBER.
void main()
{
  int n,i=1,sum=0;
  clrscr();
  printf("\nEnter a number:-");
  scanf("%d",&n);
  while(i<n)
  {
            if(n%i==0)
            sum=sum+i;
            i++;
  }
    if(sum==n)
            printf("\nThe no %d is a perfect number",i);
    else
            printf("\nThe no %d is not a perfect number",i);
  getch();
}
2. ARMSTRONG NUMBER.
void main()
{
  int num,r,sum=0,temp;
  clrscr();
  printf("\nEnter a number:-");
  scanf("%d",&num);
  temp=num;
  while(num!=0)
  {
            r=num%10;
            num=num/10;
            sum=sum+(r*r*r);
  }
    if(sum==temp)
            printf("\nThe number %d is an armstrong number",temp);
    else
            printf("\nThe number %d is not an armstrong number",temp);
  getch();
3. STRONG NUMBER
void main()
{
  int num,i,f,r,sum=0,temp;
  clrscr();
  printf("\nEnter a number");
  scanf("%d",&num);
  temp=num;
  while(num)
  {
            i=1,f=1;
            r=num%10;
            while(i<=r)
            {
                        f=f*i;
                        i++;
            }
            sum=sum+f;
            num=num/10;
  }
   if(sum==temp)
            printf("%d is a strong number",temp);
   else
            printf("%d is not a strong number",temp);
  getch();
}
4. PRIME NUMBER.
void main()
{
  int num,i,count=0;
  clrscr();
  printf("\nEnter a number:");
  scanf("%d",&num);
  for(i=1;i<=num;i++)
  {
            if(num%i==0)
               count++;
  }
   if(count==2)
            printf("%d is a prime number",num);
   else
            printf("%d is not a prime number",num);
  getch();
}
5. REVERSE A NUMBER
void main()
{
  int num,sum=0,r;
  clrscr();
  printf("\nEnter a number:");
  scanf("%d",&num);
  while(num)
  {
            r=num%10;
            sum=sum*10+r;
            num=num/10;
  }
   printf("\nReverse number=%d",sum);
  getch();
}
6. SUM OF THE DIGITS OF A NUMBER
void main()
{
  int num,sum=0,r;
  clrscr();
  printf("\nEnter a number:");
  scanf("%d",&num);
  while(num)
  {
            r=num%10;
            num=num/10;
            sum=sum+r;
  }
   printf("sum=%d",sum);
  getch();
}
7. PALINDROME NUMBER.
void main()
{
  int num,r,sum=0,temp;
  clrscr();
  printf("\nEnter a number:");
  scanf("%d",&num);
  temp=num;
  while(num)
  {
            r=num%10;
            num=num/10;
            sum=sum*10+r;
  }
   if(temp==sum)
            printf("\n%d is a palindrome",temp);
   else
            printf("\n%d is not a palindrome",temp);
  getch();
}
8. G.C.D OF TWO NUMBERS
void main()
{
  int n1,n2;
  clrscr();
  printf("\nEnter two numbers:");
  scanf("%d %d",&n1,&n2);
  while(n1!=n2)
  {
            if(n1>n2)
                        n1=n1-n2;
            else
                        n2=n2-n1;
  }
   printf("\nGCD=%d",n1);
  getch();
}
9. L.C.M OF TWO NUMBERS.
void main()
{
  int n1,n2,x,y;
  clrscr();
  printf("\nEnter two numbers:");
  scanf("%d %d",&n1,&n2);
  x=n1,y=n2;
  while(n1!=n2)
  {
            if(n1>n2)
                        n1=n1-n2;
            else
                        n2=n2-n1;
  }
   printf("L.C.M=%d",x*y/n1);
  getch();
}
10. SWAP TWO VARIABLES WITHOUT USING THIRD VARIABLE
void main()
{
  int a,b;
  clrscr();
  printf("\nEnter two numbers:");
  scanf("%d %d",&a,&b);
  printf("\nBefore swapping a=%d b=%d",a,b);
            a=a^b;
            b=b^a;
            a=a^b;
  printf("\nAfter swapping a=%d b=%d",a,b);
  getch();
}
11. FLOYD’S TRIANGLE
1
2 3
4 5 6
void main()
{
  int i,j,r,k=1;
  clrscr();
  printf("\nEnter the range:");
  scanf("%d",&r);
  printf("\nFLOYD'S TRIANGLE\n\n");
  for(i=1;i<=r;i++)
  {
            for(j=1;j<=i;j++,k++)
                        printf(" %d",k);
            printf("\n");
  }
  getch();
}
12. PRIME FACTORS OF A NUMBER
void main()
{
  int num,i=1,j,k;
  clrscr();
  printf("\nEnter a number:");
  scanf("%d",&num);
  while(i<=num)
  {
            k=0;
            if(num%i==0)
            {
                        j=1;
                        while(j<=i)
                        {
                         if(i%j==0)
                                      k++;
                         j++;
                         }
                         if(k==2)
                                      printf("\n%d is a prime factor",i);
            }
            i++;
  }
  getch();
}
13. MULTIPLICATION TABLE
void main()
{
  int r,i,j,k;
  clrscr();
  printf("\nEnter the number range:-");
  scanf("%d",&r);
  for(i=1;i<=r;i++)
  {
            for(j=1;j<=10;j++)
                     printf(" %d*%d=%d",i,j,i*j);
            printf("\n");
  }
  getch();
}
14. FACTORIAL OF A NUMBER
void main()
{
  int i=1,f=1,num;
  clrscr();
  printf("\nEnter a number:");
  scanf("%d",&num);
  while(i<=num)
  {
            f=f*i;
            i++;
  }
  printf("\nFactorial of %d is:%d",num,f);
  getch();
}
15. FIBONACCI SERIES
void main()
{
  int i=0,j=1,k=2,r,f;
  clrscr();
  printf("Enter the number range:");
  scanf("%d",&r);
  printf("\nFIBONACCI SERIES: ");
  printf("%d %d",i,j);
  while(k<r)
  {
            f=i+j;
            i=j;
            j=f;
            printf(" %d",j);
            k++;
  }
  getch();
}
Post a Comment