Itp File - 1

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 33

Program 1 : Write a program to calculate simple interest.

#include<stdio.h>
int main()
{
int p,r,t,int_amt;
printf("Input principle, Rate of interest & time to find simple interest: \n");
scanf("%d%d%d",&p,&r,&t);
int_amt=(p*r*t)/100;
printf("Simple interest = %d",int_amt);
return 0;
}

Output
Program : 2 Write a program to swap two number with the
help of third variable.
#include <stdio.h>
int main()
{
int var1, var2, temp;
printf("Enter two integersn");
scanf("%d%d", &var1, &var2);
printf("Before SwappingnFirst variable = %dnSecond variable = %dn", var1,
var2);
temp = var1;
var1 = var2;
var2 = temp;
printf("After SwappingnFirst variable = %dnSecond variable = %dn", var1,
var2);
return 0;
}

Output
Program 3 :Write a program to swap two number without the
help of third variable.
#include<stdio.h>
int main()

int a=1, b=2;

printf("Before swap a=%d b=%d",a,b);

a=a+b;

b=a-b;

a=a-b;

printf("\nAfter swap a=%d b=%d",a,b);

return 0;

Output
Program 4 : Convert temperature in Celsius to fah

#include<stdio.h>

void main()

float celsius,fahrenheit;

printf("\n Enter the Temparature in Celsius : ");

scanf("%f",&celsius);

fahrenheit = (1.8 * celsius) + 32;

printf("\n Temperature in Fahrenheit : %f ",fahrenheit);

return 0;

Output
Program 5 : Write a program to find area of triangle using
heron’s formula
#include<stdio.h>

#include<math.h>

int main()

float a, b, c, s, Area;

printf("\nPlease Enter three sides of triangle\n");

scanf("%f%f%f",&a,&b,&c);

s = (a+b+c)/2;

Area = sqrt(s*(s-a)*(s-b)*(s-c));

printf("\n Area of triangle = %.2f\n",Area);

return 0;

Output
Program 6 : Write a program to implement Logical operator
#include <stdio.h>

int main()

int a = 8, b = 7, c = 7, result;

result = (a == b) && (c > b);

printf("(a == b) && (c > b) is %d \n", result);

result = (a == b) && (c < b);

printf("(a == b) && (c < b) is %d \n", result);

result = (a == b) || (c < b);

printf("(a == b) || (c < b) is %d \n", result);

result = (a != b) || (c < b);

printf("(a != b) || (c < b) is %d \n", result);

result = !(a != b);

printf("!(a != b) is %d \n", result);

result = !(a == b);

printf("!(a == b) is %d \n", result);

return 0;

}
Output
Program 7 : Write a program to implement Relational
operator
#include <stdio.h>

int main()

int a = 7, b = 5, c = 8;

printf("%d == %d is %d \n", a, b, a == b);

printf("%d == %d is %d \n", a, c, a == c);

printf("%d > %d is %d \n", a, b, a > b);

printf("%d > %d is %d \n", a, c, a > c);

printf("%d < %d is %d \n", a, b, a < b);

printf("%d < %d is %d \n", a, c, a < c);

printf("%d != %d is %d \n", a, b, a != b);

printf("%d != %d is %d \n", a, c, a != c);

printf("%d >= %d is %d \n", a, b, a >= b);

printf("%d >= %d is %d \n", a, c, a >= c);

printf("%d <= %d is %d \n", a, b, a <= b);

printf("%d <= %d is %d \n", a, c, a <= c);

return 0;

}
Output
Program 8 : Write a program to implement Bitwise operator
#include <stdio.h>

int main()

int a = 12, b = 25;

printf("Output = %d\n", a&b);

printf("Output = %d\n", a|b);

printf("Output = %d\n", a^b);

printf("Output = %d\n",~-a);

printf("Left shift by %d: %d\n", a<<2);

printf("Right shift by %d: %d\n", a>>2);

return 0;

Output
Program 9 : Largest of two numbers using
conditional/ternary operator.
#include<stdio.h>

void main()

int a,b,larg;

printf("Enter two number\n");

scanf("%d %d",&a,&b);

larg = a>b?a:b;

printf("largest number is : %d",larg);

return 0;

Output
Program 10 : Program to check whether two numbers are
equal or not using conditional/ternary operator.
#include<stdio.h>

void main()

int a,b;

printf("Enter two number\n");

scanf("%d %d",&a,&b);

b = ( a>b ? printf("a is greater") : printf("b is greater") );

Output
Program 11 : Largest of three numbers using
conditional/ternary operator.
# include <stdio.h>

void main()

int a, b, c, big ;

printf("Enter three numbers : ") ;

scanf("%d %d %d", &a, &b, &c) ;

big = a > b ? (a > c ? a : c) : (b > c ? b : c) ;

printf("\nThe biggest number is : %d", big) ;

Output
Program 12 : whether a number is even or odd
#include <stdio.h>

