Tuesday 18 September 2012

Q. Write a program to generate a following numbers triangle:


Q. Write a program to generate a following numbers triangle:(Where user entered number through keyboard, for example if num=5)

123454321
 2345432
  34543
   454
    5
Ans.
/* c program for number structure */
#include<stdio.h>
#include<conio.h>
int main()
{
 int num,r,c,sp;
 printf("Enter loop repeat number : ");
 scanf("%d",&num);
 for(r=1; r<=num; r++)
 {
   for(sp=r; sp>1; sp--)
      printf(" ");
   for(c=r; c<=num; c++)
      printf("%d",c);
   for(c=num-1; c>=r; c--)
      printf("%d",c);
   printf("\n");
 }
 getch():
 return 0;
}

case1:
Q. Write a program to generate a following numbers triangle:(Where user entered number through keyboard, for example if num=5)

123454321
 1234321
  12321
   121
    1

Ans.
/*c program for number triangle pyramid*/

#include<stdio.h>
#include<conio.h>
int main()
{
 int num,r=1,c,sp,x;
 printf("Enter loop repeat number(rows):");
 scanf("%d",&num);
 for(; num>=1; num--,r++)
 {
   for(sp=r; sp>1; sp--)
      printf(" ");
   for(c=1; c<=num; c++)
      printf("%d",c);
   for(x=num-1; x>=1; x--)
      printf("%d",x);
   printf("\n");
 }
 getch();
 return 0;
}
case2:
17. Design numbers tringle pyramid
Q. Write a program to generate a following numbers triangle:(Where user entered number through keyboard, for example if num=5)
    1
   121
  12321
 1234321
123454321

Ans.
/*c program for number triangle pyramid*/

#include<stdio.h>
#include<conio.h>
int main()
{
 int num,r,c,sp,x;
 printf("Enter loop repeat number(rows):");
 scanf("%d",&num);
 for(r=1; num>=r; r++)
 {
   for(sp=num-r; sp>=1; sp--)
      printf(" ");
   for(c=1; c<=r; c++)
      printf("%d",c);
   for(x=r-1; x>=1; x--)
      printf("%d",x);
   printf("\n");
 }
 getch():
 return 0;
}
/****************OUTPUT***************
Enter loop repeat number(rows): 5

                               1
                              121
                             12321
                            1234321
                          123454321
case3:
16. Design numbers tringle pyramid
Q. Write a program to generate a following numbers triangle:(Where user entered number through keyboard, for example if num=5)

12345
1234
 123
  12
   1

Ans.
/*c program for number triangle pyramid*/
#include<stdio.h>
#include<conio.h>
int main()
{
 int num,r=1,c,sp;
 printf("Enter loop repeat number(rows):");
 scanf("%d",&num);
 for(; num>=1; num--,r++)
 {
  for(sp=r; sp>1; sp--)
     printf(" ");
  for(c=1; c<=num; c++)
     printf("%d",c);
  printf("\n");
 }
 getch();
 return 0;
}
/****************OUTPUT***********
Enter loop repeat number(rows): 5

12345
1234
123
12
1

********************************/
case4:
15. Design numbers tringle pyramid
Q. Write a program to generate a following numbers triangle:(Where user entered number through keyboard, for example if num=5)

                            12345
                            1234
                            123
                            12
                            1
                         
Ans.
/*c program for number triangle*/
#include<stdio.h>
#include<conio.h>
int main()
{
 int num,c;
 printf("Enter loop repeat number(rows): ");
 scanf("%d",&num);
 for(; num>=1; num--)
 {
   for(c=1; c<=num; c++)
      printf("%d",c);
   printf("\n");
 }
 getch();
 return 0;
}
/****************OUTPUT***********
Enter loop repeat number(rows): 5

                            12345
                            1234
                            123
                            12
                            1

********************************/
case5:
14. Design numbers tringle pyramid
Q. Write a program to generate a following numbers triangle:(Where user entered number through keyboard, for example if num=5)
                     
                        1
                        21
                        321
                        4321
                        54321

Ans.
/*c program for number triangle pyramid*/
#include<stdio.h>
#include<conio.h>
int main()
{
 int num,c,r;
 printf("Enter loop repeat number(rows): ");
 scanf("%d",&num);
 for(r=1; num>=r; r++)
 {
  for(c=r; c>=1; c--)
     printf("%d",c);
  printf("\n");
 }
 getch();
 return 0;
}

/*************OUTPUT****************
Enter loop repeat number(rows): 5

                        1
                        21
                        321
                        4321
                        54321

************************************/
case6:
13. Design numbers tringle pyramid
Q. Write a program to generate a following numbers triangle:(Where user entered number through keyboard, for example if num=5)

                            54321
                            4321
                            321
                            21
                            1
Ans.
/*c program for number triangle pyramid*/

#include<stdio.h>
#include<conio.h>
int main()
{
 int num,c;
 printf("Enter loop repeat number(rows): ");
 scanf("%d",&num);
 for(; num>=1; num--)
 {
  for(c=num; c>=1; c--)
     printf("%d",c);
  printf("\n");
 }
 getch();
 return 0;
}

/*************OUTPUT****************
Enter loop repeat number(rows): 5

                            54321
                            4321
                            321
                            21
                            1

************************************/
case7:
12. Design numbers tringle pyramid
Q. Write a program to generate a following numbers triangle:
   (Where user entered number through keyboard, for example if num=5).
     




1
      12
     123
    1234
   12345
Ans.
/*c program for number triangle pyramid*/

#include<stdio.h>
#include<conio.h>
int main()
{
 int num,r,c,sp;
 printf("Enter loop repeat number(rows): ");
 scanf("%d",&num);
 for(r=1; num>=r; r++)
 {
  for(sp=num-r; sp>=1; sp--)
     printf(" ");
  for(c=1; c<=r; c++)
     printf("%d",c);
  printf("\n");
 }
 getch();
 return 0;
}

/*************OUTPUT****************
Enter loop repeat number(rows): 5
1
12
123
1234
12345

************************************/
case8:
11.Design numbers tringle pyramid
Q. Write a program to generate a following numbers triangle:
   (Where user entered number through keyboard, for example if num=5)

                                12345
                                1234
                                123
                                12
                                1
Ans.
/*c program for number triangle pyramid*/
#include<stdio.h>

