Archive for the ‘Basic Programming’ Category

/* Program to print the sum,difference and average of two numbers */

#include<stdio.h>
main()
{
int a,b,sum,diff,avg;
printf(“\nEnter two integers: “);
scanf(“%d%d”,&a,&b);
sum=a+b;
diff=a-b;
avg=(a+b)/2;
printf(“\nSum= %d\nDifference= %d\nAverage= %d\n”,sum,diff,avg);
}

Sum of Digits of a Number

Posted: February 12, 2010 in Basic Programming
Tags:

/* Program to find the sum of digits of a number */

#include<stdio.h>

main()
{
int num,i,remainder,sum=0;
printf(“\nEnter the number: “);
scanf(“%d”,&num);
while(num>0)
{
remainder=num%10;
sum+=remainder;
num/=10;
}
printf(“\nSum of digits: %d\n\n”,sum);
}

Reverse a Number

Posted: February 12, 2010 in Basic Programming
Tags:

/* Program to reverse a number */

#include<stdio.h>

main()
{
int num,rem,temp,reverse=0;
printf(“\nEnter the number: “);
scanf(“%d”,&num);
temp=num;
while(temp>0)
{
rem=temp%10;
reverse=reverse*10+rem;
temp/=10;
}
printf(“\nThe reverse of the number: %d\n\n”,reverse);
}

/* Program to find the roots of a quadratic equation */

#include<stdio.h>
#include<math.h>

main()
{
int a,b,c,descr;
double root1,root2,part;
printf(“\nEnter the co-efficients of a quadratic equation: “);
scanf(“%d%d%d”,&a,&b,&c);
descr=b*b-4*a*c;
if(descr<0)
printf(“\nThe roots are imaginary!!\n\n”);
else
{
if(descr==0)
printf(“\nThe roots are equal!\n\n”);
else
printf(“\nThe roots are unique!\n\n”);
part=sqrt((b*b)-(4*a*c));
root1=(-b+part)/(2*a);
root2=(-b-part)/(2*a);
printf(“\nThe roots are…..\nRoot1= %7.3f\nRoot2= %7.3f\n\n”,root1,root2);
}
}

/* Program to print the prime numbers within a range */

#include<stdio.h>

main()
{
int rangeStart,rangeEnd,i,j,flag;
printf(“\nStart of range: “);
scanf(“%d”,&rangeStart);
printf(“\nEnd of range: “);
scanf(“%d”,&rangeEnd);
for(i=rangeStart;i<=rangeEnd;i++)
{
flag=0;
for(j=2;j<=i/2;j++)
if(i%j==0)
flag=1;
if(flag==0)
printf(“%d\t”,i);
}
printf(“\n\n”);
}

Prime or Not

Posted: February 12, 2010 in Basic Programming
Tags:

/* Program to check whther the given number is prime or not */

#include<stdio.h>

main()
{
int num,i,flag=0;
printf(“\nEnter the num: “);
scanf(“%d”,&num);
for(i=2;i<=num/2;i++)
if(num%i==0)
flag=1;
if(flag==0)
printf(“\nThe given number is prime!\n\n”);
else
printf(“\nThe given number is not prime!\n\n”);
}

Palindrome or Not

Posted: February 12, 2010 in Basic Programming
Tags:

/* Program to check whether a given number is palindrome or not */

#include<stdio.h>

main()
{
int num,rem,temp,reverse=0;
printf(“\nEnter the number: “);
scanf(“%d”,&num);
temp=num;
while(temp>0)
{
rem=temp%10;
reverse=reverse*10+rem;
temp/=10;
}
if(reverse==num)
printf(“\nThe given number is palindrome!\n\n”);
else
printf(“\nThe given number is not palindrome!\n\n”);
}

Largest Among Three Numbers

Posted: February 12, 2010 in Basic Programming

/* Program to find the largest among three numbers */

#include<stdio.h>
main()
{
int a,b,c,large;
printf(“\nEnter three integers: “);
scanf(“%d%d%d”,&a,&b,&c);
large=a>b?(a>c?a:c):(b>c?b:c);
printf(“\nLargeest number: %d\n\n”,large);
}

N Fibonacci Numbers

Posted: February 12, 2010 in Basic Programming
Tags:

/* Program to generate ‘n’ fibonacci nubers */

#include<stdio.h>

main()
{
int first=0,second=1,third,n,i;
printf(“\nEnter the value of n: “);
scanf(“%d”,&n);
printf(“\nThe fibonacci series is ……\n”);
for(i=0;i<n;i++)
{
third=first+second;
printf(“%d\t”,first);
first=second;
second=third;
}
printf(“\n\n”);
}

Factorial of a Number

Posted: February 12, 2010 in Basic Programming

/* Program to find the factorial of a number */

#include<stdio.h>

main()
{
int num,i;
long int factorial=1;
printf(“\nEnter an integer number: “);
scanf(“%d”,&num);
for(i=1;i<=num;i++)
factorial*=i;
printf(“\nFactorial= %ld\n\n”,factorial);
}