About Me

My photo
Raipur, Chhattisgarh, India
Hi , I am Amit Thakur. I have worked as a QA Engineer for two years and as a Java Developer for one year in NIHILENT TECHNOLOGIES PVT. LTD., Pune.Currently I am working as DEAN (Research & Development) in Bhilai Institute of Technology, Raipur.

Saturday, August 31, 2013

C Program To Print First 10 Natural Numbers

C Program to print first 10 Natural Numbers without using Conditional Loop
Using For Loop
#include
#include
void main()
{
int i=1;
clrscr();
for(i=1;i<=10;i++)
  printf("%d",i);
getch();
}

Using While Loop
#include
#include
void main()
{
int i=1;
clrscr();
while(i<=10)
  {
  printf("%d",i);
  i++;
  }
getch();
}

Using Do-While Loop
#include
#include
void main()
{
int i=1;
clrscr();
do{
  printf("%d",i);
  i++;
  }while(i<=10);
getch();
}

C Program to generate the Fibonacci Series starting from any two numbers

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

int main()
{
int first,second,sum,num,counter=0;
clrscr();

printf("Enter the term : ");
scanf("%d",&num);

printf("Enter First Number : ");
scanf("%d",&first);

printf("Enter Second Number : ");
scanf("%d",&second);

printf("Fibonacci Series : %d  %d  ",first,second);

while(counter< num)
    {
    sum=first+second;
    printf("%d  ",sum);
    first=second;
    second=sum;
    counter++;
    }
getch();
}

Output :
Enter the term : 5
Enter First Number : 1

Enter Second Number : 3

Fibonacci Series : 1 3 4 7 11 18 29

C Program to calculate sum of 5 subjects and find percentage


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

void main()
{
int s1,s2,s3,s4,s5,sum,total=500;
float per;

clrscr();

printf("Enter marks of 5 subjects : ");
scanf("%d%d%d%d%d",&s1,&s2,&s3,&s4,&s5);

sum = s1 + s2 + s3 + s4 + s5;

printf("Sum : %d",sum);

per = (sum * 100) / total;

printf("Percentage : %f",per);
getch();
}
Output :
Enter marks of 5 subjects : 80 70 90 80 80
Sum : 400
Percentage : 80.00

Program to show swap of two no’s without using third variable


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

void main()
{
int a,b;

clrscr();

printf("Enter value for num1 & num2 : ");
scanf("%d %d",&a,&b);

a=a+b;
b=a-b;
a=a-b;

printf("After swapping value of a : %d",a);
printf("After swapping value of b : %d",b);

getch();
}
Output :
Enter value for num1 & num2 : 10 20

After swapping value of a : 20
After swapping value of b : 10

C Program to reverse a given number !

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

void main()
{
int num,rem,rev=0;
clrscr();

printf("Enter any no to be reversed : ");
scanf("%d",&num);

 while(num>=1)
    {
    rem = num % 10;
    rev = rev * 10 + rem;
    num = num / 10;
    }

printf("Reversed Number : %d",rev);
getch();
}
Output :
Enter any no to be reversed : 123
Reversed Number : 321

C Program to find greatest in 3 numbers


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

void main()
{
int a,b,c;
clrscr();

printf("nEnter value of a, b & c: ");
scanf("%d %d %d",&a,&b,&c);

if((a>b)&&(a>c))
    printf("na is greatest");

if((b>c)&&(b>a))
    printf("nb is greatest");

if((c>a)&&(c>b))
    printf("nc is greatest");

getch();
}
Output :
Enter value for a,b & c : 15 17 21
c is greatest

Calender Program in C Programming Language : Display Day of the month

Calender Program in C Programming Language :

Program will accept Year,Month and Date from the user and will display the day of the month.
#include<stdio.h>
#include<conio.h>
#include<math.h>

int isdatevalid(int month, int day, int year)
{
  if (day <= 0) return 0 ;
  switch( month )
    {
      case 1:
      case 3:
      case 5:
      case 7:
      case 8:
      case 10:
      case 12: if (day > 31) return 0 ; else return 1 ;
      case 4:
      case 6:
      case 9:
      case 11: if (day > 30) return 0 ; else return 1 ;
      case 2:
        if ( day > 29 ) return 0 ;
        if ( day < 29 ) return 1 ;

    else return 0 ;
    }
  return 0 ;
}
//------------------------------------------------
int fm(int date, int month,int year)
{
int fmonth,leap;

//leap function 1 for leap & 0 for non-leap

if((year%100==0) && (year%400!=0))
    leap=0;
else if(year%4==0)
    leap=1;
else
    leap=0;

fmonth=3+(2-leap)*((month+2)/(2*month))+(5*month+month/9)/2;
//f(m) formula

fmonth = fmonth % 7; //bring it in range of 0 to 6

return fmonth;
}