#include<conio.h>
int main()
{
 int num,c;
 printf("Enter loop repeat number(rows): ");
 scanf("%d",&num);
 for(; num>=1; num--)
 {
  for(c=1; c<=num; c++)
     printf("%d",c);
  printf("\n");
 }
 return 0;
}

/*************OUTPUT****************
Enter loop repeat number(rows): 5

                                12345
                                1234
                                123
                                12
                                1

*********************************/
case9:
9.Design numbers rectangle structure
Q. Write a program to generate a following numbers structure:
   (Where user entered number through keyboard, for example if num=5)
                            11111
                            22222
                            33333
                            44444
                            55555
                         
Ans.


#include<stdio.h>
#include<conio.h>
int main()
{
 int num,r,c;
 printf("Enter loop repeat number(rows): ");
 scanf("%d",&num);
 for(r=1; num>=r; r++)
 {
  for(c=num; c>=1; c--)
     printf("%d",r);
  printf("\n");
 }
 getch();
 return 0;
}
/*************OUTPUT****************
Enter loop repeat number(rows): 5

                            11111
                            22222
                            33333
                            44444
                            55555

************************************/
case10:
10. Design numbers tringle pyramid
Q. Write a program to generate a following numbers triangle:
   (Where user entered number through keyboard, for example if num=5)

                        1
                        12
                        123
                        1234
                        12345
Ans.

/* c program for number triangle*/
#include<stdio.h>
#include<conio.h>
int main()
{
 int num,r,c;
 printf("Enter loop repeat number(rows): ");
 scanf("%d",&num);
 for(r=1; num>=r; r++)
 {
  for(c=1; c<=r; c++)
     printf("%d",c);
  printf("\n");
 }
 getch();
 return 0;
}

/*************OUTPUT**************
Enter loop repeat number(rows): 5

                        1
                        12
                        123
                        1234
                        12345

***********************************/
case11:
8.Design numbers rectangle structure
Q. Write a program to generate a following numbers structure:
   (Where user entered number through keyboard, for example if num=5)
                            55555
                            44444
                            33333
                            22222
                            11111

Ans.

/* c program for number structure*/
#include<stdio.h>
#include<conio.h>
int main()
{
 int num,r,c;
 printf("Enter loop repeat number(rows): ");
 scanf("%d",&num);
 for(r=num; r>=1; r--)
 {
  for(c=num; c>=1; c--)
     printf("%d",r);
  printf("\n");
 }
 getch();
 return 0;
}
/*************OUTPUT**************
Enter loop repeat number(rows): 5

                            55555
                            44444
                            33333
                            22222
                            11111

***********************************/  
case12:
7.Design numbers rectangle structure
Q. Write a program to generate a following numbers structure:
   (Where user entered number through keyboard, for example if num=5)

                                54321
                                54321
                                54321
                                54321
                                54321

Ans.

/* c program for symbol triangle*/
#include<stdio.h>
#include<conio.h>
int main()
{
 int num,r,c;
 printf("Enter loop repeat number(rows): ");
 scanf("%d",&num);
 for(r=1; num>=r; r++)
 {
  for(c=5; c>=1; c--)
     printf("%d",c);
  printf("\n");
 }
 getch();
 return 0;
}
/*************OUTPUT**************
Enter loop repeat number(rows): 5

                                54321
                                54321
                                54321
                                54321
                                54321

***********************************/
case13:
           6.Design numbers rectangle structure
Q. Write a program to generate a following numbers structure:
   (Where user entered number through keyboard, for example if num=5)

                        12345
                        12345
                        12345
                        12345
                        12345

Ans.

/* c program for number rectangle*/
#include<stdio.h>
#include<conio.h>
int main()
{
 int num,r,c;
 printf("Enter loop repeat number(rows): ");
 scanf("%d",&num);
 for(r=1; num>=r; r++)
 {
  for(c=1; c<=num; c++)
   printf("%d",c);
  printf("\n");
 }
 getch();
 return 0;
}

/*************OUTPUT**************
Enter loop repeat number(rows): 5

                        12345
                        12345
                        12345
                        12345
                        12345

***********************************/
case14:
4.Design triangle pyramid
Q. Write a program to generate a following #'s triangle?
                 
#
##
###
####
#####
Ans.

/* c program for symbol triangle*/
#include<stdio.h>
#include<conio.h>
int main()
{
 int num,r,c;
 printf("Enter loop repeat number(rows): ");
 scanf("%d",&num);
 for(r=1; num>=r; r++)
 {
  for(c=1; c<=r; c++)
     printf("#");
  printf("\n");
 }
 getch();
 return 0;
}

/*************OUTPUT**************
Enter loop repeat number(rows): 5

#
##
###
####
#####

***********************************/

case15:
5.Design triangle pyramid
Q. Write a program to generate a following @'s triangle?
 @
@@
@@ @
@@@@
@@ @@ @
Ans.
/* c program for symbol triangle*/
#include<stdio.h>
#include<conio.h>
int main()
{
 int num,r,c,sp;
 printf("Enter loop repeat number(rows): ");
 scanf("%d",&num);
 for(r=1; num>=r; r++)
 {
  for(sp=num-r; sp>=1; sp--)
     printf(" ");
  for(c=1; c<=r; c++)
     printf("@");
  printf("\n");
 }
 getch();
 return 0;
}
/*************OUTPUT**************
Enter loop repeat number(rows): 5





@
      @@
     @@@
    @@@@
   @@@@@

***********************************/
case16:
3.Design triangle pyramid
Q. Write a program to generate a following @'s triangle?  
             

@@ @@ @
@@ @@
@@ @
@@
@
Ans.

/* c program for symbol triangle*/
#include<stdio.h>
#include<conio.h>
int main()
{
 int num,c;
 printf("Enter loop repeat number(rows): ");
 scanf("%d",&num);
 for(; num>=1; num--)
 {
  for(c=1; c<=num; c++)
     printf("@");
  printf("\n");
 }
 getch();
 return 0;
}

/*************OUTPUT**************
Enter loop repeat number(rows): 5

@@@@@
@@@@
@@@
@@
@

***********************************/
case17:
2. Design triangle pyramid
Q. Write a program to generate a following #'s triangle?

#####
 ####
 ###
  ##
   #

