C PROGRAMS
C introduction programs
C Program to Print a Sentence
/* C Program to print a sentence. */
//************************************************
#include
< stdio.h >
#include
< conio.h >
void main()
{
clrscr ( ) ;
{
clrscr ( ) ;
printf("C Programming");
getch();
}
getch();
}
C Program to Print a Integer Entered by a User
/******************************************************/
//*****************************************************
#include < stdio.h >
#include < conio.h >
void main ( )
{
int num;
clrscr ( ) ;
printf ( "enter a number\n" );
scanf ( " %d ", &num);
printf ( " you entered %d ", num);
getch ( ) ;
}
c program to add two integers enterd by user
/***********************************************************/
//**********************************************************
#include < stdio.h >
#include < conio.h >
void main ( )
{
int a,b,sum;
clrscr( ) ;
printf ( "enter two numbers\n" );
scanf ( "%d%d",&a,&b);
sum=a+b ;
printf ( "sum : %d",sum);
getch ( ) ;
}
c program to multiply two floating point numbers
/****************************************************************************/
//**********************************************************************
#include < stdio.h >
#include < conio.h >
void main ()
{
float a , b ,product ;
clrscr ( ) ;
printf (" enter two numbers\n ");
scanf (" %f %f ",&a,&b);
product=a*b;
printf (" product : % . 2f ",product);
getch ( ) ;
}
c program to find ASCII value of a character
/* Source code to find ASCII value of a character entered by user */
//*****************************************************************
#include < stdio.h >
#include < conio.h >
void main ( )
{
char c ;
clrscr ( ) ;
printf ( " Enter a character: " ) ;
scanf ( " % c ", &c ) ;
printf ( " ASCII value of character is = %d ", c ) ;
getch ();
}
C Program to Find Quotient and Remainder of Two Integers Entered by User
/* C Program to compute remainder and quotient */
# include < stdio.h >
# include < conio.h >
void main()
{
int a, b, quotient, remainder ;
clrscr ( ) ;
printf ( " Enter a value \ n " ) ;
scanf ( " %d ", &a) ;
printf ( " Enter b value \ n " ) ;
scanf ( " %d ", &b) ;
quotient = a / b ; /* Computes quotient */
remainder = a % b ; /* Computes remainder */
printf ( " Quotient = %d \n ", quotient ) ;
printf ( " Remainder = %d ", remainder ) ;
getch ( ) ;
}
C Program to Find Size of int, float, double and char of Your System
/* This program computes the size of variable using sizeof operator.*/
//*********************************************************
#include
#include
void main()
{
int a;
float b;
double c;
char d;
clrscr();
printf("Size of int: %d bytes\n",sizeof(a));
printf("Size of float: %d bytes\n",sizeof(b));
printf("Size of double: %d bytes\n",sizeof(c));
printf("Size of char: %d byte\n",sizeof(d));
getch();
}
C Program to Demonstrate the Working of Keyword long
/**********************************************/
//*********************************************
#include
#include
void main()
{
int a;
long int b; /* int is optional. */
long long int c; /* int is optional. */
clrscr();
printf("Size of int = %d bytes\n",sizeof(a));
printf("Size of long int = %ld bytes\n",sizeof(b));
printf("Size of long long int = %ld bytes",sizeof(c));
getch();
}
C Program to Swap Two Numbers
Using helping variable method
/************************************************/
//***********************************************
#include < stdio.h >
#include < conio.h >
void main()
{
float a, b, c;
clrscr();
printf("Enter value of a: \n");
scanf("%f",&a);
printf("Enter value of b: \n");
scanf("%f",&b);
c = a; /* Value of a is stored in variable c */
a = b; /* Value of b is stored in variable a */
b = c; /* Value of c(which contains initial value of a) is stored in variable b*/
printf("After swapping, value of a = %.2f\n", a);
printf("After swapping, value of b = %.2f", b);
getch();
}
With out using helping variable method
/************************************************/
//***********************************************
#include < stdio.h >
#include < conio.h >
void main()
{
float a, b ;
clrscr();
printf("Enter value of a: \n");
scanf("%f",&a);
printf("Enter value of b: \n");
scanf("%f",&b);
a = a + b ;
b = a - b ;
a = a - b ;
printf("After swapping, value of a = %.2f\n", a);
printf("After swapping, value of b = %.2f", b);
getch();
}
Convert Fahrenheit to Celsius
/************************************************/
//***********************************************
#include < stdio.h >
#include < conio.h >
void main()
{
float f, c ;
clrscr();
printf("Enter Fahrenheit \n");
scanf("%f",&f);
c = ( f-32) * 5/9 ;
printf(" celsius is % f ", c ) ;
getch();
}