You are on page 1of 19

C program to find SUM and AVERAGE of two numbers

/* c program find sum and average of two numbers*/


#include <stdio.h>
int main()
{
int a,b,sum;
float avg;
printf("Enter first number :");
scanf("%d",&a);
printf("Enter second number :");
scanf("%d",&b);
sum=a+b;
avg= (float)(a+b)/2;
printf("\nSum of %d and %d is = %d",a,b,sum);
printf("\nAverage of %d and %d is = %f",a,b,avg);
return 0;
}

OUTPUT:
Enter first number :10
Enter second number :15

Sum of 10 and 15 is = 25
Average of 10 and 15 is = 12.500000

C program to calculate Simple Interest


/* c program to calculate simple interest*/
#include <stdio.h>
int main()
{
float amount,rate,time,si;

printf("Enter principal (Amount) :");


scanf("%f",&amount);
printf("Enter rate :");
scanf("%f",&rate);
printf("Enter time (in years) :");
scanf("%f",&time);
si=(amount*rate*time)/100;
printf("\nSimple Interest is = %f",si);
return 0;
}

Enter principal (Amount) :15000


Enter rate :2.8
Enter time (in years) :2

Simple Interest is = 840.000000

C program to check EVEN or ODD


/* c program to check whether number is even or odd*/
#include <stdio.h>
int main()
{
int num;
printf("Enter an integer number: ");
scanf("%d",&num);
/*If number is divisible by 2 then number
is EVEN otherwise number is ODD*/
if(num%2==0)
printf("%d is an EVEN number.",num);
else
printf("%d is an ODD number.",num);

return 0;
}

First Run:
Enter an integer number: 123
123 is an ODD number.

Second Run:
Enter an integer number: 110
110

an EVEN number.

C program to find Largest Number among three numbers


/* c program to find largest number among 3 numbers*/
#include <stdio.h>
int main()
{
int a,b,c;
int largest;
printf("Enter three numbers (separated by space):");
scanf("%d%d%d",&a,&b,&c);
if(a>b && a>c)
largest=a;
else if(b>a && b>c)
largest=b;
else
largest=c;
printf("Largest number is = %d",largest);
return 0;
}

First Run:
Enter three numbers (separated by space): 10 20 30
Largest number is = 30

Second Run:
Enter three numbers (separated by space):10 30 20
Largest number is = 30

Third Run:
Enter three numbers (separated by space):30 10 20
Largest number is = 30

Fourth Run:
Enter three numbers (separated by space):30 30 30
Largest number is = 30

C program to convert temperature from Fahrenheit to Celsius and Celsius to


Fahrenheit
/* C Program to convert temperature from Fahrenheit to Celsius and vice
versa.*/
#include <stdio.h>
int main()
{
float fh,cl;
int choice;
printf("\n1: Convert temperature from Fahrenheit to Celsius.");
printf("\n2: Convert temperature from Celsius to Fahrenheit.");
printf("\nEnter your choice (1, 2): ");
scanf("%d",&choice);

if(choice ==1){
printf("\nEnter temperature in Fahrenheit: ");
scanf("%f",&fh);
cl= (fh - 32) / 1.8;
printf("Temperature in Celsius: %.2f",cl);
}
else if(choice==2){
printf("\nEnter temperature in Celsius: ");
scanf("%f",&cl);
fh= (cl*1.8)+32;
printf("Temperature in Fahrenheit: %.2f",fh);
}
else{
printf("\nInvalid Choice !!!");
}
return 0;
}

First Run:
1: Convert temperature from Fahrenheit to Celsius.
2: Convert temperature from Celsius to Fahrenheit.
Enter your choice (1, 2): 1

Enter temperature in Fahrenheit: 98.6


Temperature in Celsius: 37.00

Second Run:
1: Convert temperature from Fahrenheit to Celsius.
2: Convert temperature from Celsius to Fahrenheit.
Enter your choice (1, 2): 2

Enter temperature in Celsius: 37.0

Temperature in Fahrenheit: 98.60

Third Run:
1: Convert temperature from Fahrenheit to Celsius.
2: Convert temperature from Celsius to Fahrenheit.
Enter your choice (1, 2): 3

Invalid Choice !!!

C program to calculate Gross Salary of an employee