Ans.

/* c program for symbol triangle*/
#include<stdio.h>
#include<conio.h>
int main()
{
 int num,r=1,c,sp;
 printf("Enter loop repeat number(rows): ");
 scanf("%d",&num);
 for(; num>=1; num--,r++)
 {
  for(sp=r; sp>1; sp--)
     printf(" ");
  for(c=1; c<=num; c++)
    printf("#");
  printf("\n");
 }
 getch();
 return 0;
}

/*************OUTPUT**************
Enter loop repeat number(rows): 5
#####
 ####
  ###
   ##
    #
***********************************/
case18:
1. Design rectangle pyramid
Q. Write a program to generate a following structure?
             
                @@@@@
                @@@@@
                @@@@@
                @@@@@
                @@@@@
             
Ans.  

/* c program for symbol structure*/
#include<stdio.h>
#include<conio.h>
int main()
{
 int num,r,c;
 printf("Enter loop repeat number(rows): ");
 scanf("%d",&num);
 for(r=1; num>=r; r++)
 {
  for(c=1; c<=num; c++)
     printf("@");
  printf("\n");
 }
 getch();
 return 0;
}

/************* OUTPUT **************
Enter loop repeat number(rows): 5

                @@@@@
                @@@@@
                @@@@@
                @@@@@
                @@@@@
***********************************/
case19:
Q. Write a c program for following number structure :
1
22
333
4444
55555
4444
333
22
1

Ans.

/*c program for above triangle structure using while*/
#include<stdio.h>
#include<conio.h>
int main()
{
 int num,n,r=1,c;
 printf("Enter triangle number : ");
 scanf("%d",&num);
 while(num >= r)
 {
  c=1;
  while(c <= r)
  {
    printf("%d",r);
    c++;
  }
  printf("\n");
  r++;
 }
 n=num-1;
 while(n >= 1)
 {
   c=1;
   while(c <= n)
   {
     printf("%d",n);
     c++;
   }
   printf("\n");
   n--;
 }
 getch();
 return 0;
}
OR
/*c program for above number triangle using for loop*/

#include<stdio.h>
#include<conio.h>
int main()
{
 int num,n,r,c;
 printf("Enter triangle number : ");
 scanf("%d",&num);
 for(r=1; r<=num; r++)
 {
   for(c=1; c<=r; c++)
      printf("%d",r);
   printf("\n");
 }
 for(n=num-1; n>=1; n--)
 {
   for(c=1; c<=n; c++)
      printf("%d",n);
   printf("\n");
 }
 return 0;
}


Output :-

Enter triangle number :5
1
22
333
4444
55555
4444
333
22
1
case20:
Pyramid triangle
Q. Write a C program for following structure:


9
0 1
2 3 4
5 6 7 8
9 0 1 2 3


Ans.


/*c program for triangle*/
#include<stdio.h>
#include<conio.h>
int main()
{
 int i,c,r,x=0,n=4;
 printf("9\n");
 for(r=1; n>=r; r++)
 {
  for(c=0; c<=r; c++,x++)
  {
   if(x<10)
     printf("%d ",x);
   else
   {
     for(i=0; i<=3; i++)
        printf("%d ",i);
     break;
   }
  }
  printf("\n");
 }
 getch();
 return 0;
}
case21;
Q. Write a C program to print the following number triangle:


1
12
123
1234
12345
1234
123
12
1


Ans.


/*c program for print the number pyramid as specific given design*/
#include<stdio.h>
#include<conio.h>
int main()
{
 int r,c,n;
 printf("Enter loop repeat number(rows): ");
 scanf("%d", &n);
 for(r=1; r<=n; r++)
 {
   for(c=1; c<=r; c++)
      printf("%d",c);
   printf("\n");
 }
 for(r=n; r>1; r--,n--)
 {
   for(c=1; c<n; c++)
      printf("%d",c);
   printf("\n");
 }
 getch();
 return 0;
}

case22:
Q. Write a C program to print the following number structure:


          1
         2 2
        3 3 3
       4 4 4 4
      5 5 5 5 5
     6 6 6 6 6 6


Ans.


/*c program for print the following number triangle*/
#include<stdio.h>
#include<conio.h>
int main()
{
 int r,c,sp,n=6;
 for(r=1; r<=6; r++)
 {
  for(sp=1; sp<=n; sp++)
     printf(" ");
  for(c=1; c<=r; c++)
  {
     printf("%d",r);
     printf(" ");
  }
  printf("\n");
  n=n-1;
 }
 getch();
 return 0;
}
case23:
Q. Write a C program to print the following number triangle or number structure:


           1
         1 2 3
       1 2 3 4 5
     1 2 3 4 5 6 7
   1 2 3 4 5 6 7 8 9


Ans.


/*c program for above number triangle codes*/
#include<stdio.h>
#include<conio.h>
int main()
{
 int r,c,sp,num;
 printf("Enter loop repeat number(rows): ");
 scanf("%d", &num);
 for(r=1; r<=num; r++)
 {
  for(sp=1; sp<=num-r; sp++)
     printf("  ");   //it is 2 blank space
  for(c=1; c<=2*r-1; c++)
     printf(" %d",c);
  printf("\n");
 }
 getch();
 return 0;
}
case24:
Q. Write a C program to print the following number structure:


          1
         1 2
        1 2 3
       1 2 3 4
      1 2 3 4 5
     1 2 3 4 5 6


Ans.


/*c program to print the above number pyramid*/
#include<stdio.h>
#include<conio.h>
int main()
{
 int r,c,sp,num;
 printf("Enter loop repeat number(rows): ");
 scanf("%d", &num);
 for(r=1; r<=num; r++)
 {
   for(sp=num; sp>=r; sp--)
      printf(" ");
   for(c=1; c<=r; c++)
      printf(" %d", c);
   printf("\n");
 }
 getch();
 return 0;
}

case25:
Q. Write a C program to display the following number structure:


1234554321
1234__4321
123____321
12______21
1________1


Ans.


