## Monday, August 31, 2015

Content Removed

Content Removed

### C Programs on flow control statements (if, switch, loop, break and continue)-6

2015 & 2016 Batch Freshers Registration Link

C Program to Find GCD of two Numbers

Note: GCD of any two numbers( say a and b)  lies between 1 and minimum of two numbers

LCM(a,b) * GCD(a,b)=a*b

main()
{
int num1, num2, min,i;
printf("Enter two integers: ");
scanf("%d %d", &num1, &num2);

min=(num1>num2)?num2:num1; /* minimum value is stored in variable min */
for(i=min;i>=1;--i)
{
if(num1%i==0 && num2%i==0)
{
printf("HCF of %d and %d is %d", num1, num2,i);
break;
}
}
}

C Program to Find LCM of two Numbers

Note: LCM of any two numbers( say a and b)  lies between maximum of two given numbers and product of two numbers

LCM(a,b) * GCD(a,b)=a*b

main()
{
int num1, num2, max;
printf("Enter two positive integers: ");
scanf("%d %d", &num1, &num2);
max=(num1>num2) ? num1 : num2; /* maximum value is stored in variable max */
while(1)                       /* Always true. */
{
if(max%num1==0 && max%num2==0)
{
printf("LCM of %d and %d is %d", num1, num2,max);
break;          /* while loop terminates. */
}
++max;
}
}

C Program to Display Fibonacci Series

The Fibonacci Sequence is the series of numbers:
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ...
The next number is found by adding up the two numbers before it.
The 2 is found by adding the two numbers before it (1+1)
Similarly, the 3 is found by adding the two numbers before it (1+2),
And the 5 is (2+3),
and so on!
Example: the next number in the sequence above is 21+34 = 55
It is that simple!
Here is a longer list:

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368, 75025, 121393, 196418, 317811
/* Displaying Fibonacci sequence up to nth term where n is entered by user. */
#include <stdio.h>
int main()
{
int count, n, t1=0, t2=1, display=0;
printf("Enter number of terms: ");
scanf("%d",&n);
printf("Fibonacci Series: %d+%d+", t1, t2); /* Displaying first two terms */
count=2;    /* count=2 because first two terms are already displayed. */
while (count<n)
{
display=t1+t2;
t1=t2;
t2=display;
++count;
printf("%d+",display);
}
}

### C Programs on flow control statements (if, switch, loop, break and continue)-5

2015 & 2016 Batch Freshers Registration Link

C pattern programs

C program to print following pyramid pattern of stars

main()
{
int row, c, n, temp;
printf("Enter the number of rows in pyramid of stars you wish to see ");
scanf("%d",&n);

temp = n;
for ( row = 1 ; row <= n ; row++ )
{
/* for spacing purpose */
for ( c = 1 ; c < temp ; c++ )
printf(" ");

temp--;
/* for printing stars */
for ( c = 1 ; c <= 2*row - 1 ; c++ )
printf("*");
printf("\n");
}
}

Write a c program to print Pascal triangle.

In mathematics, Pascal's triangle is a triangular array of the binomial coefficients. In much of the Western world it is named after French mathematician Blaise Pascal, although other mathematicians studied it centuries before him in India, Iran, China, Germany, and Italy.

long factorial(int);
main()
{
int row, c, n, temp;
printf("Enter the number of rows you wish to see in pascal triangle\n");
scanf("%d",&n);

temp = n;
for ( row = 0 ; row < n ; row++ )
{
/* for spacing purpose */
for ( c = 1 ; c < temp ; c++ )
printf(" ");

temp--;
/* for printing stars */
for ( c = 0 ; c <= row ; c++ )
printf("%ld ",factorial(row)/(factorial(c)*factorial(row-c)));
printf("\n");
}
}

long factorial(int n)
{
int c;
long int result = 1;
for (c = 1; c <= n; c++)
result = result*c;
return result;
}

Write a c program for Floyd’s triangle.
This program prints Floyd's triangle. Number of rows of Floyd's triangle to print is entered by the user. First four rows of Floyd's triangle are as follows :-
1
2 3
4 5 6
7 8 9 10
It's clear that in Floyd's triangle nth row contains n numbers.

