0% found this document useful (0 votes)
15 views17 pages

Imp Practice Program

The document contains various C programming examples covering topics such as swapping numbers, data types, compound interest calculation, temperature conversion, checking for vowels, quadratic equation roots, math operators, and control structures like loops and conditionals. Each example includes code snippets along with sample outputs demonstrating the functionality of the code. Additionally, it highlights concepts like type conversion, bitwise operators, and factorial calculation.

Uploaded by

ajayyadavmamidi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views17 pages

Imp Practice Program

The document contains various C programming examples covering topics such as swapping numbers, data types, compound interest calculation, temperature conversion, checking for vowels, quadratic equation roots, math operators, and control structures like loops and conditionals. Each example includes code snippets along with sample outputs demonstrating the functionality of the code. Additionally, it highlights concepts like type conversion, bitwise operators, and factorial calculation.

Uploaded by

ajayyadavmamidi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 17

SWAPPING OF 2 NUMBERS:

#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

CHECKING FOR VOWEL:


#include <stdio.h>
#include<ctype.h>
int main() {
char ch;
printf("enter any alphabet between A to Z\t");
scanf("%s", &ch);
ch=toupper(ch);
if (ch=='A' || ch=='E'||ch=='I'||ch=='O'||ch=='U')
printf("it is a vowel");
else
printf("it is not a vowel");
return 0;
}
ROOTS OF QUADRATIC EQUATION:

// Online C compiler to run C program online


#include <stdio.h>
#include<math.h>
#include<conio.h>
int main() {
int a, b, c, d;
float r1, r2;
clrscr();
printf("lets calculate the roots of quadratic equation\n");
printf("enter value for a \t");
scanf("%d",&a);
printf("enter value for b \t");
scanf("%d",&b);
printf("enter value for c \t");
scanf("%d",&c);
//discriminant calculation
d = b * b - 4 * a * c;
if (d==0)
{
printf("the roots are real and equal\t");
r1=r2=-b/2*a;
printf("the roots are %f\t%f", r1, r2);
}
else if (d>0)
{
r1 = (-b + sqrt(d))/2*a;
r2 = (-b - sqrt(d))/2*a;
printf("the roots are %f\t%f", r1, r2);
}
else
printf("the roots are imaginary");
getch();
return 0;
}
Output:
lets calculate the roots of quadratic equation
enter value for a 1
enter value for b 2
enter value for c 1
the roots are real and equal the roots are -1.000000 -1.000000

lets calculate the roots of quadratic equation


enter value for a 2
enter value for b 8
enter value for c 1
the roots are -0.516685-15.483315
lets calculate the roots of quadratic equation
enter value for a 2
enter value for b 4
enter value for c 3
the roots are imaginary

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:

lets use our math operators


+ , -, *,/
enter any of the above -
enter any two numbers 34
90
the difference is -56

lets use our math operators


+ , -, *,/
enter any of the above *
enter any two numbers 3
6
the product is 18
lets use our math operators
+ , -, *,/
enter any of the above %
enter any two numbers 4
2
the remainder is 0

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

TYPE CONVERSION AND TYPE CASTING


int main()
{
int a = 15, b = 2;
float c = a;// type conversion from 15 to 15.0

printf("%d\n", a/b);// integer quotient

printf("%f\n", c/b);//float quotient-no data loss

printf("%f", (float)a/b);//type casting from int to float


return 0;
}
OUTPUT:
7
7.500000
7.500000
CONDITIONAL IF:
TERNARY OPERATOR:
#include <stdio.h>
int main()
{
// NOTE THE ? : operator
int a = 15, b = 2;
a>b? printf("%d is greater", a):printf("%d is greater", b);
return 0;
}
Output:
15 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);

printf("\n Please Enter two values to perform Arithmetic Operations\n");


scanf("%d %d",&number1,&number2);

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

BREAK AND CONTINUE:


// break
#include <stdio.h>
#include<conio.h>
int main() {
int d=0;
while(d<=10)
{ d++;
if(d==5)
break;
printf("%d ",d);
}
getch();
return 0;
}
Output:
1234

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

enter any number 7


the factorial is 5040

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;
}

enter any number 457


the reverse is 754

You might also like