3.1 Conditional Statement
āĻāύā§āĻĄāĻŋāĻļāύāĻžāϞ āϏā§āĻā§āĻāĻŽā§āύā§āĻ (ā§Ē)
āĻļā§āύā§āϝ, āϧāύāĻžāϤā§āĻŽāĻ, āĻāĻŖāĻžāϤā§āĻŽāĻ - āϏāĻāĻā§āϝāĻžāϰ āĻāĻŋāĻšā§āύ āύāĻŋāϰā§āĻŖā§
[1.1] āĻāĻāĻāĻŋ āĻĒā§āϰā§āĻŖāϏāĻāĻā§āϝāĻž āĻļā§āύā§āϝ āĻāĻŋāύāĻž āύāĻŋāϰā§āĻŖā§ āĻāϰāĨ¤ (Similar)
[1.2] āĻāĻāĻāĻŋ āĻĒā§āϰā§āĻŖāϏāĻāĻā§āϝāĻž āϧāύāĻžāϤā§āĻŽāĻ āύāĻž āĻāĻŖāĻžāϤā§āĻŽāĻ āύāĻŋāϰā§āĻŖā§ āĻāϰāĨ¤ (Similar)
[1] āĻāĻāĻāĻŋ āĻĒā§āϰā§āĻŖāϏāĻāĻā§āϝāĻž āĻļā§āύā§āϝ āύāĻž āϧāύāĻžāϤā§āĻŽāĻ āύāĻž āĻāĻŖāĻžāϤā§āĻŽāĻ āύāĻŋāϰā§āĻŖā§ āĻāϰāĨ¤
[2] āĻŦā§āϏ āĻ āύā§āϝāĻžā§ā§ āĻāĻāĻāύ āĻŦā§āϝāĻžāĻā§āϤāĻŋ āĻā§āĻāĻžāϰ āĻāĻŋāύāĻž āύāĻŋāϰā§āĻŖā§ āĻāϰāĨ¤
[3] āĻŦā§āϏ āĻ āύā§āϝāĻžā§ā§ āĻāĻžāĻāϰāĻŋāϰ āĻāĻŦā§āĻĻāύ āĻāϰāĻžāϰ āϝā§āĻā§āϝ āĻāĻŋāύāĻž āύāĻŋāϰā§āĻŖā§ āĻāϰāĨ¤ (āύāĻŋāϰā§āϧāĻžāϰāĻŋāϤ āĻŦā§āϏāϏā§āĻŽāĻž ā§§ā§Ž āĻĨā§āĻā§ ā§Šā§Ģ)
[4] āĻĻā§āĻāĻāĻŋ āĻāĻāĻŋāϞ āϏāĻāĻā§āϝāĻž āϏāĻŽāĻžāύ āĻāĻŋāύāĻž āύāĻŋāϰā§āĻŖā§ āĻāϰāĨ¤
āϏāĻŽāĻžāϧāĻžāύ
[1.1] āĻāĻāĻāĻŋ āĻĒā§āϰā§āĻŖāϏāĻāĻā§āϝāĻž āĻļā§āύā§āϝ āĻāĻŋāύāĻž āύāĻŋāϰā§āĻŖā§ āĻāϰāĨ¤
#include<stdio.h>
int main(){
int a = 0; // Output => Zero
//int a = 1; // No Output
if( a == 0 ){ // Testing Equlity Operator [==]
printf("Zero\n");
}
return 0;
}#include<stdio.h>
int main(){
int a = 0; // Output => Zero
//int a = 1; // Output => Not Zero
if( a == 0 ){ // Testing Equlity Operator [==]
printf("Zero\n");
}
if( a != 0 ){ // Testing Not Equlity Operator [!=]
printf("Not Zero\n");
}
return 0;
}#include<stdio.h>
int main(){
int a = 0; // Output => Zero
//int a = 1; // Output => Not Zero
if( a == 0 ){
printf("Zero\n");
}
else{ // Else block will execute when (a == 0) is false
printf("Not Zero\n");
}
return 0;
}#include<stdio.h>
int main(){
int a = 0; // Output => Zero
//int a = 1; // Output => Not Zero
if( a ){ // if evaluate 0 as FALSE and non-zero value as TRUE
// This block will execute when the value of a is non-zero
printf("Not Zero\n");
}
else{
// This block will execute when the value of a is 0
printf("Zero\n");
}
return 0;
}#include<stdio.h>
int main(){
int a = 0; // Output => Zero
//int a = 1; // Output => Not Zero
// Logical NOT Operator
if( !a ){ // when a=0, it means a is FALSE, !a means TRUE
// This block will execute when the value of a is 0
printf("Zero\n");
}
else{
// This block will execute when the value of a is non-zero
printf("Not Zero\n");
}
return 0;
}[1.2] āĻāĻāĻāĻŋ āĻĒā§āϰā§āĻŖāϏāĻāĻā§āϝāĻž āϧāύāĻžāϤā§āĻŽāĻ āύāĻž āĻāĻŖāĻžāϤā§āĻŽāĻ āύāĻŋāϰā§āĻŖā§ āĻāϰāĨ¤
#include<stdio.h>
int main(){
int a = 5; // Output => Positive
//int a = -5; // Output => Negative
//int a = 0; // No Output
if( a > 0 ){ // Testing Grater Then Operator [>]
printf("Positive\n");
}
if( a < 0 ){ // Testing Less Then Operator [<]
printf("Negative\n");
}
return 0;
}#include<stdio.h>
int main(){
int a = 5; // Output => Positive
//int a = -5; // Output => Negative
//int a = 0; // Output => Negative
if( a > 0 ){ // Testing Grater Then Operator [>]
printf("Positive\n");
}
else{ // Else block will execute when (a > 0) is false
printf("Negative\n");
}
return 0;
}#include<stdio.h>
int main(){
int a = 5; // Output => Positive
//int a = -5; // Output => Negative
//int a = 0; // Output => Positive
if( a >= 0 ){ // Testing Grater Then or Equal Operator [>=]
printf("Positive\n");
// Zero is neither Position nor Negative
// So This Solution is not perfect
}
else{ // Else block will execute when (a > 0) is false
printf("Negative\n");
}
return 0;
}[1] āĻāĻāĻāĻŋ āĻĒā§āϰā§āĻŖāϏāĻāĻā§āϝāĻž āĻļā§āύā§āϝ āύāĻž āϧāύāĻžāϤā§āĻŽāĻ āύāĻž āĻāĻŖāĻžāϤā§āĻŽāĻ āύāĻŋāϰā§āĻŖā§ āĻāϰāĨ¤
#include<stdio.h>
int main(){
int num = 5; // Output => Positive
//int num = -5; // Output => Negative
//int num = 0; // Output => Zero
if( num > 0 ){ // Testing Grater Then Operator [>]
printf("Positive\n");
}
if( num < 0 ){ // Testing Less Then Operator [<]
printf("Negative\n");
}
if( num == 0 ){ // Testing Equlity Operator [==]
printf("Zero\n");
}
return 0;
}#include<stdio.h>
int main(){
int num = 5; // Output => Positive
//int num = -5; // Output => Negative
//int num = 0; // Output => Zero
if( num > 0 ){ // Testing Grater Then Operator [>]
printf("Positive\n");
}
else if( num < 0 ){ // Testing Less Then Operator [<]
printf("Negative\n");
}
else if( num == 0 ){ // Testing Equlity Operator [==]
printf("Zero\n");
}
return 0;
}#include<stdio.h>
int main(){
int num = 5; // Output => Positive
//int num = -5; // Output => Negative
//int num = 0; // Output => Zero
if( num > 0 ){ // Testing Grater Then Operator [>]
printf("Positive\n");
}
else if( num < 0 ){ // Testing Less Then Operator [<]
printf("Negative\n");
}
else{
// This else block will execute when both previous conditions are false
printf("Zero\n");
}
return 0;
}#include<stdio.h>
int main(){
int num = 5; // Output => Positive
//int num = -5; // Output => Negative
//int num = 0; // Output => Zero
if( num == 0 ){ // Testing Equlity Operator [==]
printf("Zero\n");
}
else{
if( num > 0 ){ // Testing Grater Then Operator [>]
printf("Positive\n");
}
//else if( num < 0 ){ // Testing Less Then Operator [<]
else{
printf("Negative\n");
}
}
return 0;
}5
Positive
-3
Negative
0
Zero
[2] āĻŦā§āϏ āĻ
āύā§āϝāĻžā§ā§ āĻāĻāĻāύ āĻŦā§āϝāĻžāĻā§āϤāĻŋ āĻā§āĻāĻžāϰ āĻāĻŋāύāĻž āύāĻŋāϰā§āĻŖā§ āĻāϰāĨ¤
Test Voting Eligibility 1 (only if solution)
#include<stdio.h>
int main(){
int age = 20; // Output => Voter
//int age = 15; // Output => Not Voter
//int age = 18; // No Output
if(age > 18){
printf("Eligible to Vote\n");
}
if(age < 18){
printf("Not Eligible to Vote\n");
}
return 0;
}#include<stdio.h>
int main(){
int age = 20; // Output => Voter
//int age = 15; // Output => Not Voter
//int age = 18; // Output => Voter
if(age == 18){
printf("Eligible to Vote\n");
}
if(age > 18){
printf("Eligible to Vote\n");
}
if(age < 18){
printf("Not Eligible to Vote\n");
}
return 0;
}#include<stdio.h>
int main(){
int age = 20; // Output => Voter
//int age = 15; // Output => Not Voter
//int age = 18; // Output => Voter
if(age == 18 || age > 18){
printf("Eligible to Vote\n");
}
if(age < 18){
printf("Not Eligible to Vote\n");
}
return 0;
}#include<stdio.h>
int main(){
int age = 20; // Output => Voter
//int age = 15; // Output => Not Voter
//int age = 18; // Output => Voter
if(age >= 18){
printf("Eligible to Vote\n");
}
if(age < 18){
printf("Not Eligible to Vote\n");
}
return 0;
}Test Voting Eligibility 2 (if else solution)
#include<stdio.h>
int main(){
int age = 20; // Output => Voter
//int age = 15; // Output => Not Voter
//int age = 18; // Output => Voter
if(age >= 18){
printf("Eligible to Vote\n");
}
else if(age < 18){
printf("Not Eligible to Vote\n");
}
return 0;
}#include<stdio.h>
int main(){
int age = 20; // Output => Voter
//int age = 15; // Output => Not Voter
//int age = 18; // Output => Voter
if(age >= 18){
printf("Eligible to Vote\n");
}
else{
printf("Not Eligible to Vote\n");
}
return 0;
}#include<stdio.h>
#define VOTE_AGE 18 // Define VOTE_AGE macro
int main(){
int age = 20; // Output => Voter
//int age = 15; // Output => Not Voter
//int age = 18; // Output => Voter
if( age >= VOTE_AGE ){
printf("Eligible to Vote\n");
}
else{
printf("Not Eligible to Vote\n");
}
return 0;
}Test Voting Eligibility 3 (alternative solution)
#include<stdio.h>
int main(){
int age = 20; // Output => Voter
//int age = 15; // Output => Not Voter
//int age = 18; // Output => Voter
if(age < 18){
printf("Not Eligible to Vote\n");
}
else{
printf("Eligible to Vote\n");
}
return 0;
}#include<stdio.h>
int main(){
int age = 20; // Output => Voter
//int age = 15; // Output => Not Voter
//int age = 18; // Output => Voter
if(age <= 17){
printf("Not Eligible to Vote\n");
}
else{
printf("Eligible to Vote\n");
}
return 0;
}[3] āĻŦā§āϏ āĻ
āύā§āϝāĻžā§ā§ āĻāĻžāĻāϰāĻŋāϰ āĻāĻŦā§āĻĻāύ āĻāϰāĻžāϰ āϝā§āĻā§āϝ āĻāĻŋāύāĻž āύāĻŋāϰā§āĻŖā§ āĻāϰāĨ¤ (āύāĻŋāϰā§āϧāĻžāϰāĻŋāϤ āĻŦā§āϏāϏā§āĻŽāĻž ā§§ā§Ž āĻĨā§āĻā§ ā§Šā§Ģ)
#include<stdio.h>
int main(){
int age = 20; // Output => Eligible to apply
//int age = 15; // Output => Not Eligible to apply
//int age = 38; // Output => Not Eligible to apply
if(age >= 18){ // Testing Grater Then or Equal Operator [>=]
if( age <= 35 ){ // Testing Less Then or Equal Operator [<=]
printf("Eligible to apply\n");
}
else{
printf("Not eligible to apply\n");
}
}
else{
printf("Not eligible to apply\n");
}
return 0;
}#include<stdio.h>
int main(){
int age = 20; // Output => Eligible to apply
//int age = 15; // Output => Not Eligible to apply
//int age = 38; // Output => Not Eligible to apply
if(age >= 18 && age <= 35){ // Testing Logical AND Operator [&&]
// Print "Eligible to apply" if the age is between 18 and 35 inclusive
printf("Eligible to apply\n");
}
else{
// Print "Not Eligible to apply" if the age is outside the range
printf("Not eligible to apply\n");
}
return 0;
}#include<stdio.h>
int main(){
int age = 20; // Output => Eligible to apply
//int age = 15; // Output => Not Eligible to apply
//int age = 38; // Output => Not Eligible to apply
if(age <= 18){ // Testing Less Then or Equal Operator [<=]
printf("Not eligible to apply\n");
}
else if( age >= 35 ){ // Testing Grater Then or Equal Operator [>=]
printf("Not eligible to apply\n");
}
else{
printf("Eligible to apply\n");
}
return 0;
}#include<stdio.h>
int main(){
int age = 20; // Output => Eligible to apply
//int age = 15; // Output => Not Eligible to apply
//int age = 38; // Output => Not Eligible to apply
if(age <= 18 || age >= 35){ // Testing Logical OR Operator [||]
printf("Not eligible to apply\n");
}
else{
printf("Eligible to apply\n");
}
return 0;
}20
Eligible to apply
15
Not Eligible to apply
38
Not Eligible to apply
[4] āĻĻā§āĻāĻāĻŋ āĻāĻāĻŋāϞ āϏāĻāĻā§āϝāĻž āϏāĻŽāĻžāύ āĻāĻŋāύāĻž āύāĻŋāϰā§āĻŖā§ āĻāϰāĨ¤
#include<stdio.h>
int main(){
int a1 = 0, b1 = 1, a2 = 0, b2 = 1; // Output => Equal
//int a1 = 1, b1 = 1, a2 = 0, b2 = 0; // Output => Not Equal
if( a1 == a2 ){
if( b1 == b2 ){
printf("Equal\n");
}
else{
printf("Not Equal\n");
}
}
else{
printf("Not Equal\n");
}
return 0;
}#include<stdio.h>
int main(){
int a1 = 0, a1 = 1, a2 = 0, b2 = 1; // Output => Equal
//int a1 = 1, b1 = 1, a2 = 0, b2 = 0; // Output => Not Equal
if( a1 == a2 && b1 == b2 ){
printf("Equal\n");
}
else{
printf("Not Equal\n");
}
return 0;
}3 4 3 4
The complex numbers are equal
5 6 5 7
The complex numbers are not equal
Last updated