0% found this document useful (0 votes)
10 views24 pages

PS Portal

The document contains multiple C programming code snippets that perform various tasks, such as calculating kinetic and potential energy, determining electricity bills, validating ticket numbers, and checking attendance eligibility for exams. Each code snippet includes input handling, conditional statements, and output formatting. The programs cover a range of topics, including mathematical calculations, user input validation, and decision-making based on specified conditions.

Uploaded by

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

PS Portal

The document contains multiple C programming code snippets that perform various tasks, such as calculating kinetic and potential energy, determining electricity bills, validating ticket numbers, and checking attendance eligibility for exams. Each code snippet includes input handling, conditional statements, and output formatting. The programs cover a range of topics, including mathematical calculations, user input validation, and decision-making based on specified conditions.

Uploaded by

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

1)

#include<stdio.h>
#include<math.h>
int main()
{
int choice;
double mass,h,v,pe,ke;
scanf("%d",&choice);
if(choice==1)
{
scanf("%lf",&mass);
scanf("%lf",&v);
ke=0.5*mass*pow(v,2);//v*v if test case not passed
printf("%.2f joules\n",ke);
return 0;
}
else if(choice==2)
{
scanf("%lf",&mass);
scanf("%lf",&h);
pe=mass*9.8*h;
printf("%.2f joules\n",pe);
return 0;

}
else
{
printf("Invalid Choice");
}
return 0;
}
2)

#include<stdio.h>
int main()
{
int units,bill;
scanf("%d",&units);
if(units<=100)
{
bill=0;
}
else if(units<=200)
{
bill=(units-100)*2;

}
else if(units>=300)
{
bill=200*2+(units-300)*5;
}
printf("%d",bill);
return 0;
}
3)

#include <stdio.h>

int main() {
int d1, d2, d3, d4, d5;

scanf("%d", &d1);
scanf("%d", &d2);
scanf("%d", &d3);
scanf("%d", &d4);
scanf("%d", &d5);
if (d1 < 0 || d1 > 9) {
printf("Invalid input for the first integer\n");
return 0;
}
if (d2 < 0 || d2 > 9) {
printf("Invalid input for the second integer\n");
return 0;
}
if (d3 < 0 || d3 > 9) {
printf("Invalid input for the third integer\n");
return 0;
}
if (d4 < 0 || d4 > 9) {
printf("Invalid input for the fourth integer\n");
return 0;
}
if (d5 < 0 || d5 > 9) {
printf("Invalid input for the fifth integer\n");
return 0;
}

int ticketNumber = d1 * 10000 + d2 * 1000 + d3 * 100 + d4 * 10 + d5;


printf("Your ticket number is: %05d\n", ticketNumber);

return 0;
}

4)

#include<stdio.h>
int main()
{
int month;
scanf("%d",&month);
if(month==3)
{
printf("Sunrise: 6:30 AM\nSunset: 6:30 PM");
}
else if(month==9)
{
printf("Sunrise: 6:30 AM\nSunset: 6:00 PM");

}
else if(month==1||month==2||month==10||month==11||month==12)
{
printf("Sunrise: 7:00 AM\nSunset: 5:30 PM");
}
else if(month==4||month==5||month==6||month==7||month==8)
{
printf("Sunrise: 5:30 AM\nSunset: 7:30 PM");
}
else
{
printf("Invalid\n");
}
return 0;

5)

#include<stdio.h>
int main()
{
int wd,ab,at;
float per;
char med;
scanf("%d\n",&wd);
scanf("%d",&ab);
at=wd-ab;
per=((float)at/wd)*100;
if(per>=75)
{
printf("%.2f",per);
return 0;

}
else if(per<75)
{
scanf(" %c",&med);
if(med=='y'||med=='Y')
{
printf("%.2f",per);
}
else
{
printf("Not Eligible");
}

return 0;

6)
if in the constraints says don’t use inbuilt functions then for the formula like in the line >pow(r,2) for
this use just r*r

#include<stdio.h>
#include <math.h>
#define pi 3.14159
int main()
{
int choice;
double vol;
double ba,h,r;
scanf("%d",&choice);
switch(choice)
{
case 1:
scanf("%lf",&r);
scanf("%lf",&h);
vol=(1.0/3)*pi*pow(r,2)*h;//pow(r,2) or r*r ->if test case is not passed
printf("%.2f",vol);
break;
case 2:
scanf("%lf",&ba);
scanf("%lf",&h);
vol=(1.0/3)*ba*h;
printf("%.2f",vol);
break;
case 3:
scanf("%lf",&ba);
scanf("%lf",&h);
vol=ba*h;
printf("%.2f",vol);
break;
default:
printf("Invalid Choice\n");

}
return 0;
}

7)
#include<stdio.h>
int main()
{
int n,nn,nnn,res;
scanf("%d",&n);
if(n>=1&&n<=9)
{
nn=n*10+n;
nnn=nn*10+n;
res=n+nn+nnn;
printf("%d",res);
}
else
{
printf("Invalid\n");
}
return 0;
}
8)