/* c program for number rectangle or number pyramid*/
#include<stdio.h>
#include<conio.h>
int main()
{
 int num,c,sp,r=1;
 printf("Enter loop repeat number(rows): ");
 scanf("%d",&num);
 printf("\n");
 for(; num>=1; num--,r++)
 {
  for(c=1; c<=num; c++)
     printf("%d",c);
  for(sp=r; sp>1; sp--)
     printf("_");
  for(sp=r; sp>1; sp--)
     printf("_");
  for(c=num; c>=1; c--)
     printf("%d",c);
  printf("\n");
 }
 getch();
 return 0;
}
case26:
Q. Write a C program to print the following number triangle or structure:


     1
    21
   321
  4321
 54321


Ans.


/*c program code for number pyramid*/
#include<stdio.h>
int main()
{
 int num,r,c,sp;
 printf("Enter number of rows: ");
 scanf("%d", &num);
 for(r=1; r<=num; r++)
 {
   for(sp=num-r; sp>0; sp--)
      printf(" ");
   for(c=r; c>=1; c--)
      printf("%d",c);
   printf("\n");
 }
 getch();
 return 0;
}

case27:
Q. Write a C program to print the following number pyramid:


54321
 4321
  321
   21
    1


Ans.


/*c program code for number triangle*/
#include<stdio.h>
int main()
{
 int num,r,c,sp;
 printf("Enter number of rows: ");
 scanf("%d", &num);
 for(r=1; r<=5; r++,num--)
 {
   for(sp=r; sp>1; sp--)
     printf(" ");
   for(c=num; c>=1; c--)
     printf("%d",c);
   printf("\n");
 }
 getch();
 return 0;
}

Monday 17 September 2012

C program to find hcf and lcm:


DESCRIPTION:
The code below finds highest common factor and least common
multiple of two integers. HCF is also known as
greatest common divisor(GCD) or greatest common factor(gcf).
CODE:
C programming code
#include <stdio.h>

int main() {
  int a, b, x, y, t, gcd, lcm;

  printf("Enter two integers\n");
  scanf("%d%d", &x, &y);

  a = x;
  b = y;

  while (b != 0) {
    t = b;
    b = a % b;
    a = t;
  }

  gcd = a;
  lcm = (x*y)/gcd;

  printf("Greatest common divisor of %d and %d = %d\n", x, y, gcd);
  printf("Least common multiple of %d and %d = %d\n", x, y, lcm);

  return 0;
}
C program to find hcf and lcm using recursion
#include <stdio.h>

long gcd(long, long);

int main() {
  long x, y, hcf, lcm;

  printf("Enter two integers\n");
  scanf("%ld%ld", &x, &y);

  hcf = gcd(x, y);
  lcm = (x*y)/hcf;

  printf("Greatest common divisor of %ld and %ld = %ld\n", x, y, hcf);
  printf("Least common multiple of %ld and %ld = %ld\n", x, y, lcm);

  return 0;
}

long gcd(long a, long b) {
  if (b == 0) {
    return a;
  }
  else {
    return gcd(b, a % b);
  }
}
C program to find hcf and lcm using function
#include <stdio.h>

long gcd(long, long);

int main() {
  long x, y, hcf, lcm;

  printf("Enter two integers\n");
  scanf("%ld%ld", &x, &y);

  hcf = gcd(x, y);
  lcm = (x*y)/hcf;

  printf("Greatest common divisor of %ld and %ld = %ld\n", x, y, hcf);
  printf("Least common multiple of %ld and %ld = %ld\n", x, y, lcm);

  return 0;
}

long gcd(long x, long y) {
  if (x == 0) {
    return y;
  }

  while (y != 0) {
    if (x > y) {
      x = x - y;
    }
    else {
      y = y - x;
    }
  }

  return x;
}
OUTPUT:
ENTER TWO INTEGERS:
9 15
GREATEST COMMON DIVISOR OF 9 AND 15= 3
LEAST COMMON MULTIPLE OF 9 AND 15=45

c program to check leap year




DESCRIPTION:
c program to check leap year: c code to check leap year,
year will be entered by the user. Please read the leap year
article before reading the code, it will help you to understand the program.
C programming code
#include <stdio.h>

int main()
{
  int year;

  printf("Enter a year to check if it is a leap year\n");
  scanf("%d", &year);

  if ( year%400 == 0)
    printf("%d is a leap year.\n", year);
  else if ( year%100 == 0)
    printf("%d is not a leap year.\n", year);
  else if ( year%4 == 0 )
    printf("%d is a leap year.\n", year);
  else
    printf("%d is not a leap year.\n", year);

  return 0;
}
OUTPUT
ENTER A YEAR TO CHECK IF IT IS A LEAP YEAR:
2012
2012 IS A LEAP YEAR

c program to print Floyd's triangle



DESCRIPTION:
C program to print 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.
code;
#include <stdio.h>

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

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

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

  return 0;
}
output:
enter the no of rows of floyds triangle to print:
5
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
case1:
#include<stdio.h>

main()
{
    int c, n, k;
    char ch = 'A';


    printf("Enter number of rows\n");
    scanf("%d",&n);

    for ( c = 1 ; c <= n ; c++ )
    {
        for ( k = 1 ; k <= c ; k++)
            printf("%c ", ch);

        printf("\n");
        ch++;
    }

    return 0;
}
output:
A
BB
CCC
DDDD
EEEEEE
CASE2:
#include<stdio.h>

main()
{
   int n, c, k, space = 0;
   char ch, temp;

   scanf("%d", &n);

   ch = 'A'+n-1;
   temp = ch;

   for ( k = n ; k >= 1 ; k-- )
   {
      for ( c = 1 ; c <= space; c++)
         printf(" ");

      space++;

      for ( c = 1 ; c <= k ; c++ )
      {
         printf("%c",temp);
         temp--;
      }

      printf("\n");
      ch--;
      temp = ch;
   }

   return 0;
}
OUTPUT:
EDCBA
 DCBA
  CBA
   BA
    A

c program to generate and print armstrong numbers


DESCRIPTION:
armstrong number in c: This program prints armstrong number. In our program we ask the user to enter a number and then we use a loop from one to the entered number and check if it is an armstrong number and if it is then the number is printed on the screen. Remember a number is armstrong if the sum of cubes of individual digits of a number is equal to the number itself. For example 371 is an armstrong number as 33 + 73 + 13 = 371. Some other armstrong numbers are 0, 1, 153, 370, 407.
C code
#include<stdio.h>
#include<conio.h>

