Write a C program to display a triangle of digits as follows. Number of rows is entered by keyboard....



          1

               2  2

             3  3  3

           4  4  4  4

         5  5  5  5  5

             6  6  6  6  6  6


#include<stdio.h>
 int main()
   {
     int  i, j, sp, n;
     printf( "How many line: " );

Write a C program to display a triangle of digits as follows. Number of rows is entered by keyboard

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


#include<stdio.h>
int main()
  {
     int   i,  j,  n;
     printf("Enter the number of line: ");

Any year is input through the keyboard. Write a program to determine whether the year is a leap year or not.

#include<stdio.h>
int main()
   {
     Int   Y;
     printf( "Enter a year: " );
     scanf( "%d" ,&Y);
     if(Y%4==0 && Y%100!=0)
        printf( "\nLeap Year" );
     else if(Y%400==0)
        printf( "\nLeap Year" );
     else
         printf( "\nNot Leap Year" );
     return  0;
    }

If cost price and selling price of an item is input through the keyboard, write a program to determine whether the seller has made profit or incurred loss. Also determine how much profit he made or loss he incurred.

#include<stdio.h>
 int main()
    {
      float  C, S;
      printf("Enter cost price of the product: ");
      scanf( "%f" ,&C);
      printf("\nEnter selling price of thr products: ");
      scanf("%f",&S);
      if(S>C)
         printf("\nSeller has made profit.\nProfit= %.3f",S-C);
      else
          printf("\nSeller has incurred loss.\nLose= %.3f",C-S);
      return  0;
    }

Write a program to calculate following series 1 + 1/2 + 1/3 + 1/4 + 1/5 +…………….1/n for any number n

#include<stdio.h>
int main()
 {
   int  n, k;
   float  sum=0;
   printf("Enter the value of n: ");
   scanf("%d",&n);
   for(k=1;k<=n;k++)
      sum= sum + (float)1/k;
   printf("\nSum= %f",sum);
   return 0;
   }

Write a program to convert seconds to hours minutes and seconds

#include<stdio.h>
int main()
   {
     int S, h, m, s;
     printf( "Second: " );
     scanf( "%d" , &S);
     h=S/3600;
     S=S%3600;
     m=S/60;
     s=S%60;
     printf("\nhh:mm:ss\n%d:%d:%d", h, m, s);
     return  0;
   }

Write a c program to calculate the distance between two points using formula √ (( y2− y1)² +( x2− x1)²)

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

int main ()
     {
        int  x1, x2, y1, y2;
        float  r;
        printf( "Enter Point (x1,y1): " );
        scanf( "%d" ,&x1);
        scanf( "%d" ,&y1);
        printf( "\nEnter Point (x2,y2): " );
        scanf( "%d%d" ,&x2,&y2);
        r = sqrt(pow((y2-y1),2) + pow((x2-x1),2));
        printf( "\nDistance= %f" ,r);
        return  0;
     }

Write a program to evaluate the expression X4+X3+10X for any number X

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

int main()
   {
     float  x, N;
     printf( "Enter a value for X: " );
     scanf( "%f" ,&x);
     N=pow(x,4) + pow(x,3) + 10*x;
     printf( "%.2f" , N);
     return  0;
     }

Write a c program to convert the given temperature Fahrenheit to Celsius using the following formula C = (F – 32)/1.8 and display the values in tabular form

#include<stdio.h>
  int main()
    {
      float  c, f;
      printf( "F= " );
      scanf( "%f" ,&f);
      c=(f-32)/1.8;
      printf( "C= %f" ,c);
      return 0;
     }

Write a program that will read a string from the user and print the number of alphabet on that string.

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

int main()
   {
      char  s[100];
      printf( "Enter your string: " );
      gets(s);
      printf( "\nThe number of alphabet in the string is : %d" , strlen(s));
      return 0;
    }

Write a c program that will accept n numbers from user and find their sum and average

#include<stdio.h>
int main()
  {
    int   n, i, sum=0, ava=0;
    printf( "Enter a positive integer number: " );
    scanf( "%d" , &n);
    for(i=1;i<=n;i++)
   sum= sum+i;
    ava=sum/(i-1);
    printf( "\nSum is: %d\nAverage is: %d" , sum, ava);
    return 0;
    }

Write a program to sum of digits of a given number. e.g. If the input is 158 then output will be 14.

#include<stdio.h>
int main()
 {
  int   n, a, sum=0;
  printf( "Enter a integer number: " );
  scanf( "%d" , &n);
  while(n%10!=0)
         {
       a=n%10;
       n=n/10;
       sum=sum+a;
        }
  printf( "\nSum of the digit: %d" , sum);
      return 0;
}

Write a program to reverse a number. e.g.: if the is 876 then output will be 678

#include<stdio.h>

int main()

   {

      Int   n, r, temp;

      printf( "Enter an integer number: " );

      scanf( "%d" , &n);

      temp=n;

      r=0;

      while(n>0)

        {

          r=r*10 + n%10;

          n=n/10;

         }

      printf( "\nReverse of %d = %d" , temp, r);

      return 0;

 }

Write a program to convert number into word. e.g. If input is 953 then output will be Nine Five Three

#include<stdio.h>

#include<math.h>

 

int main()

  {

      Int  a, n, r, k, temp;

      printf( "Enter an integer number: " );

      scanf( "%d" ,&n);

      temp=n;

      r=0;

      while(n>0)

          {

             r=r*10 + n%10;

             n=n/10;

           }

 

      while(temp>0)

           {

              a=r%10;

              r=r/10;

             temp=temp/10;

              switch (a)

                {

                    case 1:

                          printf( "One " );

                          break;

                    case 2:

                           printf( "Tow " );

                           break;

                    case 3:

                          printf( "Three " );

                          break;

                    case 4:

                           printf( "Four " );

                           break;

                    case 5:

                          printf( "Five " );

                          break;

                    case 6:

                           printf( "Six " );

                           break;

                    case 7:

                          printf( "Seven " );

                          break;

                    case 8:

                           printf( "Eight " );

                           break;

                    case 9:

                          printf( "Nine " );

                          break;

                    default :

                          printf( "Zero " );

                  }

           }

      return 0;

  }

Any number is input through the keyboard. Write a program to determine whether the number is a prime or not.

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

int main()
   {
   int   i, n, isprime;
   printf( "Enter a positive integer number: " );
   scanf( "%d" ,&n);
   if(n<2)
        isprime=0;

   else
       for(i=2;i<sqrt(n);i++)
           {
              isprime=1;

              if(n%i==0)
                 {
                     isprime=0;
                     break;
                 }
        }
    if(isprime==1)
        printf( "\nPRIME NUMBER" );
   
else 
        printf( "\nNOT PRIME NUMBER" );
    return 0;
   }

Write a C program to display a prime number in between 1 and 100.

#include<stdio.h>

#include<math.h>

 

int main()

   {

     int   i, j, isprime;

     for(i=2;i<=100;i++)

         {

           isprime=1;

           for(j=2;j<=sqrt(i);j++)

                 if(i%j==0)

                 {

                     isprime=0;

                     break ;

                 }

            if(isprime==1)

                 printf( "%8d" ,i);

           }

       return  0;

    }

Some Basic Conceptions of C programming Languge

Data Types:
There are five data types in C: character, integer, floating point, double floating point and valueless.
The sizes of these types are shown in below,
Type                                 Bit Width                                 Range
char                                        8                                      0     to     255
int                                          16                                    -32768    to    32767
float                                      32                                      3.4E-38   to    3.4E+38
double                                  64                                      1.7E-308   to   1.7E+308
void                                       0                                         valueless

To work with characters we need character type variable. So we have to declare a “char” type variable. Same case happen to work with integer type value such as 1, 8,-1,-8, 100, 587 etc. float and double are used to hold real numbers. Real numbers have both an integer and a frictional component.
Void has three uses. The first is use to declare explicitly a function as no returning no value;  the second is to declare explicitly a function as having no parameters; the third is to create generic pointers.


Commonly Used Conversion Characters for Data Input/Output:
Character                                                                         Meaning
c                                                               data item is a single character     
d                                                              data item is a decimal integer
f                                                               data item is a floating point value

s                                                              data item is a string followed by a whitespace
                                                                character (the null character \0 will automatically
                                                                be added at the end)          




Type Modifiers:
The basic data type (excepting type void) may have various modifiers preceding them. The modifier  is used to alter the meaning of the base type in various needs. The list of modifiers :
signed
unsigned
long
short

Declaration of variable:
When you are going to write a program, you should declare all variables before you use them. General form of declaration is
Data type     variable name;
Example:
                      int    I,   j,   k ;
                      char    s,  ch ;
                      double      balance,  profit ;
                      unsigned int    i ;


There are three basic places where variable will be declared: inside function, outside of all functions, or in the definition of the function parameters. These variables are called local variables, global variables, and formal parameters.

Compiler:
To compile your program you will need compiler. There are many c compiler, so you can chose any of them. But at the beginning Tc or code::blocks  will be useful. So I will tell you to collect one of them.
I have used code::blocks to describe following program in this blog. If you use Tc you can use  #inlude<conio.h>              clrscr();                       getch();
If you don’t use getch (), you will need to press ctrl+F5 to run your program otherwise ctrl+F9. To check error press F9. Remember: before you run your program save it in a hard drive to avoid loss you written code for any confliction during run. If you want to use getch(), write it before last curls-bracket  in the main function. You can use void in main function, if you do it, no need to return value to main function. But try to avoid it’s use. If you don’t use any data type before main, it will act as a int. So function will return a value. Okay you can understand all that I discussed when you do some practice.


Input and Output Library Function:           
Input                               Header File                      
scanf                                  stdio.h
fscanf                                 stdio.h
cscanf                                conio.h
sscanf                                 stdio.h
gets                                     string.h


Output                               Header File                       
printf                                  stdio.h
fprintf                               stdio.h
cprintf                                conio.h
sprintf                                 stdio.h
puts                                     string.h

There are more input output library function. We will work with some of them. At the beginning we will take input from standard I/O devices using scanf and write output using printf function.



Some Examples:
/*************** printf function ***************/
#include<stdio.h>

  int main( )
    {
      printf("C Programming Language");
      return 0;
         }



 /************** scanf function ***************/

#include<stdio.h>
  int main( )
     {
       int   x;
       printf("Enter a value: ");
       scanf("%d", &x);                                     // %d will take input a decimal value from I/O devices
       printf("\nGiven value is: %d", x);             // here %d will write a decimal value from x
       return 0;
     }
 




/*************** Variable within constant value *****************/

#include<stdio.h>
  int main( )
   {
     int a=100;
     printf("%d", a);
     return 0;
     }





/*************** Single character input ****************/

#include<stdio.h>

int main()
     {
         
char ch;
         printf(
"Enter a character: ");
         scanf(
"%c",&ch);     
         printf("\nGiven character is: %c",ch);
         
return 0;
        }