int main() {

int num;

printf("Enter an integer: ");

scanf("%d", &num);

if(num % 2 == 0)

printf("%d is even.", num);

else

printf("%d is odd.", num);

return 0;

Output
Program 13 : Write a program to find sum of 4-digit
number.

#include <stdio.h>

int main()

int n,f,x,s,y,t,l,sum;

printf("Enter 4-Digit Number: ");

scanf("%d",&n);

f=n/1000;

x=n%1000;

s=x/100;

y=x%100;

t=y/10;

l=y%10;

printf("\nFirst Digit = %d \nSecond Digit = %d \nThird Digit =


%d\nLast Digit = %d\n",f,s,t,l);

sum=f+s+t+l;

printf("\nSum of All 4-Digits : %d",sum);

return 0;

}
Program 14 : write a program to reverse a three digit number
#include <stdio.h>

int main()

int n, num1,num2,num3, reverse ;

printf("Enter the number to reverse:\n");

scanf("%d", &n);

num1 = n / 100;

num2 = (n % 100) / 10;

num3 = n%10 ;

reverse = 100*num3 + 10*num2 + num1;

printf(" The reverse is %d", reverse);

return 0;

}
Program 15 :Write a program to convert no. of days into
years, months and days.

#include <stdio.h>

int main() {

int ndays, y, m, d;

printf("Input no. of days: ");

scanf("%d", &ndays);

y = (int) ndays/365);

ndays = ndays-(365*y);

m = (int)ndays/30;

d = (int)ndays-(m*30);

printf(" %d Year(s) \n %d Month(s) \n %d Day(s)", y, m, d);

return 0;

}
Program 16 : largest of two numbers using if-else
#include <stdio.h>

int main()

int num1, num2;

printf("Please Enter Two different values\n");

scanf("%d %d", &num1, &num2);

if(num1 > num2)

printf("%d is Largest\n", num1);

else if (num2 > num1)

printf("%d is Largest\n", num2);

return 0;

Output
Program 17 : largest of three numbers using if-else
#include <stdio.h>

int main() {

double n1, n2, n3;

printf("Enter three numbers: ");

scanf("%lf %lf %lf", &n1, &n2, &n3);

if (n1 >= n2 && n1 >= n3)

printf("%.2lf is the largest number.", n1);

else if (n2 >= n1 && n2 >= n3)

printf("%.2lf is the largest number.", n2);

else

printf("%.2lf is the largest number.", n3);

return 0;

}
Output
Program 18 : Write a C program to input electricity unit
charge and calculate the total electricity bill according to the
given condition:

For first 50 units Rs. 0.50/unit

For next 100 units Rs. 0.75/unit

For next 100 units Rs. 1.20/unit

For unit above 250 Rs. 1.50/unit

An additional surcharge of 20% is added to the bill.


#include <stdio.h>

int main()

int unit;

float amt, total_amt, sur_charge;

printf("Enter total units consumed: ");

scanf("%d", &unit);

if(unit <= 50)

amt = unit * 0.50;

else if(unit <= 150)

{
amt = 25 + ((unit-50) * 0.75);

else if(unit <= 250)

amt = 100 + ((unit-150)* 1.20);

else

amt = 220 + ((unit-250) * 1.50);

sur_charge = amt * 0.20;

total_amt = amt + sur_charge;

printf("Electricity Bill = Rs. %.2f", total_amt);

return 0;

}
Output
Program 19 : Write a c program to Arrange 3 numbers in
increasing order.
#include<stdio.h>

void main()

int a,b,c;

printf("Enter numbers : ");

scanf("%d%d%d",&a,&b,&c);

if((a>=b)&&(a>=c))

if(b>=c)

printf("\n Ascending order : %d %d %d",c,b,a);

else

printf("\n Ascending order : %d %d %d",b,c,a);

else if((b>=a)&&(b>=c))

if(a>=c)

printf("\n Ascending order : %d %d %d",c,a,b);

else

printf("\n Ascending order : %d %d %d",a,c,b);


}

else if((c>=a)&&(c>=b))

if(a>=b)

printf("\n Ascending order : %d %d %d",b,a,c);

else

printf("\n Ascending order : %d %d %d",a,b,c);

Output
Program 20 : If the basic salary is less than Rs 1500 ,then
HRA is 10% of the basic salary and DA is 90% of basic salary
if the salary is either equal to or above 1500 then HRA is 500
and DA is 95% of basic salary .if the employee salary is
entered then find the gross salary and display it.
#include<stdio.h>

int main()

float bs, gs, da, hra;

printf ("Enter basic salary:");

scanf ("%f", &bs);

if (bs<1500)

hra=bs*10/100;

da=bs*90/100;

else

hra=500;

da=bs*95/100;

gs=bs + hra + da;


printf ("gross salary = Rs. %f", gs);

Output
Program 21 : to check whether vowel or not by switch case
#include <stdio.h>

int main()

char ch;

printf("Enter any character: ");

scanf("%c", &ch);

switch(ch)

case 'a':

case 'e':

case 'i':

case 'o':

case 'u':

case 'A':

case 'E':

case 'I':

case 'O':

case 'U':

printf("It is a Vowel");

break;
default: printf("Consonant");

return 0;

Output
Program 22 : Write a program for calculator using switch
case
#include <stdio.h>

int main() {

char operator;

double first, second;

printf("Enter an operator (+, -, *,): ");

scanf("%c", &operator);

printf("Enter two operands: ");

scanf("%lf %lf", &first, &second);

switch (operator) {

case '+':

printf("%.1lf + %.1lf = %.1lf", first, second, first +second);

break;

case '-':

printf("%.1lf - %.1lf = %.1lf", first, second, first - second);

break;

case '*':

printf("%.1lf * %.1lf = %.1lf", first, second, first * second);

break;

case '/':
printf("%.1lf / %.1lf = %.1lf", first, second, first / second);

break;

default:

printf("Error! operator is not correct");

return 0;

Output

You might also like