Example : Simple C Program Typical Template.
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr(); // clears the previous contents/output from the screen, if any.
-----codes------
-----codes------
-----codes------
getch(); // to holds the output on the screen until user press any key button.
}
/* ------------------------------- OR ------------------------------------*/
#include<stdio.h>
#include<conio.h>
int main()
{
clrscr(); // clears the previous output contents from the screen, if any.
-----codes------
-----codes------
-----codes------
return 0;
}
Example : Write a program in C to print a message in a different format.
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
printf("Welcome U in ur site codershelpline.com");
getch();
}
Output :
Welcome U in ur site codershelpline.com
---------- OR -----------
#include<stdio.h>
#include<conio.h>
int main()
{
clrscr();
printf("Welcome U in ur site codershelpline.com");
return 0;
}
Output :
Welcome U in ur site codershelpline.com
---------- OR -----------
#include<stdio.h>
#include<conio.h>
int main()
{
//clrscr();
printf("Welcome U in ur site \n codershelpline.com");
return 0;
}
Output :
Welcome U in ur site
codershelpline.com
---------- OR -----------
#include<stdio.h>
#include<conio.h>
int main()
{
//clrscr();
printf("Welcome U in ur site \"codershelpline.com\" ");
return 0;
}
Output :
Welcome U in ur site "codershelpline.com"
Example : Write a program in C to print a message in another different format.
#include<stdio.h>
#include<conio.h>
int main()
{
//clrscr();
printf("Welcome");
printf("New Year");
printf("2021");
return 0;
}
Output:
WelcomeNew Year2021
------------------------------ OR ----------------------------
#include<stdio.h>
#include<conio.h>
int main()
{
//clrscr();
printf("Welcome\n");
printf("New Year\n");
printf("2021");
return 0;
}
Output:
Welcome
New Year
2021
------------------------------ OR ----------------------------
#include<stdio.h>
#include<conio.h>
int main()
{
//clrscr();
printf("Welcome");
printf("\nNew Year\n");
printf("2021");
return 0;
}
Output:
Welcome
New Year
2021
------------------------------ OR ----------------------------
#include<stdio.h>
#include<conio.h>
int main()
{
//clrscr();
printf("Welcome");
printf("\nNew Year");
printf("\n2021");
return 0;
}
Output:
Welcome
New Year
2021
Example : Write a program in C to display the addition result of two numbers.
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,sum;
a=5;
b=6;
sum=a+b;
printf("%d",sum);
getch();
}
Output:
11
----------------------------- OR ------------------------------
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,sum;
a=5;
b=6;
sum=a+b;
printf("%d%d%d",a,b,sum);
getch();
}
Output:
5611
----------------------------- OR ------------------------------
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,sum;
a=5;
b=6;
sum=a+b;
printf("%d\t%d\t%d",a,b,sum);
getch();
}
Output:
5 6 11
----------------------------- OR ------------------------------
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,sum;
a=5;
b=6;
sum=a+b;
printf("%d %d %d",a,b,sum);
getch();
}
Output:
5 6 11
----------------------------- OR ------------------------------
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,sum;
a=5;
b=6;
sum=a+b;
printf("%d,%d,%d",a,b,sum);
getch();
}
Output:
5,6,11
----------------------------- OR ------------------------------
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,sum;
a=5;
b=6;
sum=a+b;
printf("%d\n%d\n%d",a,b,sum);
getch();
}
Output:
5
6
11
----------------------------- OR ------------------------------
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,sum;
a=5;
b=6;
sum=a+b;
printf("The results are = ");
printf("%d,%d,%d",a,b,sum);
getch();
}
Output:
The results are = 5,6,11
----------------------------- OR ------------------------------
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,sum;
a=5;
b=6;
sum=a+b;
printf("The addition result of %d and %d is = %d",a,b,sum);
getch();
}
Output:
The addition result of 5 and 6 is = 11
Example : Write a program in C to print an octal value of a decimal number.
#include<stdio.h>
#include<conio.h>
void main()
{
int a;
a=9;
printf("%o",a);
getch();
}
Output:
11
Example : Write a program in C to print a Hexadecimal value of a decimal number.
#include<stdio.h>
#include<conio.h>
void main()
{
int a;
a=22;
printf("%x",a);
getch();
}
Output:
16
Example : Write a program in C to print a Scientific notation of a decimal number.
#include<stdio.h>
#include<conio.h>
void main()
{
int a;
a=22;
printf("%e",a);
printf("\n%E",a);
printf("\n%g",a); (display short form of scientific notation)
getch();
}
Output:
1.086944e-322
1.086944E-322
1.08694e-322
Example : Write a program in C to print/display different formats of output.
#include<stdio.h>
#include<conio.h>
void main()
{
printf("%6d\n",7);
printf("%3d\n",7);
printf("%03d\n",7);
printf("%.2f\n",453.21546);
printf("%.2f\n",453.21246);
printf("%.3f\n",453.21546);
printf("%.5f\n",453.2);
printf("The hexadecimal value of %d is %x\n", 300,300);
printf("%04d-%04d-%d\n",05,11,2025);
printf("The interest amount is %.3f%%\n",25.32);
printf("I\'ve %d rupees\n",2500);
printf("\"The total amount is\" %d rupees",2500);
getch();
}
Output:
7
7
007
453.22
453.21
453.215
453.20000
The hexadecimal value of 300 is 12c
0005-0011-2025
The interest amount is 25.320%
I've 2500 rupees
"The total amount is" 2500 rupees
Example : Write a program in C to make a simple calculator.
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,add,multi,sub;
float div;
a=5;
b=6;
add=a+b;
multi=a*b;
sub=a-b;
div=a/b;
printf("%d%d%d%d%d%f",a,b,add,multi,sub,div);
getch();
}
Output:
561130-10.000000
/* ---------------------------------- OR --------------------------------------*/
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,add,multi,sub;
float div;
a=5;
b=6;
add=a+b;
multi=a*b;
sub=a-b;
div=a/b;
printf("%d\t%d\t%d\t%d\t%d\t%f",a,b,add,multi,sub,div);
getch();
}
Output:
5 6 11 30 -1 0.000000
/* ---------------------------------- OR --------------------------------------*/
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,add,multi,sub;
float div;
a=5;
b=6;
add=a+b;
multi=a*b;
sub=a-b;
div=a/b;
printf("%d %d %d %d %d %f",a,b,add,multi,sub,div);
getch();
}
Output:
5 6 11 30 -1 0.000000
Example : Write a program in C to calculate the area of a Circle & print them.
#include<stdio.h>
#include<conio.h>
void main()
{
float area,radius;
clrscr();
printf("Enter the value for radius: ");
scanf("%f",&radius);
area=3.14*radius*radius;
printf("The Area of Circle is = %f",area);
getch();
}
// NB: Formula of Area of Circle is = 3.14*r*r.
Example : Write a program in C to calculate the radius of a Circle & Print them.
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int area;
float r;
printf("Enter area of circle:-");
scanf("%d",&area);
r=sqrt(area/3.14);
printf("%f",r);
getch();
}
Example : Write a program in C to calculate the area of a Rectangle & Print them.
#include<stdio.h>
#include<conio.h>
void main()
{
int width,height;
float area;
clrscr();
printf("Enter the value of height: ");
scanf("%d",&height);
printf("Enter the value of width: ");
scanf("%d",&width);
area= height*width;
printf("Area of Rectangle is = %f",area);
getch();
}
/* NB: Formula of Area of Rectangle is = height * width */
Example : Write a program in C to calculate the area of the Cylinder & Print them.
#include<stdio.h>
#include<conio.h>
void main()
{
int radius,height;
float area;
clrscr();
printf("Enter the value of radius: ");
scanf("%d",&radius);
printf("Enter the value of height:");
scanf("%d",&height);
area= 2*3.14*radius*height;
printf("The Area of Cylinder is = %f",area);
getch();
}
/* NB: Formula of Area of Cylinder is =2*3.14*r*h */
Example : Write a program in C to calculate the area of the Triangle & print them.
#include<stdio.h>
#include<conio.h>
void main()
{
int base,height;
float area;
clrscr();
printf("Enter the value of base: ");
scanf("%d",&base);
printf("Enter the value of height: ");
scanf("%d",&height);
area= (0.5)*base*height;
printf("Area of Trinagle is = %f",area);
getch();
}
/* NB: Formula of Area of Triangle is = 0.5*base*height */
Example : Write a program in C to convert the Celsius value of temperature into Fahrenheit & print them.
#include<stdio.h>
#include<conio.h>
void main()
{
float fahrenheit,celsius;
clrscr();
printf("Enter the value of temperature in celsius: ");
scanf("%f",&celsius);
fahrenheit=1.8*(celsius+32);
printf("The temperature in Fahrenheit is =%f",fahrenheit);
getch();
}
/* NB: Formula is fahrenheit = 1.8*(celsius+32); */
Example : Write a program in C to convert the Fahrenheit value of temperature into Celsius & print them.
#include<stdio.h>
#include<conio.h>
void main()
{
float fahrenheit,celsius;
clrscr();
printf("Enter the value of temperature in fahrenheit: ");
scanf("%f",&fahrenheit);
celsius=0.56*(fahrenheit-32);
printf("The value in Celcius is = %f",celsius);
getch();
}
// NB: Formula is celsius=0.56*(fahrenheit-32);
Example : Write a program in C to Swap two values using third variables & print them.
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr();
printf("Enter the first Value: ");
scanf("%d",&a);
printf("Enter the second Value: ");
scanf("%d",&b);
printf("The output before swap is: ");
printf("%d\t%d",a,b);
c=a;
a=b;
b=c;
printf("\nThe output after swap is: ");
printf("%d\t%d",a,b);
getch();
}
Example : Write a program in c to Swap two values without using third variables & print them.
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
clrscr();
printf("Enter the first value :");
scanf("%d",&a);
printf("Enter the second value :");
scanf("%d",&b);
printf("The result before swapping is :");
printf("%d\t%d",a,b);
a=a+b;
b=a-b;
a=a-b;
printf("\nThe output after swapping is :");
printf("%d\t%d",a,b);
getch();
}
Example: Write a program in C to calculate the Simple Interest of taken values and display the output.
#include<stdio.h>
#include<conio.h>
void main()
{
int p,r,t;
float si;
printf("Enter a Principal Value:\n ");
scanf("%d",&p);
printf("Enter the Rate:\n ");
scanf("%d",&r);
printf("Enter time:\n ");
scanf("%d",&t);
si=(p*r*t)/100;
printf("The simple interest is = ");
printf("\n%f",si);
printf("\n%.2f",si);
getch();
}
Output:
Enter a Principal Value:
5000
Enter the Rate:
3
Enter time:
4
The simple interest is =
600.000000
600.00
Example : Write a program in C to print the larger values from two values using the conditional/ternary operator.
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
clrscr();
printf("Enter first value :");
scanf("%d",&a);
printf("Enter second value :");
scanf("%d",&b);
(a>b) ? printf("Frist value %d is larger",a) : printf("Second value %d is larger",b);
getch();
}
Example : Write a program in C to print the largest value from three values using the conditional/ternary operator.
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
//clrscr();
printf("Enter first value :");
scanf("%d",&a);
printf("Enter second value :");
scanf("%d",&b);
printf("Enter third value :");
scanf("%d",&c);
((a>b) && (a>c)) ? printf("First value %d is largest",a):((b>c)&&(b>a)) ? printf("Second value %d is largest",b):printf("Third value %d is largest",c);
getch();
}
Example : Write a program in C to print the year, month, and day value from given total days.
#include<stdio.h>
#include<conio.h>
void main()
{
int day,month,year;
clrscr();
printf("Enter the value for total days:");
scanf("%d",&day);
year=day/365;
day=day%365;
month=day/30;
day=day%30;
printf("\nYear = %d \nMonth = %d \nDay = %d",year,month,day);
getch();
}
Example : Write a program in C to print the square root of given values.
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int num;
float result;
printf("Enter a number: ");
scanf("%d",&num);
result=sqrt(num);
printf("%f",result);
getch();
}
Example : A C program to calculate the compound interest of given values.
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int p,r,t;
float ci;
p=1000;
r=20;
t=3;
ci=p*pow((1+r/100),t);
printf("%f",ci);
getch();
}
Example : A C program to convert lower case characters to upper case and vice-versa of given values using the C function.
#include<stdio.h>
#include<ctype.h>
int main()
{
char c1='a';
char c2='A';
printf("%c\n",tolower(c1));
printf("%c",toupper(c2));
return 0;
}
Example : A C program to show the use of Explicit Typecasting/Type conversion.
int main()
{
int num1 = 10;
float num2=15.35;
float result=num2+(float)num1;
printf("%f",result);
return 0;
}
Output:
25.350000
---------------- OR ----------------
#include <stdio.h>
int main()
{
int x = 10;
int y = 3;
float result;
result = x / y;
printf("The result without typcasting is: %f\n", result);
// Explicit Type casting 'x' and 'y' to float before division
result = (float)x / (float)y;
printf("The result after typcasting is: %f\n", result);
return 0;
}
Output:
The result without typcasting is: 3.000000
The result after typcasting is: 3.333333
Example : A C program to show the use of both Implicit and Explicit Typecasting/Type conversion.
int main()
{
float num = 3.14;
int result = (int)num; // Explicit Typeconversion
printf("%d\n",result);
int result1 = num; // Implicit Typeconversion
printf("%d\n",result1);
return 0;
}
Output:
3
3
Example : A C program to show the use of Implicit Typecasting/Type conversion.
int main()
int num1 = 10;
float num2=15.35;
float result=num2+num1;
printf("%f\n",result);
return 0;
}
Output:
25.350000
Example : A C program can be compiled without a main function.
#include<stdio.h>
#define start main
void start()
{
printf("Codershelpline.com");
}
Example : A C program to print messages without using a semi-colon.
#include<stdio.h>
void main()
{
if(printf("Codershelpline"))
{ }
}
0 Comments