Thursday 7 April 2022

comparing time using typedef, structures and function

 #include <stdio.h>


typedef struct time{
int hour;
int minute;
int second;
}timestruct;

void display(timestruct t1,timestruct t2)
{
    printf("First time is:%d:%d:%d\n",t1.hour,t1.minute,t1.second);
    printf("Second time is:%d:%d:%d\n",t2.hour,t2.minute,t2.second);
}
int timecomp(timestruct time1, timestruct time2)
{
    if (time1.hour>time2.hour)
    {
        return 1;
    }
    if(time1.hour<time2.hour)
    {
        return -1;
    }

    if (time1.minute>time2.minute)
    {
        return 1;
    }
    if(time1.minute<time2.minute)
    {
        return -1;
    }
    if (time1.second>time2.second)
    {
        return 1;
    }
    if(time1.second<time2.second)
    {
        return -1;

    }
    return 0;
}
   
    int main()
    {
timestruct time1={12,02,34};
timestruct time2={12,02,34};
display(time1,time2);
 int a=timecomp(time1,time2);
 if (a==0)
 {
printf("Both times are same\n");
 }
 else
 {
printf("The times are not same\n");
 }
 return 0;
}

Tuesday 22 February 2022

check whether the entered character is a lowercase or not in C

 #include <stdio.h>


int main(){

char ch;
printf("Enter the character\n");
scanf("%c",&ch);
if (ch>=97 && ch<=122)
{
    printf("The entered character is a lowercase\n");

}
else
printf("The entered character is not a lowercase\n");
 return 0;
}

convert from km to m and cm

 #include<stdio.h>

int main()
{
int A;
printf("Enter the distance in kilometers:\n");
scanf("%d",&A);
printf("The distance in meters is:%d mtr\n",A*1000);
printf("The distance in centimeters is:%d cms",A*100000);
}

Friday 18 February 2022

calculate income tax in C

 #include <stdio.h>


int main()
{
    float income, tax = 0;
    printf("Enter your income\n");
    scanf("%f", &income);
    if (income >= 250000 && income <= 500000)
    {
        tax = tax + 0.05 * (income - 250000);
    }

    if (income >= 500000 && income <= 1000000)
    {
        tax = tax + 0.2 * (income - 500000);
    }

    if (income >= 1000000 )
    {
        tax = tax + 0.3 * (income - 500000);
    }
    printf("Total tax you have to pay is:%f", tax);
    return 0;
}

Wednesday 16 February 2022

SwitchCase in C

 #include <stdio.h>


int main(){
int rating;
printf("Enter your rating:");
scanf("%d",&rating);
switch(rating)
{
    case 1:printf("Your rating is 1\n");
    break;
    case 2:printf("Your rating is 2\n");
    break;
    case 3:printf("Your rating is 3\n");
    break;
    case 4:printf("Your rating is 4\n");
    break;
    case 5:printf("Your rating is 5\n");
    break;
    default:printf("Your rating is invalid\n");
    break;
}


 return 0;
}

Even Odd number in C

 #include <stdio.h>


int main(){
int a;
printf("Enter a number:");
scanf("%d",&a);
if(a%2==0)
{
    printf("%d is an even number",a);
}
else{
    printf("%d is an odd number",a);
}

 return 0;
}

Expression in c

 #include <stdio.h>


int main(){
int x=2,y=3,z=3,k=1;
int result;
result=3*x/y-z+k;
printf("The value of result is:%d",result);
 return 0;
}