Imp Practice Program
Imp Practice Program
#include <stdio.h>
int main() {
int a, b;
printf("Please enter any two numbers\t");
scanf("%d%d", &a, &b);
printf("before swapping, the values are %d and %d\n", a, b);
a = a+ b;
b = a-b;
a = a- b;
printf("after swapping, the values are %d and %d", a, b);
return 0;
}
Output:
Please enter any two numbers 23 45
before swapping, the values are 23 and 45
after swapping, the values are 45 and 23
DATATYPES:
#include <stdio.h>
int main() {
int a=56;
float b = 89.4;
char c = 'y';
long d = 67883542;
double e = 34.5e10;// check the representation
char sname[20]=”trees are tall”;
printf("%d %f %c %ld %lf %s", a, b, c, d, e, sname);
return 0;
}
OUTPUT:
56 89.400002 y 67883542 345000000000.000000 trees are tall
COMPOUND INTEREST:
#include <stdio.h>
#include<math.h>
int main()
{
int P, r, n, t;
double CI;
printf("enter principal amount\n");
scanf("%d",&P);
printf("enter rate of interest\n");
scanf("%d", &r);
printf("enter number of times\n");
scanf("%d",&n);
printf("enter time in years\n");
scanf("%d",&t);
CI = P * pow((1 + r/n),n*t)-P;
printf("the Compound Interest Value is %lf\n", CI);
}
OUTPUT:
nter principal amount
4587892
enter rate of interest
3
enter number of times
2
enter time in years
5
the Compound Interest Value is 4693413516.000000
CELCIUS TO FARENHEIT:
// Online C compiler to run C program online
#include <stdio.h>
int main() {
float cel, farH;
printf("please enter temperature in celcius\n");
scanf("%f", &cel);
farH = cel * 1.8 + 32;
printf("the temperature in farenheit is %e F",farH);
return 0;
}
OUTPUT:
please enter temperature in celcius
98
the temperature in farenheit is 2.084000e+02 F
MATH OPERATORS:
#include<stdio.h>
#include<conio.h>
int main()
{
int a, b;
char ch;
clrscr();
printf("lets use our math operators\n + , -, *,/ \n");
printf("enter any of the above\t");
scanf("%c",&ch);
printf("enter any two numbers\t");
scanf("%d%d", &a,&b);
if (ch=='+')
printf("the sum is %d", a + b);
else if(ch=='-')
printf("the difference is %d", a-b);
else if(ch=='*')
printf("the product is %d", a * b);
else if(ch=='/')
printf("the quotient is %d", a/b);
else if(ch=='%')
printf("the remainder is %d", a%b);
else
printf("Invalid operator");
getch();
}
OUTPUT:
GREATEST OF 3 NUMBERS
#include<stdio.h>
#include<conio.h>
int main()
{
int a, b, c;
clrscr();
printf("lets check the greatest of three numbers\n");
printf(“enter 3 numbers\n”);
scanf("%d%d%d",&a,&b,&c);
if (a>=b && a>=c)
printf("%d is greater", a);
else if (b>=a && b>=c)
printf("%d is greater", b);
else
printf("%d is greater", c);
getch();
return 0;
}
Output:
Lets check the greatest of three numbers
enter 3 numbers
23 45 12
45 is greater
NESTED IF:
#include <stdio.h>
int main()
{
int a, b, c;
printf("lets check the greatest of three numbers\n");
printf(“enter 3 numbers\n”);
scanf("%d%d%d",&a,&b,&c);
if (a>b)
if(a>c)
printf("%d is greatest ", a);
else
printf("%d is greatest", c);
else
if (b>c)
printf("%d is greatest", b);
else
printf("%d is greatest", c);
return 0;
}
Output:
lets check the greatest of three numbers
enter 3 numbers
23 12 45
45 is greatest
SWITCH CASE:
#include<stdio.h>
int main()
{
char opertor;
int number1, number2;
printf(" Please select any ARITHMETIC OPERATOR You wish!\n");
scanf("%c",&opertor);
switch (opertor)
{
case '+':
printf("Addition of two numbers is: %d", number1 + number2);
break;
case '-':
printf("Subtraction of two numbers is: %d", number1 - number2);
break;
case '*':
printf("Multiplication of two numbers is: %d", number1 * number2);
break;
case '/':
printf("Division of two numbers is: %d", number1/number2);
break;
case '%':
printf("Modulus of two numbers is: %d",number1%number2);
break;
default:
printf("You have entered Wrong operator\n");
printf("Please enter the Correct operator such as +, -, *, /, %%");
break;
}
return 0;
}
#include <stdio.h>
int main()
{
int x;
x=1;
while(x<=10)
{
printf("%d\t",x);
x++;
}
return 0;
}
OUTPUT:
1 2 3 4 5 6 7 8 9 10
#include <stdio.h>
int main()
{
int x;
x=10;
while(x>=1)
{
printf("%d\t",x);
x--;
}
return 0;
}
OUTPUT:
10 9 8 7 6 5 4 3 2 1
#include <stdio.h>
int main()
{
int r= 1;
while(r<=5)
{
printf("welcome to loops\n");
r++;
return 0;
}
welcome to loops
welcome to loops
welcome to loops
welcome to loops
welcome to loops
#include <stdio.h>
int main()
{
int x = 1;
printf("odd numbers from 1 to 50\n");
while(x<=50)
{
printf("%d\t",x);
x += 2;
return 0;
}
OUTPUT:
odd numbers from 1 to 50
1 3 5 7 9 11 13 15 17 19 21 23 25
27 29 31 33 35 37 39 41 43 45 47 49
#include <stdio.h>
int main()
{
int num, n=1;
printf("enter any number\t");
scanf("%d",&num);
while(n<=10)
{
printf("%d X %d = %d\n", num, n, num*n);
n++;
}
return 0;
}
OUTPUT:
enter any number 7
7 X 1 = 7
7 X 2 = 14
7 X 3 = 21
7 X 4 = 28
7 X 5 = 35
7 X 6 = 42
7 X 7 = 49
7 X 8 = 56
7 X 9 = 63
7 X 10 = 70
PRINTING PATTERNS
#include <stdio.h>
#include<conio.h>
int main()
{
int x=1;
clrscr();
while(x<=10)
{
printf("1/%d\t",x);
x++;
}
getch();
return 0;
}
OUTPUT:
1/1 1/2 1/3 1/4 1/5
1/6 1/7 1/8 1/9 1/10
#include <stdio.h>
#include<conio.h>
int main() {
char ch='A'; clrscr();
while(ch<=90)
{
printf("%c\t",ch);
ch++;
}
getch();
return 0;
}
Output:
A B C D E F G H I J K L M
N O P Q R S T U V W X Y Z
#include <stdio.h>
#include<conio.h>
int main() {
int x = 10; clrscr();
while(x<=100)
{
printf("%d\t",x);
x+=10;
}
getch();
return 0;
}
10 20 30 40 50 60 70 80 90 100
#include <stdio.h>
#include<conio.h>
int main() {
char ch='y';
char names[20];
clrscr();
while(ch=='y')
{
printf("enter names ");
scanf("%s",names);
printf("want to contnue? ");
scanf(" %c",&ch);
}
getch();
return 0;
}
Output:
enter names hari
want to contnue? y
enter names jaya
want to contnue? y
enter names mani
want to contnue? y
enter names praveen
want to contnue? n
CONTINUE:
#include <stdio.h>
#include<conio.h>
int main() {
int d=0;
while(d<=10)
{ d++;
if(d==5)
continue;
printf("%d\t ",d);
}
getch();
return 0;
}
OUTPUT:
1 2 3 4 6 7 8 9 10 11
INCREMENT AND DECREMENT OPERATORS:
#include <stdio.h>
#include<conio.h>
int main() {
int a=3,b =2, c;
c = ++a + ++b;
printf("%d\t",c);
a=3; b=2;
c = ++a + b++;
printf("%d\t",c);
a=3; b=2;
c = a++ + b++;
printf("%d\t",c);
getch();
return 0;
}
Output:
7 6 5
decrement:
#include <stdio.h>
#include<conio.h>
int main() {
int a=3,b =2, c;
c = --a + --b;
printf("%d\t",c);
a=3; b=2;
c = --a + b--;
printf("%d\t",c);
getch();
return 0;
output:
3 4
Bitwise operators:
left shift and right shift operators:
#include <stdio.h>
#include<conio.h>
int main() {
int s=9;
printf("%d\n",s<<2);
printf("%d\n",s>>2);
getch();
return 0;
}
OUTPUT:
36
2
Explanation:
(9) 10 = (00001001)2 23 + 20 8+1 =9
9 << 2 = (00100100)2 25 + 22 32+4 =36
9>>2 = (00000010)2 21 2
XOR operator:
#include <stdio.h>
#include<conio.h>
int main() {
int s=9;
printf("%d\n",s^2);
getch();
return 0;
}
Output:
11
//tilde operator ~
#include <stdio.h>
#include<conio.h>
int main() {
int s=9;
printf("%d\n",~s);
getch();
return 0;
}
Output:
-10
FACTORIAL:
//factorial
#include<stdio.h>
#include<conio.h>
int main()
{
int k,num,fact=1;
clrscr();
printf("enter any number\t");
scanf("%d",&num);
for(k=num;k>=1;k--)
fact = fact*k;
printf("the factorial is %d",fact);
getch();
return 0;
}
OUTPUT
REVERSE OF A NUMBER
#include<stdio.h>
#include<conio.h>
int main()
{
int k,rev=0,rem, num;
printf("enter any number\t");
scanf("%d",&num);
while(num!=0)
{
rem = num%10;
rev = rev*10 + rem;
num= num/10;
}
printf("the reverse is %d",rev);
getch();
return 0;
}