main()
{
   int r;
   long number = 0, c, sum = 0, temp;

   printf("Enter the maximum range upto which you want to find armstrong numbers ");
   scanf("%ld",&number);

   printf("Following armstrong numbers are found from 1 to %ld\n",number);

   for( c = 1 ; c <= number ; c++ )
   {
      temp = c;
      while( temp != 0 )
      {
         r = temp%10;
         sum = sum + r*r*r;
         temp = temp/10;
      }
      if ( c == sum )
         printf("%ld\n", c);
      sum = 0;
   }

   getch();
   return 0;
}
Armstrong number program executable.
OUTPUT;
Enter the maximum range upto which you want to find armstrong numbers1000
 Following armstrong numbers are found from 1 to 1000
1
153
370
371
407
purpose of returning zero

Every c program begins with function main,
and main is called by operating system of your computer .
 So we return a value to os indicating whether our program
has successfully executed or an error has occurred. Returning zero
indicates success i.e. program has been executed successfully.armstrong number c program
.CASE1:
armstrong number c program: c programming code to check
whether a number is armstrong or not
DESCRIPTION:
 A number is armstrong
if the sum of cubes of individual digits of a number
is equal to the number itself. For example 371 is an
armstrong number as 33 + 73 + 13 = 371.
Some other armstrong numbers are: 0, 1, 153, 370, 407.
C programming code
#include <stdio.h>

main()
{
   int number, sum = 0, temp, remainder;

   printf("Enter a number\n");    
   scanf("%d",&number);

   temp = number;

   while( temp != 0 )
   {
      remainder = temp%10;
      sum = sum + remainder*remainder*remainder;
      temp = temp/10;
   }

   if ( number == sum )
      printf("Entered number is an armstrong number.");
   else
      printf("Entered number is not an armstrong number.");        

   return 0;
}
OUTPUT;
ENTER A NUMBER
407
ENTER NUMBER IS AN ARMSTRONG NUMBER

anagram in c


DESCRIPTION;
Anagram in c: c program to check whether two strings are anagrams or not, string is assumed to consist of alphabets only. Two words are said to be anagrams of each other if the letters from one word can be rearranged to form the other word. From the above definition it is clear that two strings are anagrams if all characters in both strings occur same number of times. For example "abc" and "cab" are anagram strings, here every character 'a', 'b' and 'c' occur only one time in both strings. Our algorithm tries to find how many times characters appears in the strings and then comparing their corresponding counts.
PROGRAM
C anagram programming code
#include <stdio.h>

int check_anagram(char [], char []);

int main()
{
   char a[100], b[100];
   int flag;

   printf("Enter first string\n");
   gets(a);

   printf("Enter second string\n");
   gets(b);

   flag = check_anagram(a, b);

   if (flag == 1)
      printf("\"%s\" and \"%s\" are anagrams.\n", a, b);
   else
      printf("\"%s\" and \"%s\" are not anagrams.\n", a, b);

   return 0;
}

int check_anagram(char a[], char b[])
{
   int first[26] = {0}, second[26] = {0}, c = 0;

   while (a[c] != '\0')
   {
      first[a[c]-'a']++;
      c++;
   }

   c = 0;

   while (b[c] != '\0')
   {
      second[b[c]-'a']++;
      c++;
   }

   for (c = 0; c < 26; c++)
   {
      if (first[c] != second[c])
         return 0;
   }

   return 1;
}
OUTPUT:
ENTER THE FIRST STRING
CARE
ENTER SECOND STRING
RACE
"CARE" AND "RACE" ARE ANAGRAMS

Pascal Triangle in c:


DESCRIPTION:
Pascal Triangle in c: C program to print Pascal triangle which you might have studied in Binomial Theorem in Mathematics. Number of rows of Pascal triangle to print is entered by the user. First four rows of Pascal triangle are shown below :-

   1
  1 1
 1 2 1
1 3 3 1
PROGRAM:
Pascal triangle in c
#include<stdio.h>

long factorial(int);

main()
{
   int i, n, c;

   printf("Enter the number of rows you wish to see in pascal triangle\n");
   scanf("%d",&n);

   for ( i = 0 ; i < n ; i++ )
   {
      for ( c = 0 ; c <= ( n - i - 2 ) ; c++ )
         printf(" ");

      for( c = 0 ; c <= i ; c++ )
         printf("%ld ",factorial(i)/(factorial(c)*factorial(i-c)));

      printf("\n");
   }

   return 0;
}

long factorial(int n)
{
   int c;
   long result = 1;

   for( c = 1 ; c <= n ; c++ )
         result = result*c;

   return ( result );
}
ENTER THE NUMBER OF ROWS YOU WISH TO SEE IN PASCAL TRIANGLE5
      1
     1 1
    1 2 1
   1 3 3 1
  1 4 6 4 1
source code of pattern below:

   1
  121
 12321
1234321 
#include<stdio.h>

main()
{
    int n, c, k, number = 1, space = n;

    printf("Enter number of rows\n");
    scanf("%d",&n);

    space = n;

    for ( c = 1 ; c <= n ; c++ )
    {
        for ( k = space ; k > 1 ; k-- )
            printf(" ");

        space--;

        for ( k = 1 ; k <= 2*c - 1 ; k++ )
        {
            if ( k <= c)
            {
                 printf("%d", number);

                 if ( k < c )
                 number++;
            }
            else
            {
                number--;
                printf("%d", number);
            }
        }

        number = 1;
        printf("\n");
    }

    return 0;
}

C program to find next prime palindrome


DESCRIPTION:
C program to find next prime palindrome: In this code user will enter a number(say n) and we have to find a number greater than n which is a palindrome as well as prime. For example if the input is 7 then output will be 11 as it is prime as well as palindrome, if input is 21 then output will be 101. In our code we first check if a number is palindrome and then check if it is prime as it will take less time as primes occur more frequently then palindromes.
PROGRAM:
C programming code
#include <stdio.h>
#include <math.h>
#define TRUE 1
 int main()
{
  long n, t, r = 0, c, d;

  printf("Enter an integer\n");
  scanf("%ld", &n);    /* n must be a natural number */

  while (TRUE)
  {
    n++;
    t = n;

    /* Calculating reverse of number */

    while(t)
    {
      r *= 10;  /* Compound assignment operator r*=10 => r=r*10 */
      r += t%10;
      t /= 10;
    }

    /* if reverse equals original then it is palindrome */

    if (r == n)
    {
      d = (int)sqrt(n);

      /* Checking prime */

      for (c = 2; c <= d; c++)
      {
        if (n%c == 0)
          break;
      }
      if (c == d+1)
        break;
    }
    r = 0;
  }

  printf("%ld\n",n);

  return 0;
}
OUTPUT:
ENTER AN INTEGER
97
101

