C Programs
C Programs
Institute of Technology
An Autonomous Institution under VTU
Department of Information Science and Engineering
List of Programs
#include<stdio.h>
int main()
{
float amount, rate, time, si;
printf("\nEnter Principal Amount : ");
scanf("%f", &amount);
printf("\nEnter Rate of Interest : ");
scanf("%f", &rate);
printf("\nEnter Period of Time : ");
scanf("%f", &time);
si = (amount * rate * time) / 100;
printf("\nSimple Interest : %f", si);
return(0);
}
1b. Write a program to read a character and print its ASCII value
#include<stdio.h>
int main()
{
char x;
printf(“Enter an character : “);
scanf(“%c”, &x);
printf(“\n The character is %c and its ASCII value is %d \n”, x, x);
return 0;
}
#include<stdio.h>
int main()
{
int num1, num2;
float fraction;
char character;
printf("Enter two numbers number\n");
scanf("%d%i", &num1, &num2);
printf("\n\nThe two numbers You have entered are %d and %i\n\n", num1, num2);
printf("\n\nEnter a Decimal number\n");
scanf("%f", &fraction);
printf("\n\nThe float or fraction that you have entered is %f", fraction);
printf("\n\nEnter a Character\n");
scanf("%c",&character);
printf("\n\nThe character that you have entered is %c", character);
return 0;
}
3a. Construct a program to find whether the given number is odd or even using conditional
operator
#include<stdio.h>
void main()
{
int num,rem;
printf(“\n Enter a number : “);
scanf(“%d”, &num);
rem=num % 2;
(rem==0)?printf(“No. is even\n”): printf(“No. is odd\n”);
}
#include<stdio.h>
void main()
{
int x = 6, y = 4;
x = x^y;
y = x^y;
x = x^y;
printf("x = %d and y = %d", x, y);
}
#include<stdio.h>
int main()
{
int a, b, add, subtract, multiply;
float divide;
printf("Enter two integers: \n");
scanf("%d%d", &a, &b);
add = a+b;
subtract = a-b;
multiply = a*b;
divide = a/b;
printf("\nAddition of the numbers = %d\n", add);
printf("Subtraction of 2nd number from 1st = %d\n", subtract);
printf("Multiplication of the numbers = %d\n", multiply);
printf("Dividing 1st number from 2nd = %f\n", divide);
return 0;
}
5.Construct a program to read an angle from the user and display its quadrant using if-
else-if statement
#include<stdio.h>
void main()
{
int angle;
printf(“Enter angle [0-360] : “);
scanf(“%d”,&angle);
if(angle > 0 && angle < 90)
printf(“The angle is in first quadrant \n”);
else if(angle>90 && angle <180)
printf(“The angle is in second quadrant \n”);
else if(angle >180 && angle <270)
printf(“The angle is in third quadrant \n”);
else
printf(“The angle is in fourth quadrant \n);
}
1 2 3 n
8a. To find sum of series + + + ………. + using for loop.
2 3 4 n+1
#include<stdio.h>
void main()
{ int n, i; float sum=0;
printf(“Enter a value for n : “);
scanf(“%d”,&n);
for(i=1;i<=n;i++)
sum = sum + (i / (i+1.0));
printf(“The sum of the series = %f \n”,sum);
}
9. Construct a program to read a value [1-7] and print ‘Monday’ if input is 1, ‘Tuesday’ if
input is 2 and so on using Switch Statement.
#include<stdio.h>
void main()
{
int num;
printf(“Enter a value between 1 to 7 : “);
scanf(“%d”,&num);
switch(num)
{
case 1: printf(“Monday \n”);
break;
case 2: printf(“Tuesday \n”);
break;
case 3: printf(“Wednesday \n”);
break;
case 4: printf(“Thursday \n”);
break;
case 5: printf(“Friday \n”);
break;
case 6: printf(“Saturday \n”);
break;
case 7: printf(“Sunday \n”);
break;
default: printf(“The input is not valid \n”);
break;
}
}
#include<stdio.h>
void main()
{ int x[5]={1,2,3,4,5}, i, *p,sum=0;
p = x;
for(i=0;i<5;i++)
{ sum sum + *p;
p=p+1;
}
printf(“The sum = %d \n”, sum);
}
#include<stdio.h>
void main()
{
float degc, degf;
printf(“Enter temperature in Celsius : \n”);
scanf(“%f”, °c);
degf = (degc * 9 / 5) + 32;
printf(“The temperature in Fahrenheit is %f \n”, degf);
}
#include<stdio.h>
void main()
{
int a[5]={1,2,3,4,5}, i, *p,temp,n=5;
p = a;
for(i=0;i<n/2;i++)
{
temp = *(p+i);
*(p+i) = *(p+n-1-i);
*(p+n-1-i) = temp;
}
for(i=0;i<n;i++)
printf(“ %d \t”, a[i]);
printf(“\n”);
}