Write a program in C to read a character from terminal.

In this program we use the function 'getchar' to read a character. The getchar function may be called successively to read the characters contained in a line of text.

Warning: The getchar() function accepts any character keyed in. This includes RETURN and TAB.


#inlcude <stdio.h>
main()
{
char answer;
printf("Would you like to know my name ?\n");
printf("Type Y for YES and N for NO: ");
answer = getchar(); /*...Reading a Character...*/
if(answer == 'Y' || answer == 'y')
printf("\n\n My name is Bond...,JAMES BOND \n");
else
print("\n\n You are good for nothing \n");
}

Output:

Would you like to know my name?
Type Y for YES and N for NO: Y

My name is Bond...,JAMES BOND

Would you like to know my name?
Type Y for YES and N for NO: N

You are good for nothing

Write a program to read the price of an item in decimal form and print the output in paise.

The program is to read the price of an item in decimal form like 15.95 and print the output in paise like 1595 paise.


#include<stdio.h>
main()
{
float n;
int sum;
printf("Enter the value in decimal ");
scanf("%f",&n);
sum = 100 * n;
printf(" \nThe paise is %d.", sum);
getch();
return 0;
}

Output :

Enter the value in decimal 15.95
The paise is 1595.

Write a program to demonstrate Temperature Conversion Problem

The program presented converts the given temperature in Fahrenheit to Celsius using the following conversion formula:


F - 32
C = ----------------
1.8

#include "stdio.h"
#define F_LOW 0
#define F_MAX 250
#define STEP 25
main()
{
typedef float REAL;
REAL fahrenheit, celsius;
fahrenheit = F_LOW;
printf("Fahrenheit Celsius\n\n");
while( fahrenheit &lt;= F_MAX )
{
celsius = ( fahrenheit - 32.0)/1.8;
printf("%5.1f %7.2f\n", fahrenheit, celsius);
fahrenheit = fahrenheit + STEP;
}
return 0;
}

Output:

Fahrenheit Celsius

0.0 -17.78
25.0 -3.89
50.0 10.00
75.0 23.89
100.0 37.78
125.0 51.67
150.0 65.56
175.0 79.44
200.0 93.33
225.0 107.22
250.0 121.11


Write a program to calculate Average of Numbers.

A program to calculate the average of a set of N numbers ...

