C Code 18 _ To Find odd or even number, Prime or Not, Fibbonacci Series and Sum of Digits of a number (4 in 1)



C Code 18 _ To Find odd or even number, Prime or Not, Fibbonacci Series and Sum of Digits of a number (4 in 1) 


Program:



#include<stdio.h>
int main()
{
                int ch,n,i,p=0,a=0,b=1,c=0,sum=0,t,ans;
                printf("\n* * * * * * Program Menu * * * * * * *");
                printf("\n\n1 - Find odd or even");
                printf("\n\n2 - Find prime or not");
                printf("\n\n3 - Generate fibbonaci series");
                printf("\n\n4 - Find sum of digits");
                printf("\n\nEnter a choice : ");
                scanf("%d",&ch);
                switch(ch)
                {
                                case 1:
                                                printf("\n\n* * * * * * Odd or Even * * * * * *");
                                                printf("\n\nEnter a number : ");
                                                scanf("%d",&n);
                                                if(n<0)
                                                {
                                                                printf("%d is a negative number",n);
                                                }
                                                else if(n%2==0)
                                                {
                                                                printf("\n\n%d is a Even Number",n);
                                                }
                                                else
                                                {
                                                                printf("\n\n%d is a Odd Number",n);
                                                }
                                                break;
                                               
                                case 2:
                                                printf("\n\n* * * * * * Prime or Not * * * * * *");
                                                printf("\n\nEnter a number : ");
                                                scanf("%d",&n);
                                                if(n==1)
                                                {
                                                                p=1;
                                                }
                                                for(i=2;i<n;i++)
                                                {
                                                                if(n%i==0)
                                                                {
                                                                                p=1;
                                                                                break;
                                                                }
                                                }
            if(p==1)
            {
                printf("\n\n%d is Not a Prime Number",n);
                                                }
                                                else
                                                {
                                                                printf("\n\n%d is a Prime Number",n);
                                                }
                                                break;
                                               
                                case 3:
                                                printf("\n\n* * * * * * Fibbonacci Series * * * * * *");
                                                printf("\n\nEnter a number : ");
                                                scanf("%d",&n);
                                                printf("\n\nFibbonaci Series : ");
                                                for(i=1;i<=n;i++)
                                                {
                                                                printf("%d ",c);
                                                                c=a+b;
                                                                a=b;
                                                                b=c;
                                                }
                                                break;
                                               
                                case 4:
                                                printf("\n\n* * * * * * Sum of Digits * * * * * *");
                                                printf("\n\nEnter a number : ");
                                                scanf("%d",&n);
                                                ans=n;
                                                while(n>0)
                                                {
                                                                t=n%10;
                                                                sum=sum+t;
                                                                n=n/10;
                                                }
                                                printf("\n\nThe sum of digit of %d is %d",ans,sum);
                                                break;
                                               
                                default:
                                                printf("\n\nEnter choice only from 1 t0 4");
                }
                getch();
                return 0;
}



Output:

















Thank You ...
Previous Post
Next Post
Related Posts

0 comments:

Popular Posts