Anda di halaman 1dari 8

// a program to convert any lentgh from CM to inch

#define CM_PER_INCH 2.54;


main()
{
float inches , cm;
printf("Enter the lentgh in inches: \n");
scanf("%f",& inches);
cm = 2.54* inches;
printf("%.2f",cm);
}
--------------------------------------------------------------------------------
--------------
--------------------------------------------------------------------------------
--------------
// A program for finding area of a circle by radius
#include <stdio.h>
// area of the circle
main()
{
int radius;
double area;
const double PI=3.14;
printf("Enter radius of the circle: ");
scanf("%d",& radius);
area= PI*radius*radius;
printf("the area of the circle is : %f", area);
}
--------------------------------------------------------------------------------
--------------
--------------------------------------------------------------------------------
--------------
// A program to count stars (less than 2)
#include<stdio.h>
main()
{
int count_star = 0;
do{printf("*");
count_star++;
}while (count_star<2);
}
--------------------------------------------------------------------------------
--------------
--------------------------------------------------------------------------------
--------------
// A program to find an averge of three scores
#include<stdio.h>
main()
{
int N=3;
int score;
int total=0;
int count=1;
float avg;
while (count<=N)
{
printf("Enter score: \n");
scanf("%d",& score);
total += score;
count++;
}
printf("the total is %d\n", total);
avg= total/N;
printf("the average is %.2f\n", avg);
}
--------------------------------------------------------------------------------
--------------
--------------------------------------------------------------------------------
--------------
// A program to find an average
#include<stdio.h>
#define SENTINEL 3
main()
{

int total=0;
int score;
float avg;
int count=0;

printf("Enter score or 3 to end:");


scanf ("%d", &score);
while (score != SENTINEL) {
total += score;
count++;
}
if (count) {
avg = total/count;
printf("the total is %d\n", total);
printf("Average is %.2f\n", avg);
}else printf("No scores were entered\n");
}
--------------------------------------------------------------------------------
--------------
--------------------------------------------------------------------------------
--------------
// An example of summing the numbers
#include<stdio.h>
main()
{
int num=0;
int sum=0;
for(num=2;num < 20;num=num+2)
{
sum += num;
printf("The sum of numbers is: %d\n", sum);
}
}
--------------------------------------------------------------------------------
--------------
--------------------------------------------------------------------------------
--------------
//An example of using for loop
#include<stdio.h>
main()
{
int x;
for(x=0;x<10;x++)
{
if (x==5)break;
printf("%d", x);
}
printf("the broke out of loop at x= %d", x);
}
--------------------------------------------------------------------------------
--------------
--------------------------------------------------------------------------------
--------------
// An example of using Using continue in a for loop
#include<stdio.h>

main()
{ int x;
for (x=1;x<=10;x++){
if (x == 5)
continue;
printf("%d ",x);
}
printf("\n Used the continue statement to skip printing the value 5\n\n");

}
--------------------------------------------------------------------------------
--------------
--------------------------------------------------------------------------------
--------------
// A program to make a box of stars
#include<stdio.h>
main()
{
int length,width,num_stars;
printf("enter the length of the square side: \n");
scanf("%d",& length);
width=length;
for(length=0;length<10;length++)
{ for(width=0;width<10;width++)
{printf("*");
}
putchar('\n');
}
}
--------------------------------------------------------------------------------
--------------
--------------------------------------------------------------------------------
--------------
// An example of storing value into an array
#include<stdio.h>
main()
{
int A[5],i;
printf("Enter the values of an Array:");
for(i=0;i<5;i++)
scanf("%d",&A[i]);
for(i=0;i<5;i++)
printf("%d", A[i]);
}

--------------------------------------------------------------------------------
--------------
--------------------------------------------------------------------------------
--------------
// Another example of storing values in
#include<stdio.h>
main()
{
int A[5]={10,20,30,40,50};
int i;
for(i=0;i<5;i++)
printf("%d,", A[i]);
}
--------------------------------------------------------------------------------
--------------
--------------------------------------------------------------------------------
--------------
// An example of finding the maximum value in an array
#include<stdio.h>
main()
{
int A[5]={10,20,30,40,50};
int i,max;
max=A[0];
for(i=0;i<5;i++)
{
if(A[i]>max)
{
max=A[i];
printf("the max no: %d,\n", max);
}
}
}

