C Programs Example 1

Prime Number PROGRAM

prime.c

#include<stdio.h>
#include<conio.h>

void main()
{
   int i,num;
   clrscr(); //Clears the screen
   printf("\n\t\tPRIME NUMBER CHECKING");
   printf("\n\t\t---------------------\n");
   printf("\nEnter the integer number to check for prime :");
   scanf("%d",&num);
   if(num==1)
   {      printf("\n 1 is neither prime nor composite");   }
   else   {
              /* Initializing i=2(smallest factor other than one) and
                 repeating for all values of i less than or equal to
                 n/2      */
       for(i=2;i<=num/2;i++)
       {      if(num%i==0)
                   break;       }
       if( i > num/2 )
                  printf("\n%d is a prime number",num);
       else
                  printf("\n%d is not a prime number",num);   }
   getch();

}


Armstrong Program

armstrong.c

#include<stdio.h>
int main()
{
           int x,r,n,i,s=0;
           printf("Enter the number to check whether armstrong or not : ");
            scanf("%d",&n);
           x=n;
           while(n>0)
           {
                       r=n%10;
                       s=s+(r*r*r);
                       n=n/10;
           }
           if(x==s)
                       printf("%d is armstrong",x);
           else
                       printf("%d is not armstrong",x);
           return 0;
}


Sum of N numbers Program

numbers.c

#include<stdio.h>
#define max 50
int main()
{
      int i=0,n,s=0,a[max];
      printf("Enter the number of natural numbers to find the sum : ");
      scanf("%d",&n);
      printf("\nEnter numbers\n");
       while(i<n)
      {   
                  scanf("%d",&a[i]);
                  s=s+a[i];
                  i++;
      }
      printf("\n\nSum of the numbers entered is %d",s);
      return 0;
}



Output
Enter the number of natural numbers to find the sum : 5
Enter numbers
1
2
3
4
5
Sum of the numbers entered is 15



palindrom number program

palindrom.c

#include<stdio.h>
int main()
{
  int x,r,n,s=0;
  printf("Enter the number to check whether a palindrome or not : ");
  scanf("%d",&n);
  x=n;
  while(n>0)
  {
               r=n%10;
               s=s*10+r;
               n=n/10;
  }
  if(s==x)
               printf("%d is a palindrome number",x);
  else
               printf("%d is not a palindrome number",x);
  return 0;
}


Output

Enter the number to check whether a palindrome or not :
121
121 is a palindrome number
Enter the number to check whether a palindrome or not :
123
123 is not a palindrome number

Fibonacci Series Program

fibo.c

#include<stdio.h>
int main()
{
              int x,y,s=0,n;
              printf("Enter the range : ");
              scanf("%d",&n);
              x=0;y=1;
              printf("\n\t\tFibonacci series\n%d\n%d\n",x,y);
              while(s<=n)
              {
              s=x+y;
              if(s<=n)
              printf("%d\n",s);
              x=y;
              y=s;
              }
              return 0;
}




Output
Enter the range : 20
               Fibonacci series
0
1
1
2
3
5
8
13


Maths Operation Using Menu Program

switch_menu.c

#include<stdio.h>
#include<conio.h>
void main()
{
    int a,b,op;
    char ch;
    clrscr();
    printf("\t\tMenu Driven Program for Basic Mathematical Operations\n");
    do
    {
      printf("Enter values for A and B : \n");
      scanf("%d%d",&a,&b);
      printf("\n Press 1 for Addition\n Press 2 for Subtraction\n Press 3 for Multiplication\n Press 4 for Division\n");
      printf("\nEnter operator : ");
      scanf("%d",&op);
      switch(op)
      {
                  case 1: printf("\n%d + %d = %d",a,b,a+b);
                               break;
                  case 2: printf("\n%d - %d = %d",a,b,a-b);
                               break;
                  case 3: printf("\n%d * %d = %d",a,b,a*b);
                               break;
                  case 4: printf("\n%d / %d = %f",a,b,a/b);
                               break;
                  default : printf("\nNot valid entry\n");
      }

      printf("\nDo you want to continue ? (y/n) : ");
      scanf("%c",&ch);
    }while(ch=='y'||ch=='Y');
    getch();
}

Comments

Popular posts from this blog

પટેલ સમાજનો ઈતિહાસ જાણો : કોણ અને ક્યાંથી આવ્યા હતા પાટીદારો

Python HTML Generator using Yattag Part 1

Java Event Delegation Model, Listener and Adapter Classes