C program to shutdown or turn off computer




DESCRIPTION
C Program to shutdown your computer: This program turn off i.e shutdown your computer system. Firstly it will asks you to shutdown your computer if you press 'y' the your computer will shutdown in 30 seconds, system function of "stdlib.h" is used to run an executable file shutdown.exe which is present in C:\WINDOWS\system32 in Windows XP. You can use various options while executing shutdown.exe for example -s option shutdown the computer after 30 seconds, if you wish to shutdown immediately then you can write "shutdown -s -t 0" as an argument to system function. If you wish to restart your computer then you can write "shutdown -r".

If you are using Turbo C Compiler then execute your file from folder. Press F9 to build your executable file from source program. When you run from within the compiler by pressing Ctrl+F9 it may not work.
C programming code for Windows XP
#include <stdio.h>
#include <stdlib.h>

main()
{
   char ch;

   printf("Do you want to shutdown your computer now (y/n)\n");
   scanf("%c",&ch);

   if (ch == 'y' || ch == 'Y')
      system("C:\\WINDOWS\\System32\\shutdown -s");

   return 0;
}
C programming code for Windows 7
#include <stdio.h>
#include <stdlib.h>

main()
{
   char ch;

   printf("Do you want to shutdown your computer now (y/n)\n");
   scanf("%c",&ch);

   if (ch == 'y' || ch == 'Y')
      system("C:\\WINDOWS\\System32\\shutdown /s");

   return 0;
}
To shutdown immediately use "C:\\WINDOWS\\System32\\ shutdown /s /t 0". To restart use /r instead of /s.

C programming code for Ubuntu Linux
#include <stdio.h>

int main() {
  system("shutdown -P now");
  return 0;
}
You need to be logged in as root user for above program to execute otherwise you will get the message shutdown: Need to be root, now specifies that you want to shutdown immediately. '-P' option specifes you want to power off your machine. You can specify minutes as:
shutdown -P "number of minutes"
For more help or options type at terminal: man shutdown

c program to get ip address



DESCRIPTION
This c program prints ip (internet protocol) address of your computer, system function is used to execute the command ipconfig which prints ip address, subnet mask and default gateway. The code given below works for Windows xp and Windows 7. If you are using turbo c compiler then execute program from folder, it may not work when you are working in compiler and press Ctrl+F9 to run your program.
C programming code
#include<stdlib.h>

main()
{
   system("C:\\Windows\\System32\\ipconfig");
   system("pause");

   return 0;
}
IP address program executable.
OUTPUT
WINDOWS IP CONFIGURATION
ETHERNET ADAPTER LOCAL AREA CONNECTION:
CONNECTION-SPECIFIC DNS SUFFIC.:
IP ADDRESS...........27.107.241.183
SUBNET MASK..........255.255.255.255
DEFAULT GATEWAY.........27.107.241.183

c program to print datE



DESCRIPTON
This c program prints current system date. To print date we will use getdate function.
PROGRAM
C programming code
#include<stdio.h>
#include<conio.h>
#include<dos.h>
 main()
{
   struct date d;

   getdate(&d);

   printf("Current system date is %d/%d/%d",d.da_day,d.da_mon,d.da_year);
   getch();
   return 0;
}
OUTPUT;
CURRENT SYSTEM DATE IS 16/09/2012

c program to generate random numbers