#include<stdio.h>
int main(){
int tot_hrs,att_std;
float per;
scanf("%d",&tot_hrs);
scanf("%d",&att_std);
per=((float)att_std/tot_hrs)*100;
if(per>=75)
{
printf("You are allowed to sit in the exam");

}
else if(per>=66&& per<75)
{
printf("You are not allowed to sit in the exam\nApply for Condonation");

}else
{
printf("You are not allowed to sit in the exam\nNot eligible for Condonation");
}
return 0;}
9)

#include<stdio.h>
int main()
{
int steps,min;
scanf("%d",&steps);
scanf("%d",&min);
if(steps<5000)
{
printf("0\n");
}
else if(steps>=5000&&steps<10000)
{
printf("25\n");
}
else if(steps>=10000&&steps<15000)
{
printf("50\n");
}
else
{
printf("100\n");
}
float range=(float)steps/min;
printf("%.2f\n",range);
return 0;

}
10)

#include<stdio.h>
int main()
{
int age;
scanf("%d",&age);
if(age>=0&&age<=12)
{
printf("10");
return 0;
}
else if(age>=13&&age<=17)
{
printf("15");
return 0;
}
else if(age>=18&&age<=60)
{
printf("20");
return 0;
}
else{
printf("12");
}
return 0;
}
11)

#include<stdio.h>
int main()
{
int bas_sal;
float hra,da,sal;
scanf("%d",&bas_sal);
if(bas_sal<=10000)
{
hra=(20.0/100)*bas_sal;
da=(80.0/100)*bas_sal;

}
if(bas_sal>10000&&bas_sal<=20000)
{
hra=(25.0/100)*bas_sal;
da=(90.0/100)*bas_sal;

}
if(bas_sal>20000)
{
hra=(30.0/100)*bas_sal;
da=(95.0/100)*bas_sal;

}
sal= ((float)bas_sal)+hra+da;
printf("%.2f",sal);
return 0;

}
12)

#include<stdio.h>
int main(){
int amount;
char type;
scanf("%d\n",&amount);
scanf("%c",&type);
if(amount<=0)
{
printf("Invalid\n");
}
else if(type=='P'||type=='p')
{
if(amount<=1000)
{
printf("Withdrawl successful. Please take your money");
}
else
{
printf("Withdrawl amount exceeds the limit of your acount type");

}
else if(type=='R'||type=='r')
{
if(amount<=500)
{
printf("Withdrawal successful. Please take your money");
}
else
{
printf("Withdrawal amount exceeds the limit for your account type");

}
else
{
printf("Invalid\n");
}
return 0;
}
13)

#include<stdio.h>
#include<math.h>
int main()
{
//if test case is not passing use double instead of int
int base,ex,res;
scanf("%d",&base);
scanf("%d",&ex);
printf("%d\n",res=pow(base,ex));/*to priint double without
decimal point->printf("%.0lf\n",res=pow(base,ex))*/
return 0;
}
14)

#include <stdio.h>

int main() {
int h1, m1, h2, m2;
int start_minutes, end_minutes, duration_minutes;
int hours, minutes;

// Read start time


scanf("%d %d", &h1, &m1);
// Read end time
scanf("%d %d", &h2, &m2);

// Convert both times to minutes


start_minutes = h1 * 60 + m1;
end_minutes = h2 * 60 + m2;

// Calculate duration
duration_minutes = end_minutes - start_minutes;

// Convert back to hours and minutes


hours = duration_minutes / 60;
minutes = duration_minutes % 60;

// Output
printf("%d hours %d minutes\n", hours, minutes);

return 0;
}
15)

#include <stdio.h>

int main() {
int x1, y1, x2, y2;
int sum1, sum2, total;

scanf("%d %d", &x1, &y1);


scanf("%d %d", &x2, &y2);

sum1 = x2 - x1;
sum2 = y2 - y1;

if (sum1 < 0) sum1 = -sum1;


if (sum2 < 0) sum2 = -sum2;

total = sum1 + sum2;

printf("%d\n", total);
return 0;
}
16)

