C Programming Conditional Practice
12. ArithmeticOperator
// Write a program that takes two integers and displays a sum.
#include<stdio.h>
int main() {
int num1,num2,sum;
float avg;
printf(“Enter Two Numbers = “);
scanf(“%d %d”, &num1,&num2);
sum = num1+num2;
printf(“The Sum Is : %d \n”, sum);
avg = (float)sum/2; //type-casting
printf(“The average is : %.2f \n”,avg);
return 0; }
11. AreaCalculate
// Write a program that calculates the area of triangle.
#include<stdio.h>
int main()
{
float base,height,area;
printf(“Enter The Base : “);
scanf(“%f”, &base);
printf(“Enter The Height : “);
scanf(“%f”, &height);
area = 0.5 * base * height;
printf(“Area : %2f \n”, area);
return 0; }
10. Hexadecimal to Decimal
#include<stdio.h>
int main() {
int number;
printf(“Hexa-Decimal Number = “);
scanf(“%x”, &number);
printf(“Decimal Number : %d”, number);
getch(); }
09. Hexadecimal to Octal
#include<stdio.h>
int main() {
int number;
printf(“Hexa-Decimal Number = “);
scanf(“%x”, &number);
printf(“Octal Number : %o”, number);
getch(); }
08. Octal to Hexadecimal
#include<stdio.h>
int main() {
int number;
printf(“Octal Number = “);
scanf(“%o”, &number);
printf(“Hexa-Decimal Number : %x”, number);
getch(); }
07. Octal to Decimal
#include<stdio.h>
int main() {
int number;
printf(“Octal Number = “);
scanf(“%o”, &number);
printf(“Decimal Number : %d”, number);
getch(); }
06. Decimal to Hexadecimal
#include<stdio.h>
int main() {
int number;
printf(“Decimal Number = “);
scanf(“%d”, &number);
printf(“Hexa-Decimal Number : %x”, number);
getch(); }
05. Decimal to Octal
#include<stdio.h>
int main() {
int number;
printf(“Decimal Number = “);
scanf(“%d”, &number);
printf(“Octal Number : %o”, number);
getch(); }
04. Fahrenheit to Centigrade
// C = ( F – 32 )/1.8
#include<stdio.h>
int main() {
float C,F;
printf(“Enter Fahrenheit : “);
scanf(“%f”, &F);
C = ( F – 32 )/1.8;
printf(“Centigrade : %.2f \n”, C); }
03. Even and Odd
#include<stdio.h>
int main(){
int num;
printf(“Enter an integer number : “);
scanf(“%d”, &num);
if(num%2==0)
printf(“This is an Even Number.\n”);
if(num%2!=0)
printf(“This is an Odd Number. \n”); }
01. Age Height Weight
#include <stdio.h>
int main () {
char name [20];
int age;
double height;
double weight;
printf(“\nHello Sir, Assalamualaikum. \nI hope that you are pretty good today.”);
printf(“\nPlease type just four information and getting know something about you.”);
printf(“\n”);
printf (“\nEnter your name ( like-Jakaria) : “);
scanf(“%s”, &name);
printf (“\nEnter your age ( like-21 ) : “);
scanf(“%d”, &age);
printf (“\nEnter your hight ( like-5.6 ) : “);
scanf(“%lf”, &height);
printf (“\nEnter your weight ( like-58.4 ) : “);
scanf(“%lf”, &weight);
char person [] = “are so young person.”;
char very [] = “are very long than Jakaria.”;
char body [] = ” weight and body structure is really good.”;
printf(“\n\nYour beautiful name is : %s! I like your name. “, name, like);
printf(“\n\nYour young age is : %d years. you %s “, age, person);
printf(“\n\nYour correct height is : %.1lf fit. You %s “, height, very);
printf(“\n\nYour healthy body’s weight is : %.1lf kg. Your %s “, weight, body);
return 0; }
12. Coding With Variables
#include <stdio.h>
int main() {
//Student Information And Grade
int ID = 11858559;
int x = 17;
int y = 21;
int z = 11;
float CGPA = 1.67;
char Grade = ‘D’;
int Score = x+y+z;
//Printing Grade
printf(“Student Name : Sayed Ricky \n”);
printf(“Student ID : %d\n”, ID);
printf(“Obtained Marks : %d\n”, Score);
printf(“CGPA : %f\n”, CGPA);
printf(“Grade : %c\n”, Grade);
printf(“Comment : Poor”);
return 0; }
11. Simple Gaming
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
int guess, number, attempts = 0;
srand(time(0));
number = rand() % 100 + 1; // Generates a random number between 1 and 100
printf(“Welcome to the Guess the Number game!\n”);
printf(“I have chosen a number between 1 and 100. Can you guess it?\n”);
do {
printf(“Enter your guess: “);
scanf(“%d”, &guess);
attempts++;
if (guess > number) {
printf(“Too high! Try again.\n”);
} else if (guess < number) {
printf(“Too low! Try again.\n”);
} else {
printf(“Congratulations! You guessed the number in %d attempts.\n”, attempts);
}
} while (guess != number);
return 0; }
10. While_Loop
#include <stdio.h>
int main() {
int i=0;
do {
printf(“%d\n”, i);
i++; }
while(i<30); }
09. Leap Year
#include<stdio.h>
#include<conio.h>
int main ( ) {
int year;
printf(“Enter year”);
scanf(“%d”,& year);
if (year % 400==0)
printf(“%d is Leap Year”,year);
else if (year %100 == 0)
printf(“%d is not Leap Year”,year);
else if (year %4 == 0)
printf(“%d is Leap Year”,year);
else
printf(“%d is Leap Year”,year);
getch ( );
return 0; }
08. Complex Calculator
#include <stdio.h>
#include <stdlib.h>
// void funtion for calculator brain
void main_Calc(float number_one ,float number_two,char oparations){
if (oparations == ‘+’){
printf(“\nresult :%.2f”,number_one + number_two);
}else if(oparations == ‘-‘){
printf(“\nresult :%.2f”,number_one – number_two);
}else if(oparations == ‘*’){
printf(“\nresult :%.2f”,number_one * number_two);
}else if(oparations == ‘/’){
printf(“\nresult :%.2f”,number_one / number_two);
}else{
printf(“syntex erorr”); } }
//main function
int main(){
float num1 , num2;
char op,dis;
int x = 1;
while (x){
printf(“\n\nThis is a Easy Calculator. \n\nYou can do any types of calculation by using this calculator”);
printf(“\n\ninput number one : “);
scanf(“%f”,&num1);
printf(“\ninput number two :”);
scanf(“%f”,&num2);
printf(“\ninput oparator (+,-,*,/): “);
scanf(” %c”,&op);
main_Calc(num1,num2,op);
printf(“\n\nFor using calculator again type (yes) or (no) for exit: “);
scanf(” %c”,&dis);
if(dis == ‘yes’){
continue;
}else{
break; } }
return 0; }
07. Simple Calculator
#include<stdio.h>
int main() {
int num1,num2,result;
printf(“Enter Two Numbers : “);
scanf(“%d %d”, &num1,&num2);
result = num1 + num2;
printf(“The Sum Is : %d \n”, result);
result = num1 – num2;
printf(“The Sub Is : %d \n”, result);
result = num1 * num2;
printf(“The Mul Is : %d \n”, result);
result = num1 / num2;
printf(“The Div Is : %d \n”, result);
result = num1 % num2;
printf(“The Remainder Is : %d \n”, result);
return 0; }
06. Math problem
#include <math.h>
int main() {
int x1, y1, x2, y2;
printf(“First value of x and y: “);
scanf(“%d %d”, &x1,&y1);
printf(“Second value of x and y: “);
scanf(“%d %d”, &x2,&y2);
double distance;
distance = sqrt((x1-x2)*(x1-x2)+(y1+y2*(y1-y2)));
printf(“Distance = %.2lf\n”, distance);
return 0; }
05. ResultGrade
#include<stdio.h>
int main(){
float marks;
printf(“Enter your marks : “);
scanf(“%f”, &marks);
if(marks >=80)
printf(“A+\n”);
else if(marks >=70)
printf(“A\n”);
else if(marks >=60)
printf(“A-\n”);
else if(marks >=50)
printf(“B\n”);
else if(marks >=40)
printf(“C\n”);
else if(marks >=33)
printf(“D\n”);
else
printf(“Fail\n”);
return 0;
}
04. Decimal to Octal and Hexadecimal
#include<stdio.h>
void main()
{
int a;
printf(“\nDecimal number: “);
scanf(“%d”,&a);
printf(“\nOctal number: %o”,a);
printf(“\nHexadecimal number: %x”,a);
printf(“\n\n\n\n\nProgramming by Md Jakaria Nur!”);
printf(“\n\n\n\n”);
return 0;
}
03. Small Letter to Capital Letter
#include<stdio.h>
void main()
{
char n;
printf (“\nEnter any small character: “);
scanf(“%c”, &n);
printf(“\nYour typing capital charecter: %c”, n-32);
printf(“\n\n\n\n\nProgramming Md Jakaria Nur!”);
printf(“\n\n\n\n”);
return 0;
}
03. Small Letter to Capital Letter
#include
#include
#include
int main() {
int guess, number, attempts = 0;
srand(time(0));
number = rand() % 100 + 1; // Generates a random number between 1 and 100
printf(“Welcome to the Guess the Number game!\n”);
printf(“I have chosen a number between 1 and 100. Can you guess it?\n”);
do {
printf(“Enter your guess: “);
scanf(“%d”, &guess);
attempts++;
if (guess > number) {
printf(“Too high! Try again.\n”);
} else if (guess < number) {
printf(“Too low! Try again.\n”);
} else {
printf(“Congratulations! You guessed the number in %d attempts.\n”, attempts);
}
} while (guess != number);
return 0;
}
03. Small Letter to Capital Letter
#include <stdio.h>
void printF(int height, int width) {
for (int i = 0; i < height; ++i) {
if (i == 0 || i == height || i == height / 2) {
for (int j = 0; j < width; ++j) {
printf(“#”);
} printf(“\n”);
} else {
printf(“#\n”);
} } }
void printLargeC(int height, int width) {
for (int i = 0; i < height; ++i) {
if (i == 0 || i == height – 1) {
for (int j = 0; j < width; ++j) {
if (j >= width / 2) {
printf(“#”);
} else {
printf(” “); } }
printf(“\n”);
} else if (i == 1 || i == height – 2) {
for (int j = 0; j < width; ++j) {
if (j >= width / 2 – 1) {
printf(“#”);
} else {
printf(” “); } }
printf(“\n”);
} else if (i == 2 || i == height – 3) {
for (int j = 0; j < width; ++j) {
if (j >= width / 2 – 2) {
printf(“#”);
} else {
printf(” “); } }
printf(“\n”);
} else {
for (int j = 0; j < width; ++j) {
printf(“#”);
} printf(“\n”);
} } }
int main() {
printF(8, 5);
printf(“\n”);
printLargeC(6, 7);
return 0; }
#include <stdio.h>
#include <math.h>
int main() {
float a, b, c;
float discriminant, root1, root2, realPart, imaginaryPart;
printf(“Enter coefficients a, b and c: “);
scanf(“%f %f %f”, &a, &b, &c);
discriminant = b * b – 4 * a * c;
switch(discriminant > 0) {
case 1:
root1 = (-b + sqrt(discriminant)) / (2 * a);
root2 = (-b – sqrt(discriminant)) / (2 * a);
printf(“Roots are real and different: %.2f and %.2f”, root1, root2);
break;
case 0:
switch(discriminant < 0) {
case 1:
realPart = -b / (2 * a);
imaginaryPart = sqrt(-discriminant) / (2 * a);
printf(“Roots are complex and different: %.2f + %.2fi and %.2f – %.2fi”, realPart, imaginaryPart, realPart, imaginaryPart);
break;
case 0:
root1 = root2 = -b / (2 * a);
printf(“Roots are real and same: %.2f and %.2f”, root1, root2);
break;
}
break;
}
return 0;
}
Thank You!
Leave a Reply