DESCRIPTION:
c program generates random numbers using random function, randomize function is used to initialize random number generator. If you don't use randomize function then you will get same random numbers each time you run the program.
PROGRAM
C programming code using rand
#include <stdio.h>
#include <stdlib.h>
 int main() {
  int c, n;
  printf("Ten random numbers in [1,100]\n");
 for (c = 1; c <= 10; c++) {
    n = rand()%100 + 1;
    printf("%d\n", n);
  }

Write a function to find the area of a triangle whose length of three sides is given.



Area=sqrt(y(y-a)*(y-b)*(y-c)
y=(a+b+c)/2
PROGRAM.
 /*Program to area of triangle using Heron's Formula*/
 #include<stdio.h>
 #include<conio.h>
 #include<math.h>
 int main()
 {
  int x=10,y=8,z=7;
  float s=0.0;
  double area=0;
  s=(x+y+z)/2.0;
  area=(s*(s-x)*(s-y)*(s-z));
  printf("\nArea of triangle = %lf",area);
  getch();
  return 0;
 }

OUTPUT


Area of triangle = 773.43750075

Write a program to calculate power of particular number?


Example: 4=625
              5
PROGRAM
/* c program for calculator power of number*/
#include<stdio.h>
#include<conio.h>
int main()
{
 int num,pow,res=1;
 printf("\nEnter number and power : ");
 scanf("%d %d",&num,&pow);
 while(pow>=1)
 {
  res=res*num;
  pow--;
 }
 printf("%d",res);
 return 0;
}

 OUTPUT
Enter number and power : 5
4
625

Write an interactive program to calculate compound interest.


PROGRRAM;/*c program for calculate compound interest*/
#include<stdio.h>
#include<math.h>
#include<conio.h>
int main()
{
 float p,rate,time,ci;
 printf("Enter principal amount : ");
 scanf("%f", &p);
 printf("Enter interest rate : ");
 scanf("%f", &rate);
 printf("Enter time period in year : ");
 scanf("%f", &time);
 //calculate ci
 ci=p*(pow((1+rate/100),time)-1);
 printf("\nCompound interest = %f",ci);
 return 0;
}
OUTPUT
Enter principal amount : 48500
Enter interest rate : 6
Enter time period in year : 2
Compound interest = 5994.600098

Find students grades through structure


Before writing program we make some assumption as following :

Maximum total marks is 500.    
  Student_percentage         Grades
     >=80                        A
     >=60                        B
     >=50                        C
     >=40                        D
     <40                         F
 
/*C program to find students grades in a class through structure */
#include<stdio.h>
#include<conio.h>
struct stud
{
  char nam[20];
  int obtain_mark;
  int per;
  char grad[5];
};
struct stud s[5];
int i;
int main()
{
 for(i=1; i<=5; i++)
 {
  printf("Enter %d student name : ",i);
  scanf("%s",&s[i].nam);
  printf("Enter %d student obtained marks = ",i);
  scanf("%d",&s[i].obtain_mark);
  fflush(stdin);
 }
 for(i=1; i<=5; i++)
   s[i].per=s[i].obtain_mark/5;
 for(i=1; i<=5; i++)
 {
  if(s[i].per>=80)
    strcpy(s[i].grad,"A");
  else if(s[i].per>=60)
    strcpy(s[i].grad,"B");
  else if(s[i].per>=50)
    strcpy(s[i].grad,"C");
  else if(s[i].per>=40)
    strcpy(s[i].grad,"D");
  else
    strcpy(s[i].grad,"F");
 }

search prime number


Definition of prime number:A prime number is one,which is divisible only by one or itself. And its must greater than 1.
And if number is not prime, it is called composite number and vice versa.

Ans.

#include<stdio.h>
#include<stdio.h>
int main()
{
 int x,num;
 printf("Enter number : ");
 scanf("%d",&num);
 x=2;
 while(x<=num-1)
 {
   if(num%x==0)
   {
      printf("Number is not prime!!");
      break;
   }
   x++;
 }
 if(x==num)
    printf("Number is prime!!");
 getch();
 return 0;
}

    output of above program :

Enter number : 7
Number is prime!!

RELATED PROGRAMS
case1:
Generate first n Prime number
/*c program for generate first n prime number*/
#include<stdio.h>
#include<conio.h>
int main()
{
 int n,num,t,div,count;
 printf("How many prime number you want to print: ");
 scanf("%d", &n);
 printf("\n%d\t",2); /*2 is first prime number*/
 count=1;
 num=3;
 while(count<n)
 {
  t=sqrt(num);
  div=2;
  while(div<=t)
  {
    if(num%div==0)
       break;
    div++;
  }
  if(div>t)
  {
     printf("%d\t",num);
     count++;
  }
  num=num+2;
 }
 getch();
 return 0;
}
case2:
Print prime number 1 to 100
Q. Write a C program to accept a specific last number and print all the less then or equal prime number.
or
Q. Write a C program to print the prime number between 1 to n number.
for example: 1 to 10
Note:-
1. 1 is not the prime number.
2. All even number is not the prime number except the number 2.

/*C program to print 1 to 100 prime numbers*/
#include<stdio.h>
#include<conio.h>
int main()
{
 int num,n,div,p;
 printf("Enter any number: ");
 scanf("%d", &num);
 for(n=2; n<=num; n++)
 {
  for(div=2; div<n; div++)
  {
   if(n%div==0)
   {
     p=0;
     break;
   }
   p=1;
  }
  if(p)
    printf("\t%d",n);
 }
 getch();
 return 0;
}

Calculate sales tax


Your program should guide the user to accept price of item and result with adding  sales tax.
/*C program for calculate sales tax*/
#include<stdio.h>
#include<conio.h>
#define S_TAX 0.5
int main()
{
 float price,tot_cost;
 printf("Enter price of item : ");
 scanf("%f", &price);
 tot_cost=price+(price*S_TAX);
 printf("Total cost(with Sales Tax)= %f",tot_cost);
 getch();
 return 0;
}

Display string vertically


For example: Entered String is : Sample String
After programmed, resulted string is :
S
a
m
p
l
e


S
t
r
i
n
g


Ans.


/*c program for display a string in vertical*/
#include<stdio.h>
#include<conio.h>
int main()
{
 int i;
 char str[30];
 printf("Enter any string : ");
 gets(str);
 for(i=0; str[i]!='\0'; i++)
   printf("%c\n",str[i]);
 getch();
 return 0;
}

Print Even position character


*c program for display character only that are stored in even position*/
#include<stdio.h>
#include<conio.h>
int main()
{
 int i;
 char str[30];
 printf("Enter any string: ");
 gets(str);
 for(i=0; str[i]!='\0'; i++)
 {
   if(i%2==0)
      printf("%c",str[i]);
 }
 getch();
 return 0;
}

Saturday 15 September 2012

Write a c program for selectioon sort


 Description:
                       This is the simplest method of sorting. In this method, to sort the data in ascending order, the 0th element is compared with all other eements. If the 0th element is found to be greater than the compared element then they are interchanged.

        Algorithm:
1)      Start
2)      Initiliaze the variables I,j,temp and arr[]
3)      Read the loop and check the condition. If the condition is true print the array elements and increment the I value. Else goto step 4
4)      Read the loop and check the condition. If the condition true then goto next loop.
5)      Read the loop and check the condition. If the condition true then goto if condition
6)      If the condition if(arr[i]>arr[j]) is true then do the following steps
i)                    temp=arr[i]
ii)                  arr[i]=arr[j]
iii)                arr[j]=temp
7)      increment the j value
8)      perform the loop operation for the displaying the sorted  
elements.
9)      print the sorted elements
10)  stop

 Program:
#incude<stdio.h>
#incude<conio.h>
Void main()
{
       Int arr[5]={25,17,31,13,2};
Int I,j,temp;
Clrscr();
Printf(“selection sort\n”);
Printf(“\n array before sorting:\n”);
For(i=0;i<=3;i++)
Printf(“%d\t,arr[i]”);
For(i=0;i<=3;i++)
{
      For(j=j+1;j<=4;j++)
{
             If(arr[i]>arr[j])
                {
                      Temp=arr[i];
                      Arr[i]=arr[j];
                     Arr[j]=temp;
               }
        }
}
Printf(“\n\n array after sortong:\n”);
For(i=0;i<=4;i++)
Printf(“%d\t”,arr[i]);
Getch();
}
Sampe input & output:
1)    Section sort
Array before sorting:
25   17  31        13     2
Array after sorting:
2    13    17    25    31
2)   section sort
Array before sort
25   31  30 12   1
Array after sort
1   12     25  30   31
Concusion: this program is error free
VIVA QUESATIONS
1)  The complexity of the section  sort algorithm ?
Ans:  O(n2)
2) 1) Drawback of the binary tree ?
Ans: Additional space is required for building the tree
3) The complexity of the heap sort algorithm ?
Ans: O(n og n)