#include <stdio.h>

int main() {
float a, b, c;
scanf("%f %f %f", &a, &b, &c);

if (a < b && a < c) {


printf("Brand 1 offers the best deal.\n");
}
else if (b < a && b < c) {
printf("Brand 2 offers the best deal.\n");
}
else if (c < a && c < b) {
printf("Brand 3 offers the best deal.\n");
}
else {
printf("Multiple brands have the same price.\n");
}

return 0;
}
17)

#include<stdio.h>
int main()
{
int age,days,wage,sal;
char gender;
scanf("%d",&age);
if(age<18||age>40)
{
printf("Invalid\n");
return 0;
}
scanf(" %c",&gender);
scanf("%d",&days);
if(age>=18&&age<30)
{
if(gender=='m'||gender=='M')
{
wage=700;
}
else if(gender=='f'||gender=='F')
{
wage=750;
}
else
{
printf("Invalid\n");
return 0;
}

}
else if(age>=30&&age<=40)
{
if(gender=='m'||gender=='M')
{
wage=800;
}
else if(gender=='f'||gender=='F')
{
wage=850;
}
else
{
printf("Invalid\n");
return 0;
}
}
sal=days*wage;
printf("%d",sal);
return 0;
}

18)

#include<stdio.h>
int main()
{
int d;
scanf("%d",&d);
if(d<0||d>360)
{
printf("Invalid Input");
return 0;
}
if ((d >= 337 && d <= 360) || (d >= 0 && d <= 22))
{
printf("North");
}
else if(d>=23&&d<=67)
{
printf("Northeast");
}
else if(d>=68&&d<=112)
{
printf("East");
}
else if(d>=113&&d<=157)
{
printf("Southeast");
}
else if(d>=158&&d<=202)
{
printf("South");
}
else if(d>=203&&d<=247)
{
printf("Southwest");
}
else if(d>=248&&d<=292)
{
printf("West");
}
else if(d>=293&&d<=337)
{
printf("Northwest");
}
return 0;
}
19)

#include<stdio.h>
int main()
{
int n;
scanf("%d",&n);
if(n==0)
{
printf("Normal");
}
else if(n<0)
{
if(n>=-4&&n<=-1)
{
printf("Short Sightedness-Minimum");
}
else if(n>=-8&&n<=-5)
{
printf("Short Sightedness-Moderate");
}
else if(n>=-12&&n<=-9)
{
printf("Short Sightedness-High");
}
else if(n>=-16&&n<=-13)
{
printf("Short Sightedness-chrotic");
}
else
{
printf("Invalid");
return 0;
}
}
else if(n>0)
{
if(n>=1&&n<=4)
{
printf("Long Sightedness-Minimum");
}
else if(n>=5&&n<=8)
{
printf("Long Sightedness-Moderate");
}
else if(n>=9&&n<=12)
{
printf("Long Sightedness-High");
}
else if(n>=13&&n<=16)
{
printf("Long Sightedness-chrotic");
}
else
{
printf("Invalid");
return 0;
}

return 0;
}
20)
Write a C program to perform operations based on whether the two input characters are vowels or
consonants.
Accept two characters as input from the user.
Based on the type of characters:
• If both characters are vowels, calculate and display the sum of their ASCII values.
• If only one character is a vowel, calculate and display the difference of their ASCII values.
• If both characters are consonants, calculate and display the product of their ASCII values.
Display the appropriate message along with the result.
#include<stdio.h>
int main()
{
char a,b;
int sum;
scanf(" %c %c",&a,&b);
if((a=='a'||a=='e'||a=='i'||a=='o'||a=='u')&&(b=='a'||b=='e'||b=='i'||b=='o'||b=='u'))
{
sum=a+b;

}
else if((a=='a'||a=='e'||a=='i'||a=='o'||a=='u')||(b=='a'||b=='e'||b=='i'||b=='o'||b=='u'))
{
sum=a-b;
}
else
{
sum=a*b;
}
printf("%d",sum);
return 0;
}
21)WEIRD NOT WEIRD
#include <stdio.h>

int main() {
int number;
scanf("%d", &number);
if (number < 1 || number > 100) {
printf("Invalid\n");
}
else if (number % 2 != 0) {
// Odd number
printf("Weird\n");
}
else {
// Even number
if (number >= 2 && number <= 5) {
printf("Not Weird\n");
}
else if (number >= 6 && number <= 20) {
printf("Weird\n");
}
else {
printf("Not Weird\n");
}
}

return 0;
}

You might also like