--------------------------------------------------------------------------------
--------------
--------------------------------------------------------------------------------
--------------
// A program to find a required value in an array !
#include<stdio.h>
main()
{
int search_key, i;
int a[5]={10,20,30,40,50};
printf("\nEnter the Search key value:");
scanf("%d",&search_key);
for(i=0;i<5;i++)
{
if (a[i] == search_key)
{ printf("the location of the element %d is %d\n", search_key,i)
;
break;
}
}
}

--------------------------------------------------------------------------------
--------------
--------------------------------------------------------------------------------
--------------
// An example of string
#include<stdio.h>
main()
{ char s1[20];
int i;
printf("\nEnter a string :");
scanf("%s",s1);
printf("%s",s1);
}
--------------------------------------------------------------------------------
--------------
--------------------------------------------------------------------------------
--------------
// A simple example of pointer
#include<stdio.h>
void main()
{
char var = 'X'; // var a character variable, is declared and initialized to X
char *charPtr; //charPtr a pointer to a character is declared
charPtr = &var; //charPtr now holds (points to) the address of var
*charPtr = 'Y'; //The value which charPtr points to, is now changed to Y .
//using * in this manner is called derefere
ncing a pointer
printf("\n Value of var now is : %c",var); // displays new value Y
}
--------------------------------------------------------------------------------
--------------
--------------------------------------------------------------------------------
--------------
// An example of Call by value
#include<stdio.h>
void main()
{
void callbyvalue(int); //function prototype
int num = 5;
callbyvalue(num); //Function call sending value of num
printf("\n Value of num is %d",num); //Displays original value of nu
m
}
void callbyvalue (int b) //Parameter b is a mirror image of num
{
b=b+100; //b is increased by 100
printf("the value of B is %d\n",b);
}

--------------------------------------------------------------------------------
--------------
--------------------------------------------------------------------------------
--------------
//An example of Call by reference
#include<stdio.h>
void main()
{
void callbyref(int *); //function prototype
int num = 5; //Declaring integer variable num
callbyref(&num); //Function call sending address of num
printf("\n Value of num is %d",num); //Displays new value of num
}
void callbyref (int *b) //function header
{
*b=*b+100; //Value pointed to by b is increased by 100
}
--------------------------------------------------------------------------------
--------------
--------------------------------------------------------------------------------
--------------
// Another exapmle of pointer and its uses
#include<stdio.h>
void main() {
int * check (int); //function prototype
int *ptr, a=1000; //Declare ptr and a, assign 1000 to a
ptr=check(a); //Function call
printf( \n Value returned is %d\n ,*ptr); // *ptr is 0
}
int * check (int x) //function header
{
if(x<=1000)
x=0;
return (&x); //returns address of x
}
--------------------------------------------------------------------------------
--------------
--------------------------------------------------------------------------------
--------------
// Example of call by value and the changes in values
#include<stdio.h>
extern int a=1, b=2, c=3;
int f(void);
int main()
{ printf("%3d\n", f());
printf("%3d%3d%3d\n", a, b, c);
return 0;
}
int f(void)
{ int b, c;
a=b=c=4;
return (a + b + c);
}

--------------------------------------------------------------------------------
--------------
--------------------------------------------------------------------------------
--------------
// Call by value
#include<stdio.h>
void f(void);
int main()
{
printf("The function is run for the first time\n");
f();
printf("The function is run for the second time\n");
f();
return 0;
}
void f(void)
{
static int b=5;
printf("The value of
b is %d\n", b);
b++;
}
--------------------------------------------------------------------------------
--------------
--------------------------------------------------------------------------------
--------------
// Playing with values !
#include<stdio.h>
int main()
{
int a=1, b=2, c=3;
printf("Values for a, b and c in outer loop are : \n");
printf("%3d %3d %3d\n", a, b, c);
{ int b=4;
float c=5.0;
printf("Values for a, b and c in inner loop are : \n");
printf("%3d %3d %5.1f\n", a, b, c);
a=b;
{
int c;
c=b;
printf("Values for a, b, c in inner loop are: \n");
printf("%3d %3d %3d\n", a, b, c);
}
printf("Values for a, b and c in inner loop are: \n");
printf("%3d %3d %5.1f\n", a, b, c);
}
printf("Values for a, b and c in the outer loop: \n");
printf("%3d %3d %3d\n", a, b, c);
return 0;
}
--------------------------------------------------------------------------------
--------------
--------------------------------------------------------------------------------
--------------

Anda mungkin juga menyukai