#include <stdio.h>
#define N 10
main()
{
int count;
float sum, average, number;
sum =0;
count =0;
while( count &lt;&gt;
{
scanf( "%f",&number);
sum = sum + number;
count = count + 1;
}
average = sum/N ;
printf("N = %d Sum = %f", N, sum);
printf(" Average = %f", average);
return 0;
}

Output:

1
2.3
4.67
1.42
7
3.67
4.08
2.2
4.25
8.21

N = 10 Sum = 38.799999 Average = 3.880000

Write a program to demonstrate the use of scanf function.

Though I've already explained about 'scanf' function in my previous examples. I would like to add some of point here. The first executable statement is of-course the 'printf' function. Which is also know as 'prompt message'. Some compilers permit the use of the 'prompt message' as a part of control string in scanf, like

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


#include <stdio.h>
main()
{
int number;
printf("Enter an integer number\n");
scanf("%d", &number);
if( number <>
printf("Your number is smaller then 100\n");
else
printf("Your number contains more than two digits\n");
}

Output:

Enter an integer number
54
Your number is smaller than 100

Enter an integer number
108
Your number contains more than two digits

Witer a program to Represent integer constants.

We rarely use octal and hexadecimal numbers in programming. The largest integer value that can be stored in machine-dependent. It is 32767 on 16-bit machines and 2,147,483,647 on 32-bit machines.


#include <stdio.h>
main()
{
printf("Integer values\n");
printf("%d%d%d\n", 32767,32767+1,32767+10);
printf("\n");
printf("Long integer values\n\n");
printf("%ld%ld%ld\n", 32767L, 32767L+1L, 32767L+10L);
return 0;
}


Output:

Integer values

32767 -32768 -32759

Long integer values

32767 32768 32777

Write a program to output the Multiplication table.

The problem is to program a multiplication table. The program is....


#include <stdio.h>
main()
{
int i,n,x;
printf("Enter the value for n: ");
scanf("%d", &n);
for(i=1; i<=10; ++i)
{
x = n * i ;
printf("%d * %d = %d\n", n,i,x);
}
getch();
return 0;
}


The program reads the value of "n" at the run time using the predefine function "scanf".

Output:

Enter the value for n: 5

5 * 1 = 5
5 * 2 = 10
5 * 3 = 15
5 * 4 = 20
5 * 5 = 25
5 * 6 = 30
5 * 7 = 35
5 * 8 = 40
5 * 9 = 45
5 * 10 = 50

Write a program to print your mailing address.

The program ask the programmer to print the mailing address in the following form:

1. First line: Name.
2. Second line: Door No, Street.
3. Third line: City, Pin Code.

To display the out put in above formate we use newline character "\n". A newline character instructs the computer to go to the next(new) line.

#include <stdio.h>
main()
{
printf("Jubilant Organosys Limited\n");
printf("Nimbut Village, Nira R. S.\n");
printf("Pune-412102 \n");
return 0;
}

Output:

Jubilant Organosys Limited,
Nimbut Village, Nira R.S.
Pune-412102.

Write a program using math funtion.

We often use standard mathematical funtions such as cos, sin, exp, etc. In this program we add a new header called #include"math.h" that instructs the compiler to link the specified mathematical function form the library. We shall see now the use of a mathematical function in a program.

#include <stdio.h>
#include <math.h>
#define PI 3.1416
#define MAX 180
main()
{
int angle;
float x,y;
angle = 0;
printf(" Angle Cos(angle)\n\n");
while(angle <= MAX)
{
x = (PI/MAX)*angle;
y = cos(x);
printf("%15d %13.4f\n", angle, y);
angle = angle + 10;
}
}

Output:


Angle Cos(angle)
0 1.0000
10 0.9848
20 0.9397
30 0.8660
40 0.7660
.
.
.
.
.
160 -0.9397
170 -0.9848
180 -1.0000

Write a program using user-defined funtion.

The program uses a user-defined function. A function defined by the user is equivalent to a subroutine in FORTRAN or subprogram in BASIC.

#include <stdio.h>
main()
{
int a,b,c;
a = 5;
b = 10;
c = mul(a,b);
printf(" Multiplication of %d and %d is %d.", a,b,c);
}
mul(x,y)
int p,x,y;
{
p = x*y;
return(p);
}

The program will print the following output:

Multiplication of 5 and 10 is 50.

Write a program to calculater Interest rate.

The program calculates the value of money at the end of each year of investment assuming an interest rate of 11 percent and prints the year and the corresponding amount, in two columns.

  1. #include"stdio.h"
  2. #define PERIOD 10
  3. #define PRINCIPAL 5000.00
  4. main()
  5. {
  6. int year;
  7. float amount, value, inrate;
  8. amount = PRINCIPAL;
  9. inrate = 0.11;
  10. year = 0;
  11. while(year <= PERIOD)
  12. {
  13. printf("%2d %8.2f\n", year, amount);
  14. value = amount + inrate * amount;
  15. year = year + 1;
  16. amount = value;
  17. }
  18. }

The output after the execution will be :

0 5000.00
1 5550.00
2 6160.50
3 68.38.15
.
.
.
.
10 14197.11

Write a program to add two numbers.

This program performs addition on two numbers and displays the result.

#include <stdio.h>
main()
{
int number;
float amount;
number = 100;
amount = 30.75 + 75.35;
printf("%d\n", number);
printf("%5.2f", amount);
}


This program when executed will produce following output:

100
106.10

Hello World.......! in C

The beginning of programming in ' C '. The #include"stdio.h"is a standard I/O header file containing standard input and output functions and it is often required.

#include<stdio.h>
void main()
{
printf("Hello, World..........!");
}

Output :-

Hello, World..........!
 

About Me

It's Me!Hi, I'm Moinuddin. I'm a Software Engineer at WIPRO working on Microsoft .NET Technology. I'm interested in a wide range of technology topics, mainly Microsoft including Windows Azure, WCF, WPF, Silverlight and any other cool technology that catches the eye.

Site Info

ProjectCSharp.com is mainly about demonstrating real time examples in different technologies like ASP.NET, WCF, WPF, Silverlight, Azure etc..,

Followers

Help Links

Project CSharp (C#) Copyright © 2011. All Rights Reserved.