Basic C Programs


Hi.
This is a basic programs for C language. These programs are wanted in the interview questions. They are as follows:

                                                           /* floyd’s triangle*/
                                            
                                             1.   /* Print Floyd's Triangle of size n */
                                               void floyd(int n)
                                                 {
                                             /* Print the triangle */
                                               for (i = 0; i < n; i++) {
                                               for (j = 0; j < i; j++) {
                                            /* Print a single number */
                                                  }
                                           /* Go to the next line */
                                                  }
                                                  }


                                         2.  /* Print Floyd's Triangle of size n */
                                             void floyd(int n)
                                                {
                                                   int i, j;
                                              int k = 1; /* Numbers start at 1 in our triangle */
                                             /* Print the triangle */
                                              for (i = 0; i < n; i++) {
                                              for (j = 0; j < i; j++) {
                                            /* Print a single number */
                                           /* Maybe needs more formatting */
                                              printf("%d ", k++);
                                              }
                                           /* Go to the next line */
                                          printf("\n");
                                                 }
                                                 }
         
                                    #include <stdio.h>
                                  /* Paste the floyd function here */
                                    int main(void)
                                      {
                                   floyd(5);
                                   return 0;
                                      }

                             A test of the output shows that something isn't quite right:
CODE
1
2 3
4 5 6
7 8 9 10
                                          /*Pascal Triangle*/

                                     int loop (int i, int j)
                                     {      
                                     int i, j;  
                                for (i = 0; i<=7; ++i) //7 will be the test value to stop loop  
                                     {                        
                               for (j = 0; j<=i; ++j)             
                                     {         
                               printf ("%3d%3d", i, j);
                                if (i==j)
                                 {
                               printf("\n");
                                     }
                                       }               
                                         }
                                           }                                                                                                                                                                                                                                                                     
                               int factorial (int n)
                                   {
                              if (n==0) return 1;
                              else if (n*factorial(n-1));
                              else return n;
                                   }
                              int combo (int n, int r)
                                   {
                              return (factorial (n))/(factorial (r) * (factorial (n-r)));
                                    }

                                        /*armstorng*/

Write a program to print out all Armstrong numbers between 1 and 500.
If sum of cubes of each digit of the number is equal to the number itself,
then the number is called an Armstrong number.
For example,
153 = (1*1*1) + (5*5*5) + (3*3*3)

#include<stdio.h>
main()
{
int number, temp, digit1, digit2, digit3;

printf("Printing all Armstrong numbers between 1 and 500:\n\n");

number = 001;

while (number <= 500)
{
digit1 = number - ((number / 10) * 10);

digit2 = (number / 10) - ((number / 100) * 10);

digit3 = (number / 100) - ((number / 1000) * 10);

temp = (digit1*digit1*digit1) + (digit2*digit2*digit2) + (digit3*digit3*digit3);

if (temp == number)
{
printf("\nAmstrong Number:%d", temp);
}
number++;
}
}
 /*Another Armstorng */
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int a, b, c, num, i=1;
Printf("Printing all the armstrong numbers");
while(i<=500)
{
a= i/100;
a= a*a*a;
num= i%100;
b= num/10;
b= b*b*b;
c= num%10;
c=c*c*c;
if(i==a+b+c)
{
printf("\n Armstrong number= %d", i);
}
i++;
}
getch();
}









Comments

Popular posts from this blog

TRAVELING SALESMAN USING BRANCH AND BOUND TECHNIQUE

BOOKS DETAILS USING C STRUCTURE

TRAVELING SALESMAN USING BRANCH AND BOUND TECHNIQUE