/* c program to calculate salary of an employee with name */
#include <stdio.h>
int main()
{
char name[30];
float basic, hra, da, pf, gross;
printf("Enter name: ");
gets(name);
printf("Enter Basic Salary: ");
scanf("%f",&basic);
printf("Enter HRA: ");
scanf("%f",&hra);
printf("Enter D.A.: ");
scanf("%f",&da);
/*pf automatic calculated 12%*/
pf= (basic*12)/100;
gross=basic+da+hra+pf;
printf("\nName: %s \nBASIC: %f \nHRA: %f \nDA: %f \nPF: %f \n***GROSS
SALARY: %f ***",name,basic,hra,da,pf,gross);
return 0;

Enter name: Mike


Enter Basic Salary: 23000
Enter HRA: 9500
Enter D.A.: 9500

Name: Mike
BASIC: 23000.000000
HRA: 9500.000000
DA: 9500.000000
PF: 2760.000000
***GROSS SALARY: 44760.000000 ***

C program to find area and perimeter of circle


/*C program to find area and perimeter of circle.*/

#include <stdio.h>

#define PI 3.14f

int main()
{
float rad,area, perm;

printf("Enter radius of circle: ");


scanf("%f",&rad);

area=PI*rad*rad;
perm=2*PI*rad;

printf("Area of circle: %f \nPerimeter of circle: %f\n",area,perm);


return 0;
}
Enter radius of circle: 2.34
Area of circle: 17.193384
Perimeter of circle: 14.695200

C program to find area of rectangle


/*C program to find area of a rectangle.*/
#include <stdio.h>
int main()
{
float l,b,area;
printf("Enter the value of length: ");
scanf("%f",&l);
printf("Enter the value of breadth: ");
scanf("%f",&b);
area=l*b;
printf("Area of rectangle: %f\n",area);
return 0;
}

Enter the value of length: 1.25


Enter the value of breadth: 3.15
Area of rectangle: 3.937500

C program to find HCF (Highest Common Factor) of two numbers.

/*C program to find HCF of two numbers.*/


#include <stdio.h>
//function to find HCF of two numbers
int findHcf(int a,int b)
{
int temp;
if(a==0 || b==0)
return 0;
while(b!=0)
{
temp = a%b;
a
= b;
b
= temp;
}
return a;
}
int main()
{
int a,b;
int hcf;
printf("Enter first number: ");
scanf("%d",&a);
printf("Enter second number: ");
scanf("%d",&b);
hcf=findHcf(a,b);
printf("HCF (Highest Common Factor) of %d,%d is: %d\n",a,b,hcf);
return 0;
}

Enter first number: 100


Enter second number: 40
HCF (Highest Common Factor) of 100,40 is: 20

C program to read weekday number and print weekday name using switch
/*C program to read weekday number and print weekday name using switch.*/

#include <stdio.h>
int main()
{
int wDay;
printf("Enter weekday number (0-6): ");
scanf("%d",&wDay);
switch(wDay)
{
case 0:
printf("Sunday");
break;
case 1:
printf("Monday");
break;
case 2:
printf("Tuesday");
break;
case 3:
printf("Wednesday");
break;
case 4:
printf("Thursday");
break;
case 5:
printf("Friday");
break;
case 6:
printf("Saturday");
break;
default:
printf("Invalid weekday number.");
}
printf("\n");
return 0;
}

First Run:
Enter weekday number (0-6): 3
Wednesday

Second run:

Enter weekday number (0-6): 9


Invalid weekday number.

C program to check whether a character is VOWEL or CONSONANT using switch


/*C program to check whether a character is VOWEL or CONSONANT using switch.*/
#include <stdio.h>
int main()
{
char ch;
printf("Enter a character: ");
scanf("%c",&ch);
//condition to check character is alphabet or not
if((ch>='A' && ch<='Z') || (ch>='a' && ch<='z'))
{
//check for VOWEL or CONSONANT
switch(ch)
{
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
printf("%c is a VOWEL.\n",ch);
break;
default:
printf("%c is a CONSONANT.\n",ch);
}
}
else
{
printf("%c is not an alphabet.\n",ch);
}
return 0;
}

First Run:
Enter a character: E
E is a VOWEL.

Second Run:
Enter a character: X
X is a CONSONANT.

Third Run:
Enter a character: +
+ is not an alphabet.

C program to design calculator with basic operations using switch


/*C program to design calculator with basic operations using switch.*/
#include <stdio.h>
int main()
{
int num1,num2;
float result;
char ch;
//to store operator choice
printf("Enter first number: ");
scanf("%d",&num1);
printf("Enter second number: ");
scanf("%d",&num2);
printf("Choose operation to perform (+,-,*,/,%): ");
scanf(" %c",&ch);
result=0;
switch(ch)
{

case '+':
result=num1+num2;
break;
case '-':
result=num1-num2;
break;
case '*':
result=num1*num2;
break;
case '/':
result=(float)num1/(float)num2;
break;
case '%':
result=num1%num2;
break;
default:
printf("Invalid operation.\n");
}
printf("Result: %d %c %d = %f\n",num1,ch,num2,result);
return 0;
}

First run:
Enter first number: 10
Enter second number: 20
Choose operation to perform (+,-,*,/,%): +
Result: 10 + 20 = 30.000000

Second run:
Enter first number: 10
Enter second number: 3
Choose operation to perform (+,-,*,/,%): /

Result: 10 / 3 = 3.333333

Third run:
Enter first number: 10
Enter second number: 3
Choose operation to perform (+,-,*,/,%): >
Invalid operation.
Result: 10 > 3 = 0.000000

C program to input and print text using Dynamic Memory Allocation


/*C program to input and print text
using Dynamic Memory Allocation.*/
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n;
char *text;
printf("Enter limit of the text: ");
scanf("%d",&n);
/*allocate memory dynamically*/
text=(char*)malloc(n*sizeof(char));
printf("Enter text: ");
scanf(" "); /*clear input buffer*/
gets(text);
printf("Inputted text is: %s\n",text);
/*Free Memory*/
free(text);
return 0;
}

Enter limit of the text: 100


Enter text: I am mike from California, I am computer geek.
Inputted text is: I am mike from California, I am computer
geek.

C program to read and print One Dimensional Array of integer elements


/*Program to read and print one dimensional array. */
#include <stdio.h>
/** funtion :
readArray()
input
:
arr ( array of integer ), size
to read ONE-D integer array from standard input device (keyboard).
**/
void readArray(int arr[], int size)
{
int i =0;
printf("\nEnter elements : \n");
for(i=0; i<size; i++)
{
printf("Enter arr[%d] : ",i);
scanf("%d",&arr[i]);
}
}
/** funtion :
printArray()
input
:
arr ( array of integer ), size
to display ONE-D integer array on standard output device (moniter).
**/
void printArray(int arr[],int size)
{
int i =0;
printf("\nElements are : ");
for(i=0; i<size; i++)
{
printf("\n\tarr[%d] : %d",i,arr[i]);
}
printf("\n");
}

int main()
{
int arr[10];
readArray(arr,10);
printArray(arr,10);
return 0;
}

Enter elements :
Enter arr[0] : 1
Enter arr[1] : 2
Enter arr[2] : 3
Enter arr[3] : 4
Enter arr[4] : 5
Enter arr[5] : 6
Enter arr[6] : 7
Enter arr[7] : 8
Enter arr[8] : 9
Enter arr[9] : 10

Elements are :
arr[0] : 1
arr[1] : 2
arr[2] : 3
arr[3] : 4
arr[4] : 5
arr[5] : 6

arr[6] : 7
arr[7] : 8
arr[8] : 9
arr[9] : 10

program to create a text file using file handling, example of fopen, fclose

#include< stdio.h >


int main()
{
FILE *fp;
/* file pointer*/
char fName[20];
printf("Enter file name to create :");
scanf("%s",fName);
/*creating (open) a file, in w: write mode*/
fp=fopen(fName,"w");
/*check file created or not*/
if(fp==NULL)
{
printf("File does not created!!!");
exit(0); /*exit from program*/
}
printf("File created successfully.");
return 0;
}

Run 1:
Enter file name to create : file1.txt
File created successfully.

Run 2:

Enter file name to create : d:/file1.txt


File created successfully.

file will be created in d: drive.

Run 3:
Run 1:
Enter file name to create : h:/file1.txt
File does not created!!!

C program to create, declare and initialize structure


/*C program to create, declare and initialize structure.*/
#include <stdio.h>
/*structure declaration*/
struct employee{
char
name[30];
int
empId;
float salary;
};
int main()
{
/*declare and initialization of structure variable*/
struct employee emp={"Mike",1120,76909.00f};
printf("\n Name: %s"
,emp.name);
printf("\n Id: %d"
,emp.empId);
printf("\n Salary: %f\n",emp.salary);
return 0;
}

Name: Mike

Id: 1120

Salary: 76909.000000

You might also like