main()
{
int n, row,  c, a = 1;

printf("Enter the number of rows of Floyd's triangle to print\n");
scanf("%d", &n);

for (row = 1; row <= n; row++)
{
for (c = 1; c <= row; c++)
{
printf("%d ",a);
a++;
}
printf("\n");
}

}

### C Programs on flow control statements (if, switch, loop, break and continue)-4

2015 & 2016 Batch Freshers Registration Link

C Program to Find Factorial of a Number

main()
{
int i, num, fact = 1;
printf("Enter a number”);
scanf("%d", &num);

for (i = 1; i <= num; i++)
fact = fact * i;

printf("Factorial of %d = %d\n", num, fact);
}

C program to Generate Multiplication Table

main()
{
int i, num;
printf("Enter a number”);
scanf("%d", &num);

for (i = 1; i <= 10; i++)
printf(“%d * %d= %d”,num,i,num*i);

}

C program to calculate the Power of a Number

main()
{
int i, base,exp, power = 1;
printf("Enter base and exponent ”);
scanf("%d %d", &base, &exp);

for (i = 1; i <= exp; i++)
power = power * base;

printf(" %d  power  %d is :%d\n", base,exp,power);

}

Write a c program to find out NCR factor of given number

main()
{

printf(“Enter n and r values”);
scanf(“%d %d”,&n,&r);

printf("%ld ",factorial(n)/(factorial(r)*factorial(n-r));
}

long factorial(int n)
{
int c;
long int result =1;
for(c=1;c<=n;c++)
result=result * c;
return result;
}

## Sunday, August 30, 2015

### C Programs on flow control statements (if, switch, loop, break and continue)-3

2015 & 2016 Batch Freshers Registration Link

C Program to Count Number of Digits of an Integer
Input: Enter an integer : 234
Output: 3

main()
{
int num,count=0;
printf("Enter an integer: ");
scanf("%d", &num);
while(num!=0)
{
num=num/10;
count++;
}
printf("Number of digits: %d",count);
}

C Program to Reverse a Number

Input: Enter an integer : 234
Output: 432

main()
{
int num,rev=0,rem;
printf("Enter an integer: ");
scanf("%d", &num);
while(num!=0)
{
rem=num%10;
num=num/10;
rev=rev*10+rem;
}
printf("reverse of %d is %d",num,rev);
}

C Program to Check Whether a Number is Palindrome or Not

Input: Enter an integer : 234
Output: not a palindrome

main()
{
int num,rev=0,rem,orig;
printf("Enter an integer: ");
scanf("%d", &num);
orig=num;
while(num!=0)
{
rem=num%10;
num=num/10;
rev=rev*10+rem;
}
if(orig==rev)
printf("Palindrome”);
else
printf(“Not a palindrome”);

}

### C Programs on flow control statements (if, switch, loop, break and continue)-2

2015 & 2016 Batch Freshers Registration Link

Printing numbers from 1 to given number.

Intput:  Enter a number : 10
Output: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10

Program:

main()
{
int num,i;
printf(“Enter a number”);
scanf(“%d”,&num);
printf(“printing numbers from 1 to %d\n”,num);
for(i=1;i<=num;i++)
{
printf(“%d “,i);
}
}

Printing odd numbers from 1 to given number.

Intput:  Enter a number : 10
Output: 1,3, 5, 7, 9

Program:

main()
{
int num,i;
printf(“Enter a number”);
scanf(“%d”,&num);
printf(“printing numbers from 1 to %d\n”,num);
for(i=1;i<=num;i++)
{
if(num%2==1)
printf(“%d “,i);
}
}

Printing even numbers from 2 to given number.

Intput:  Enter a number : 10
Output:  2, 4, 6, 8, 10

Program:

main()
{
int num,i;
printf(“Enter a number”);
scanf(“%d”,&num);
printf(“printing numbers from 1 to %d\n”,num);
for(i=1;i<=num;i++)
{
if(num%2==0)
printf(“%d “,i);
}

}

C Program to Calculate Sum of Natural Numbers

Intput:  Enter a number : 10
Output: 55

Program:

main()
{
int num,i,sum=0;
printf(“Enter a number”);
scanf(“%d”,&num);

for(i=1;i<=num;i++)
{
sum=sum+i;
}
printf(“Sum of natural numbers from 1 to %d is %d”,num,sum);
}

Printing factors of a given number

Intput:  Enter a number : 10
Output: 1, 2, 5, 10

Program:

main()
{
int num,i;
printf(“Enter a number”);
scanf(“%d”,&num);
printf(“printing factors of %d\n”,num);
for(i=1;i<=num;i++)
{
if(num%i==0)
printf(“%d “,i);
}
}

Printing number of factors of a given number

Intput:  Enter a number : 10
Output:4

Program:

main()
{
int num,i,count=0;
printf(“Enter a number”);
scanf(“%d”,&num);

for(i=1;i<=num;i++)
{
if(num%i==0)
count++;
}
printf(“Number of factors of %d are %d\n”,num,count);
}

C Program to Check Whether a Number is Prime or Not

Intput:  Enter a number : 10
Output:not a prime

Program:

main()
{
int num,i,count=0;
printf(“Enter a number”);
scanf(“%d”,&num);

for(i=1;i<=num;i++)
{
if(num%i==0)
count++;
}
if(count==2)
printf(“Prime”);
else
printf(“not a prime”);
}

Printing sum of the factors of a given number

Intput:  Enter a number : 10
Output:18

Program:

main()
{
int num,i,sum=0;
printf(“Enter a number”);
scanf(“%d”,&num);

for(i=1;i<=num;i++)
{
if(num%i==0)
sum=sum+i;
}
printf(“sum of factors of %d are %d\n”,num,sum);
}

Write a c program to check given number is perfect number or not.

In number theory, a perfect number is a positive integer that is equal to the sum of its proper positive divisors, that is, the sum of its positive divisors excluding the number itself (also known as its aliquot sum).

Intput:  Enter a number : 6
Output: Perfect Number

Program:

main()
{
int num,i,sum=0;
printf(“Enter a number”);
scanf(“%d”,&num);

for(i=1;i<num;i++)
{
if(num%i==0)
sum=sum+i;
}
if(sum==num)
printf(“perfect number”);
else
printf(“Not a perfect number”);
}

### C Programs on flow control statements (if, switch, loop, break and continue)-1

2015 & 2016 Batch Freshers Registration Link

Write a c program to print Hello world without using any semicolon.

main()
{
if(printf(“Hello World”))
{
}
}

C Program to Check Whether a Number is Even or Odd

main()
{
int num;
printf(“ Enter a number “);
scanf(“ %d ”,&num);

if((num%2)==0)
{
printf(“Even”);
}
else
{
printf(“Odd);
}
}

C Program to Find the Largest Number between two Numbers

main()
{
int num1,num2,large;
printf(“ Enter two numbers “);
scanf(“ %d %d ”,&num1,&num2);

if(num1 > num2)
{
large=num1
}
else
{
large=num2;
}
printf( “Largest number is %d”,large);
}

C Program to Find the Smallest Number between two numbers

main()
{
int num1,num2,small;
printf(“ Enter two numbers “);
scanf(“ %d %d ”,&num1,&num2);

if(num1 < num2)
{
small=num1;
}
else
{
small=num2;
}
printf( “Smallest number is %d”,small);
}

C Program to Find the Largest Number among Three Numbers

main()
{
int num1,num2,num3,large;
printf(“ Enter three numbers “);
scanf(“ %d %d %d”,&num1,&num2,&num3);

if(num1 > num2 && num1 > num3)
{
large=num1;
}
else if(num2 > num1 && num2 > num3)
{
large=num2;
}
else
{
large=num3;
}
printf( “Largest number is %d”,large);
}

C Program to Find the Smallest Number among Three Numbers

main()
{
int num1,num2,num3,small;
printf(“ Enter three numbers “);
scanf(“ %d %d %d”,&num1,&num2,&num3);

if(num1 < num2 && num1 < num3)
{
small=num1;
}
else if(num2 < num1 && num2 < num3)
{
small=num2;
}
else
{
small=num3;
}
printf( “Smallest number is %d”,small);
}

C Program to Check Leap Year

main()
{
int year;
printf(“Enter year”);
scanf(“%d”,&year);

if(year%4==0 && (year % 100 !=0 || year % 400 ==0))
{
printf(“Leap year”);
}
else
{
printf(“not a leap year);
}

}