Write a c program for heap sort


 Description:
                        In this method, a tree structure caed heap is used. A heap is type of a binary tree. An ordered baanced binary tree is caed a min-heap where the vaue at the roo of any sub tree is ess than or equa to the vaue of either of its chidern. Heap sort is basically an improvement over the binary tree sort.

        Algorithm:

              Heap sort
           SWAP FUNCTION

1. start

2. assign *a to temp

3. assign *b to *a

4. assign  temp to *b

5. stop


           HEAP SORT

1. start

2. assign n to i and a[n] to item

3. if i > 1 and a[i/2]< item repeat through step 4 other wise goto  
     step 5
            begin

4.          assign a[i/2] to a[i] and i/2 to i
            end if

5. assign item to a[i]

6. stop

  Program:
#include<stdio.h>
int a[20];
main()
{
int n,i;
clrscr();
printf("Enter number of elements: ");
scanf("%d",&n);
printf("Enter %d elements: ",n);
for(i=1;i<=n;i++)
scanf("%d",&a[i]);
heapsort(n);
printf("Sorted elements are: \n");
for(i=1;i<=n;i++)
printf("%3d",a[i]);
getch();
}
heapsort(int n)
{
int t;
while(n>1)
{
         maxheap(n);
         t=a[1];
         a[1]=a[n];
         a[n]=t;
         n=n-1;
}
}

maxheap(int n)
{
int i,t,j;
for(i=2;i<=n;i++)
{
         t=a[i];
         j=i;
         while(a[j/2]<t&&j>1)
         {
         a[j]=a[j/2];
         j=j/2;
         }
         a[j]=t;
}
}

Input/Output:

Enter number of elements: 4
Enter 4 elements: 23
4
12
8
Sorted elements are:
  4  8 12 23


Enter number of elements: 6
Enter 6 elements: 67
23
6
45
99
78
Sorted elements are:
  6 23 45 67 78 99
Conclusion: 
    
                         The program is error free

VIVA QUESATIONS
1) Drawback of the binary tree ?
Ans: Additional space is required for building the tree

2) The complexity of the heap sort algorithm ?
Ans: O(n og n)


Program to Implement Traezodial and simpson methods.


Traezodial method:

Algorithm:

Step 1. Read x1, x2, e { x1 and x2 are the two end points of  the 
            internal the allowed error in integral is e}

Step 2. h=x2-x1

Step 3. SI = (f(x1) + f(x2))/2;

Step 4. I = h-si

Step 5. i=1 Repeat

Step 6. x=x1 + h/2

Step 7. for J= 1 to I do

Step 8. SI= SI + f(x)

Step 9. x=x+h
Endfor

Step 10. i=21

Step 11. h=h/2 { Note that the internal has been halved above and 
             the number of points where the function has to be computed     
             is doubled}

Step 12.i0=i1

Step 13. i1 = h.si

Step 14. until / I1-i0 / <=c./i1/

Step 15. Write I1,h,i

Step 16. Stop

 Program:


#include<stdio.h>
#include<math.h>
main()
{
float h,a,b,n,x[20],y[20],sum=0,integral;
int i;
clrscr();
printf("enter the value ofa,b,n:");
scanf("%f %f %f",&a,&b,&n);
printf("enter the values of x:");
for(i=0;i<=(n-1);i++)
{
scanf("%f",&x[i]);
}
printf("\n enter the values of y:");
for(i=0;i<=(n-1);i++)
{
scanf("%f",&y[i]);
}
h=(b-a)/n;
x[0]=a;
for(i=1;i<=n-1;i++)
{
x[i]=x[i-1]+h;
sum=sum+2*y[i];
}
sum=sum+y[b];
integral=sum*(h/2);
printf("approximate integral value is: %f",integral);
getch();
}












Input/Output:

Enter the values of a,b,n
1
2
3
Enter the values of x:
1
2
3
Enter the values of y:
1
2
3
Approximate integral value is 2.166667
Conclusion: The program is error free






























           Simpsons Method:

   Algorithm:

Step 1. Read x1,x2,e

Step 2. h=(x2-x1)/2

Step 3. i=2

Step 4. si=f(x1) + f(x2)

Step 5. s2=0

Step 6. s4=f(x1+h)

Step 7. I0=0

Step 8.  In =(s+4s4). (h/3)
             Repeat

Step 9. s2=s2+s4 {s2 stores already computed functional value and s4 the value computed in   the new    nitration }

Step 10. s4=0

Step 11. x=x1+h/2

Step 12. for j=1 to I do

Step 13. s4=s4+f(x)

Step 14. x=x+h

Step 15. h=h/2

Step 16. i=2i

Step 17. io=in

Step 18. in= (s1+2s2+4s4) . (h/3)
Step 19. until |In-Io|≤e. /in

Step 20. Write In,h,i

Step 21. STOP

                Program:

#include<stdio.h>
#include<conio.h>
#include<math.h>
main()
{
float h,a,b,n,x[20],y[20],sum=0,itgl;
int i;
clrscr();
printf("enter the values of a,b,n");
scanf("%f%f%f",&a,&b,&n);
printf("enter the values of x");
for(i=0;i<=n;i++)
{
scanf("%f",&x[i]);
}
printf("\n enter the values of y");
for(i=0;i<=n;i++)
{
scanf("%f",&y[i]);
}
h=(b-a)/n;
a=x[0];
b=x[n];
for(i=0;i<=(n-2);i++)
{
x[i]=x[i]+h;
if(i%2==0)
{
sum=sum+4*y[i];
}
else
{
sum=sum+2*y[i];
}
}
itgl=sum*(h/3);
printf("integral value%f",itgl);
getch();
}





Input/Output:

Enter the values of a,b,n
1
2
3
Enter the value of x
4
5
6
7
Enter the values of y
8
9
1
2
Integral value is 5.555556


Conclusion: The program is error free

VIVA QUESATIONS
1)  Define Binary search ?
Ans: Binary search is a vast improvement over the sequential search. For binary search to work, the item in the list  must be in assorted order. The approach employed in the binary search is divid and conquer. If the list to be sorted for a specific item is not sorted, binary search fails.