Practice Sessions:
a. Write a simple program that prints the results of all the operators available in C
(including pre/ post increment , bitwise and/or/not , etc.). Read required operand values
from standard input.
#include <stdio.h>
void main()
{
int a,b,sum,sub,div,mul,mod;
printf("enter number a and b");
scanf("%d%d",&a,&b);
//artithmatic operators
printf("addition of a and b is %d\n",a+b);
printf("subtraction of a and b is %d\n",a-b);
printf("multiplication of a and b is %d\n",a*b);
printf("division of a and b is %d\n",a/b);
printf("modulus of of a and b is %d\n",a%b);
//unary operators
printf("pre increment of a is %d\n",++a);
printf("postincrement of a is %d\n",a++);
printf("predecrement of b is %d\n",--b);
printf("postdecrement of b is %d\n",b--);
printf("unary minus of a is %d\n",-a);
printf("size of a is %d\n",sizeof(a));
a=(15,20,25);
printf("comma operator %d \n",a);
//relational operators
printf("less than operator %d \n",a<b);
printf("greater than operator %d \n",a>b);
printf("equals operator %d \n",a==b);
printf("not equlas operator %d \n",a!=b);
//logical operators
printf("logical and (&&) operator %d \n",a&&b);
printf("logical or (||) operator %d \n",a||b);
printf("logical not (!) operator %d \n",!a);
//bitwise operators
printf("bitwise and(&) operator %d \n",a&b);
printf("bitwise or (|) operator %d \n",a|b);
printf("bitwise xor operator(^) %d \n",a^b);
printf("left shift operator (<< )%d \n",a<<2);
printf("right shift operator (>>)%d \n",a>>2);
}
Output:
b. Write a simple program that converts one given data type to another using auto
conversion and casting. Take the values from standard input.
#include<stdio.h>
#include<conio.h>
void main()
{
int i,x;
float f;
double p;
short s;
printf("enter integer value\n");
scanf("%d",&i);
printf("enter float value\n");
scanf("%f",&f);
p=i; // implicit conversion
printf("implicit value is %lf\n",p);
x= (int) f; // Explicit conversion
printf("Explicit value is %d\n",x);
}
Output:
enter integer value
24
enter float value
45.87
implicit value is 24.000000
Explicit value is 45
Simple Numeric Problems:
a. Write a program for finding the max and min from the three numbers.
#include <stdio.h>
void main()
{
int num1, num2, num3, max,min;
printf("Enter three numbers: ");
scanf("%d%d%d", &num1, &num2, &num3);
if(num1 > num2)
{
if(num1 > num3)
max = num1;
else
max = num3;
}
else if(num2 > num3)
max = num2;
else
max = num3;
//finding min
if(num1 < num2)
{
if(num1 < num3)
min= num1;
else
min = num3;
}
else if(num2 < num3)
min = num2;
else
min = num3;
printf("Maximum among all three numbers = %d\n", max);
printf("Minimum among all three numbers = %d", min);
}
b. Write the program for the simple, compound interest.
#include <stdio.h>
#include<math.h>
void main()
{
float si,p,t,r;
printf("enter principle amount,time,rate");
scanf("%f%f%f",&p,&t,&r);
si=(p*t*r)/100;
printf("simple interest is %f",si);
// Calculating compound Interest
double Amount = p * ((pow((1 + r / 100),t)));
double CI = Amount - p;
printf("Compound Interest is : %lf",CI);
}
c. Write a program that declares Class awarded for a given percentage of marks, where
mark <40%= Failed, 40% to <60% = Second class, 60% to <70%=First class, >= 70% =
Distinction. Read percentage from standard input.
#include<stdio.h>
void main()
{
int per;
printf("\n Enter percentage :");
scanf("%d", & per);
if (per >=70)
printf("\n You got Distinction");
else if ((per>=60)&&(per<70))
printf("\nYou got First Class");
else if ((per>=40)&&(per<60))
printf("\n You got Second Class");
else
printf("\n You got Fail");
}
d. Write a program that prints a multiplication table for a given number and the number
of rows in the table. For example, for a number 5 and rows = 3, the output should be:
5x1=5
5 x 2 = 10
5 x 3 = 15
#include<stdio.h>
void main()
{
int n,r,i,m;
printf("\n Enter number for multiplication table :");
scanf("%d", &n);
printf("\n Enter number of rows:");
scanf("%d", &r);
for(i=1;i<=r;i++)
{
m=n*i;
printf("%d x %d = %d\n",n,i,m);
}
}
e. Write a program that shows the binary equivalent of a given positive number between 0
to 255.
#include <stdio.h>
void main()
{
int n, bin,temp,rem, j,i;
printf("Enter a decimal number: ");
scanf("%d", &n);
for(i=1;i<n;i++)
{
bin = 0;
temp=i;
j=1;
while (temp!=0)
{
rem = temp % 2;
temp /= 2;
bin += rem * j;
j *= 10;
}
printf("%d in decimal = %lld in binary\n", i, bin);
}}
Expression Evaluation:
a. A building has 10 floors with a floor height of 3 meters each. A ball is dropped from the
top of the building. Find the time taken by the ball to reach each floor. (Use the formula s
= ut+(1/2)at^2 where u and a are the initial velocity in m/sec (= 0) and acceleration in
m/sec^2 (= 9.8 m/s^2)).
#include<stdio.h>
#include<math.h>
main()
{
int s=30,u=0,r,t;
float a=9.8;
r=2*s/a;
t=sqrt(r);
printf(“time taken is%d”,t);
}
b. Write a C program, which takes two integer operands and one operator from the user,
performs the operation and then prints the result. (Consider the operators +,-,*, /, % and
use Switch Statement)
#include<stdio.h>
void main()
{
int a, b, result;
char ch;
printf("1.(+) Addition\n");
printf("2.(-)subtraction\n");
printf("3.(*) Multiplication\n");
printf("4.(/) Division\n");
printf("5.(mod) Modulus\n");
printf("Enter your operator(+, -, /, *, mod)\n");
scanf("%c ",&ch);
printf("Enter the values of a and b\n");
scanf("%d %d",&a,&b);
switch(ch)
{
case '+': result = a + b;
break;
case '-': result = a - b;
break;
case '*': result = a * b;
break;
case '/': result = a / b;
break;
case '%': result = a % b;
break;
default: printf("Invalid operator");
}
printf("%c of two numbers is %d", ch,result);
}
c. Write a program that finds if a given number is a prime number.
#include<stdio.h>
void main()
{
int n, i, count= 0;
printf("Enter a positive integer: ");
scanf("%d", &n);
for (i = 2; i < n; ++i)
{
if (n % i == 0)
count=1;
}
if (count == 0)
printf("%d is a prime number.", n);
else
printf("%d is not a prime number.", n);
}
d. Write a C program to find the sum of individual digits of a positive integer and test
given number is palindrome.
#include <stdio.h>
void main()
{
int n,sum=0,r,rev=0,temp;
printf("enter a +ve integer");
scanf("%d",&n);
temp=n;
while(n>0)
{
r=n%10;
rev=rev*10+r;
sum=sum+r;
n=n/10;
}
printf("sum of individual digits of a positive integer is %d\n",sum);
printf("%d reverse number is %d\n",temp,rev);
if(temp==rev)
printf(“The given number is palindrom\n”);
}
e. A Fibonacci sequence is defined as follows: the first and second terms in the sequence
are 0 and 1. Subsequent terms are found by adding the preceding two terms in the
sequence. Write a C program to generate the first n terms of the sequence.
#include<stdio.h>
void main()
{
int i, n, t1 = 0, t2 = 1, nt;
printf(“Enter the number of terms:”);
scanf(“%d”, &n);
printf(“Fibonacci Series:”);
for (i = 1; i <= n; ++i)
{
printf(“%d\t”, t1);
nt= t1 + t2;
t1 = t2;
t2 = nt;
}
}
f. Write a C program to generate all the prime numbers between 1 and n, where n is a
value supplied by the user.
#include<stdio.h>
void main()
{
int n, i,j, count;
printf("Enter a positive integer: ");
scanf("%d", &n);
printf("prime numbers upto %d\n",n);
for(j=1;j<n;j++)
{
count=0;
for (i = 2; i < j;++i)
{
if (j % i == 0)
count=1;
}
if (count == 0)
printf("%d \n", j);
}
}
g. Write a C program to find the roots of a Quadratic equation.
#include <math.h>
#include <stdio.h>
void main()
{
double a, b, c, D, r1, r2;
printf("Enter coefficients a, b and c: ");
scanf("%lf %lf %lf", &a,&b,&c);
D = b * b - 4 * a * c;
if (D > 0)
{
r1 = (-b + sqrt(D)) / (2 * a);
r2 = (-b - sqrt(D)) / (2 * a);
printf("root1 = %.2lf and root2 = %.2lf", r1, r2);
}
else if (D == 0)
{
r1 = r2 = -b / (2 * a);
printf("root1 = root2 = %.2lf;", r1);
}
else
printf("roots are imaginary");
}