//----------------------------------------------
int day_of_week(int date, int month, int year)
{
int dow; //day of week

int YY = year % 100;
int century = year / 100;

printf("nDate: %d/%d/%dnn",date,month,year);

dow = 1.25 *  YY + fm(date,month,year) + date - 2*( century % 4);
//function of weekday for Gregorian

dow = dow % 7; //remainder on division by 7

switch (dow)
    {
    case 0:
        printf("weekday = Saturday");
        break;
    case 1:
        printf("weekday = Sunday");
        break;
    case 2:
        printf("weekday = Monday");
        break;
    case 3:
        printf("weekday = Tuesday");
        break;
    case 4:
        printf("weekday = Wednesday");
        break;
    case 5:
        printf("weekday = Thursday");
        break;
    case 6:
        printf("weekday = Friday");
        break;
    default:
        printf("Incorrect data");
    }
return 0;
}
//------------------------------------------
void main()
{
int date,month,year;
clrscr();

printf("Enter the year ");
scanf("%d",&year);

printf("Enter the month ");
scanf("%d",&month);

printf("Enter the date ");
scanf("%d",&date);

day_of_week(date,month,year);

getch();
}

Output :

Enter the year 2012
Enter the month 02
Enter the date 29

Date: 29/2/2012

weekday = Wednesday

Friday, August 30, 2013

C Program to print table of n and square of n using pow()

Print Table of n and Square of n

#include<stdio.h>
#include<conio.h>
void main()
{
      int n;
      printf("Not Square");
      printf("-----------------n");
      for(n=1;n <=10;n++)
          printf("%d\t%d\n",n,n*n);
      getch();
}
Output :
 No Square
 -------------
  1 1
  2 4
  3 9
  4 16
  5 25
  6 36
  7 49
  8 64
  9 81
 10 100

Explanation of Program :

We can alternately write above program like this -
printf("%d\t%d\n",n,pow(n,2));
In order to use above line in the code we need to include math.h header file inside C program.
#include<math.h>

Alternate Way Using Pow :

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

void main()
{
      int n;
      printf("Not Square\n");
      printf("-----------------n");
      for(n=1;n <=10;n++)
          printf("%d\t%d\n",n,pow(n,2));
      getch();
}

Find Factorial of Number without using function


#include<stdio.h>
#include<conio.h>
void main()
{
int i,number,factorial;
printf("Enter the number : ");
scanf("%d",&n);

factorial = 1;
for(i=1;i<=n;i++)
      factorial = factorial * i;

printf("Factorial of %d is %d",n,factorial );
getch();
}
Output :
Enter the number : 5
Factorial of 5 is 120

Explanation of Program :

Before Explaining the program let us see how factorial is calculated -
Factorial of 5 = 5!
               = 5 * 4!
               = 5 * 4  * 3!
               = 5 * 4  * 3 * 2!
               = 5 * 4  * 3 * 2 * 1!
               = 5 * 4  * 3 * 2 * 1 * 0!
               = 5 * 4  * 3 * 2 * 1 * 1
               = 120
Firstly accept the number whose factorial is to be found.
printf("nEnter the number : ");
scanf("%d",&n);
We have to iterate from 1 to (n-1) using for/while/do-while loop. In each iteration we are going to multiply the current iteration number and result.
for(i=1;i<=n;i++)
      factorial = factorial * i;

Some Precautions to be taken :

Precaution 1 : Initialize ‘factorial’ variable

factorial = 1;
before going inside loop we must initialize factorial variable to 1 since by default each c variable have garbage value. If we forgot to initialize variable then garbage value will be multiplied and you will get garbage value as output.

Precaution 2 : For loop must start with 1

Suppose by mistake we write following statement -
for(i=0;i<=n;i++)
      factorial = factorial * i;
then in the very first iteration we will get factorial = 0 and in all successive iteration we will get result as 0 since anything multiplied by Zero is Zero

Precaution 3 : Try to accept lower value to calculate factorial

We have 2 bytes to store the integer in Borland C++ compiler, so we can have maximum limit upto certain thousand. Whenever we try to accept value greater than 20 we will get factorial overflow.