Like us on Facebook

September 26, 2015

Basics and Common C Programs Part 2

  9/26/2015 No comments

Today I have posted programs to check prime number,program to find
GCD,LCM ,Sum of digit of a given number and How to write program
for Fibonacci Series in C language.
>

1. /*How To Check whether a number is prime or not in C*/

#include<stdio.h>
#include<conio.h>
//Header files required are declared

void main()
{
//Beginning Of main()
int i=2,n,f=0;// Required variables are declared and initialized
clrscr();//Output Screen cleared

printf("Enter The Number To Test :\n");
scanf("%d",&n);
if(n==0 || n==1)
printf("%d is not prime no.",n);
else
{
while(i<n)
{
if(n%i==0)
{
f=1;
break;
}
i++;
}
if(f==0)
printf("%d is a prime number.",n);
else
printf("%d is not a prime number.",n);
       }
getch();
}//End Of main()


2./*How To To find the GCD of two numbers in C */

#include <stdio.h>
#include <conio.h>
//Header files required are declared

void main()
{
//Beginning Of main()
int a,b,c,i,x;// Required variables are declared
clrscr();//Output Screen cleared

puts("Enter the two numbers : ");
scanf("%d%d",&a,&b);
      printf("\nGCD of %d and %d is : ",a,b);
if(a<b)
{
c=a;
a=b;
b=c;
}
while(b>0)
{
c=a%b;
a=a/b;
a=b;
b=c;
}
printf("%d\n",a);
getch();
}//End Of main()


3./*How To Find out the LCM of two numbers in C*/

#include <stdio.h>
#include <conio.h>
//Header files required are declared

void main()
{
//Beginning Of main()
int a,b,c,i,x;// Required variables are declared
clrscr();//Output Screen cleared

puts("Enter the two numbers : ");
scanf("%d%d",&a,&b);

if(a<b)
{
c=a;
a=b;
b=c;
}

x=a*b;

for(i=a;i<x;i++)
{
if(i%a==0 && i%b==0)
break;
}

printf("\nLCM of %d and %d is : %d \n",b,a,i);
getch();
}//End Of main()

4./*How To  Calculate the sum of digit of a number*/

#include<stdio.h>
#include<conio.h>
//Required header files are declared
void main()
{
//Beginning Of main()
int d,m,n,s=0;// Required variables are declared
clrscr();

printf("Enter The Number To Find Sum Of Digits : \n");
scanf("%d",&n);

m=n;
while(n>0)
{//Sum Of digits calculated
d=n%10;
s=s+d;
n=n/10;
}

printf("Sum Of  Digits Of %d  Is : %d.\n",m,s);
getch();
}//End Of main()


5./*How to write program for Fibonacci Series : 0 1 1 2 3 5 8 13 ..
.....nth term*/

#include <stdio.h>
#include <conio.h>
//Header files required are declared

void fibo(int n)
{//Beginning of function fibo()
int i,a=1,b=0,c;
printf("0 ");
for(i=1;i<=n;i++)
      {//Loop to calculate and print the fibonacci series
c=a+b;
printf("%d ",c);
a=b;
b=c;
}
}//End of function fibo()

void main()
{
//Beginning Of function main()
int n;
clrscr();
printf("\nEnter The Term : ");
scanf("%d",&n);
fibo(n);
getch();
}//End of function main()

Related Basics and Common C Programs Part 3

Author: Vikas Pandey

He is a software and web developer working for renowned European investment bank.He loves to share knowledge through blogging whatever he knows and encourages others as well to share the same to make world a better place to live.

0 comments:

.
© 2014-2015 Informational Digit : How-To & Tech Guides. The content is copyrighted to Vikas Pandey and may not be reproduced on other websites. WP themonic converted by Bloggertheme9.
TOP