simple program in c language | CTechnotips

1. 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();
}

2. 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();
}

3. 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();
}

4. 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();
}

5. 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();
}

6. 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();
}

7. 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();
}

8. 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();
}

9. 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();
}

10. 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();
}

11. 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();
}

12. 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();
}

13. 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();
}

14. 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
  {
            f=i+j;
            i=j;
            j=f;
            printf(" %d",j);
            k++;
  }
  getch();
}

0 comments:

Post a Comment