Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
64 views

C Programming

The document contains 17 code snippets that demonstrate various ways to write C programs to perform tasks like: 1) Converting between seconds, hours, minutes and seconds. 2) Converting between days, years, months and days. 3) Summing odd values between input numbers. 4) Checking if two numbers are multiplied. 5) Getting the month name from a number. 6) Counting positive and negative numbers. 7) Averaging positive values. 8) Converting between temperatures and speeds. 9) Checking properties of input numbers. 10) Performing mathematical operations.

Uploaded by

Dhruva Mishra
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
64 views

C Programming

The document contains 17 code snippets that demonstrate various ways to write C programs to perform tasks like: 1) Converting between seconds, hours, minutes and seconds. 2) Converting between days, years, months and days. 3) Summing odd values between input numbers. 4) Checking if two numbers are multiplied. 5) Getting the month name from a number. 6) Counting positive and negative numbers. 7) Averaging positive values. 8) Converting between temperatures and speeds. 9) Checking properties of input numbers. 10) Performing mathematical operations.

Uploaded by

Dhruva Mishra
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 25

1.

Write a C program to convert a given integer (in seconds) to hours , minutes and seconds

#include <stdio.h>
int main() {
int sec, h, m, s;
printf("Input seconds: ");
scanf("%d", &sec);

h = (sec/3600);

m = (sec -(3600*h))/60;

s = (sec -(3600*h)-(m*60));

printf("H:M:S - %d:%d:%d\n",h,m,s);

return 0;
}
2. Write a C program to convert a given integer (in days) to years, months and days, assumes
that all months have 30 days and all years have 365 days.

#include <stdio.h>

int main() {

int ndays, y, m, d;

printf("Input no. of days: ");

scanf("%d", &ndays);

y = (int) ndays/365;

ndays = ndays-(365*y);

m = (int)ndays/30;

d = (int)ndays-(m*30);

printf(" %d Year(s) \n %d Month(s) \n %d Day(s)", y, m, d);

return 0;

}
3. Write a C program that read 5 numbers and sum of all odd values between them

#include <stdio.h>

int main() {

int j, numbers[5],total=0;

printf("\nInput the first number: ");

scanf("%d", &numbers[0]);

printf("\nInput the second number: ");

scanf("%d", &numbers[1]);

printf("\nInput the third number: ");

scanf("%d", &numbers[2]);

printf("\nInput the fourth number: ");

scanf("%d", &numbers[3]);

printf("\nInput the fifth number: ");

scanf("%d", &numbers[4]);

for(j = 0; j < 5; j++) {

if((numbers[j]%2) != 0)

total += numbers[j];

printf("\nSum of all odd values: %d", total);

printf("\n");

return 0;

}
4. Write a C program that reads two integers and checks whether they are multiplied or not

#include <stdio.h>

int main() {
int x, y;
printf("\nInput the first number: ");
scanf("%d", &x);
printf("\nInput the second number: ");
scanf("%d", &y);

if(x > y)
{
int temp;
temp = x;
x = y;
y = temp;
}

if((y % x)== 0)
{
printf("\nMultiplied!\n");
}
else
{
printf("\nNot Multiplied!\n");
}
5. Write a C program that reads an integer between 1 and 12 and print the month of the year in
English.

#include <stdio.h>

int main() {

int mno;

printf("\nInput a number between 1 to 12 to get the month name: ");

scanf("%d", &mno);

switch(mno) {

case 1 : printf("January\n"); break;

case 2 : printf("February\n"); break;

case 3 : printf("March\n"); break;

case 4 : printf("April\n"); break;

case 5 : printf("May\n"); break;

case 6 : printf("June\n"); break;

case 7 : printf("July\n"); break;

case 8 : printf("August\n"); break;

case 9 : printf("September\n"); break;

case 10 : printf("October\n"); break;

case 11 : printf("November\n"); break;

case 12 : printf("December\n"); break;

default : printf("Input a number between 1 to 12.");

return 0;

}
6. Write a C program that read 5 numbers and counts the number of positive numbers and
negative numbers.

#include <stdio.h>

int main() {

float numbers[5];

int j, pctr=0, nctr=0;

printf("\nInput the first number: ");

scanf("%f", &numbers[0]);

printf("\nInput the second number: ");

scanf("%f", &numbers[1]);

printf("\nInput the third number: ");

scanf("%f", &numbers[2]);

printf("\nInput the fourth number: ");

scanf("%f", &numbers[3]);

printf("\nInput the fifth number: ");

scanf("%f", &numbers[4]);

for(j = 0; j < 5; j++) {

if(numbers[j] > 0)

pctr++;

else if(numbers[j] < 0)

nctr++;

}
}

printf("\nNumber of positive numbers: %d", pctr);


7. Write a C program that read 5 numbers and counts the number of positive numbers and print
the average of all positive values

#include <stdio.h>
int main() {
float numbers[5],total=0, avg;
int j, pctr=0;
printf("\nInput the first number: ");
scanf("%f", &numbers[0]);
printf("\nInput the second number: ");
scanf("%f", &numbers[1]);
printf("\nInput the third number: ");
scanf("%f", &numbers[2]);
printf("\nInput the fourth number: ");
scanf("%f", &numbers[3]);
printf("\nInput the fifth number: ");
scanf("%f", &numbers[4]);
for(j = 0; j < 5; j++) {
if(numbers[j] > 0)
{
pctr++;
total += numbers[j];
}
}
avg = total/pctr;
printf("\nNumber of positive numbers: %d", pctr);
printf("\nAverage value of the said positive numbers: %.2f", avg);
printf("\n");
return 0;
8. Write a C program that read 5 numbers and sum of all odd values between them.

#include <stdio.h>

int main() {

int j, numbers[5],total=0;

printf("\nInput the first number: ");

scanf("%d", &numbers[0]);

printf("\nInput the second number: ");

scanf("%d", &numbers[1]);

printf("\nInput the third number: ");

scanf("%d", &numbers[2]);

printf("\nInput the fourth number: ");

scanf("%d", &numbers[3]);

printf("\nInput the fifth number: ");

scanf("%d", &numbers[4]);

for(j = 0; j < 5; j++) {

if((numbers[j]%2) != 0)

total += numbers[j];

printf("\nSum of all odd values: %d", total);

printf("\n");

return 0;

}
9. Write a program that converts Centigrade to Fahrenheit

#include <stdio.h>

float temp_f;

float temp_c;

char line_text[50];

int main() {

printf("Input a temperature (in Centigrade): ");

fgets(line_text, sizeof(line_text), stdin);

sscanf(line_text, "%f", &temp_c);

temp_f = ((9.0 / 5.0) * temp_c) + 32.0;

printf("%f degrees Fahrenheit.\n", temp_f);

return(0);

}
10. Write a C program that converts kilometres per hour to miles per hour

#include <stdio.h>
float kmph;
float miph;
char line_text[50];

int main()
{
printf("Input kilometers per hour: ");
fgets(line_text, sizeof(line_text), stdin);
sscanf(line_text, "%f", &kmph);

miph = (kmph * 0.6213712);


printf("%f miles per hour\n", miph);

return(0);
}
11.Write a C program to check two given integers, and return true if one of them is 30 or if their
sum is 30.

#include <stdio.h>
#include <stdlib.h>

int main(void){
int test(int x, int y);
printf("%d",test(5, 4));
printf("\n%d",test(4, 3));
printf("\n%d",test(1, 4));
}

int test(int x, int y)


{
return x == 5 || y == 5 || x + y == 5 || abs(x - y) == 5;
}
12.Write a C program that takes hours and minutes as input, and calculates the total number of
minutes.

#include <stdio.h>

int hrs;

int mins;

int tot_mins;

const int MINaHOUR = 60;

char line_text[50];

int main() {

printf("Input hours: ");

fgets(line_text, sizeof(line_text), stdin);

sscanf(line_text, "%d", &hrs);

printf("Input minutes: ");

fgets(line_text, sizeof(line_text), stdin);

sscanf(line_text, "%d", &mins);

tot_mins = mins + (hrs * MINaHOUR);

printf("Total: %d minutes.\n", tot_mins);

return(0);
}
13. Write a C program to integral quotient and remainder of a division

#include<stdio.h>
#include<stdlib.h>

int main ()
{
long int n,d;
ldiv_t result;

printf("\n Input numerator : ");


scanf("%ld",&n);
printf(" Input denominator : ");
scanf("%ld",&d);
result = ldiv (n,d);
printf (" quotient = %ld, remainder = %ld.\n\n",result.quot,result.rem);
return 0;
}
14. Write a C program to compute the sum of the two given integers. If the sum is in the range
10..20 inclusive return 30.

#include <stdio.h>
#include <stdlib.h>

int main(void){
printf("%d",test(12, 17));
printf("\n%d",test(2, 17));
printf("\n%d",test(22, 17));
printf("\n%d",test(20, 0));
}

int test(int a, int b)


{
return a + b >= 10 && a + b <= 20 ? 30 : a + b;
}
15. Write a C program that accept two integers and return true if either one is 5 or their sum or
difference is 5.

#include <stdio.h>
#include <stdlib.h>

int main(void){
int test(int x, int y);
printf("%d",test(5, 4));
printf("\n%d",test(4, 3));
printf("\n%d",test(1, 4));
}

int test(int x, int y)


{
return x == 5 || y == 5 || x + y == 5 || abs(x - y) == 5;
}
16. Write a C program to check whether two or more non-negative given integers have the same
rightmost digit

#include <stdio.h>
#include <stdlib.h>
int main(void){
int test(int x,int y,int z);

printf("%d",test(11, 21, 31));


printf("\n%d",test(11, 22, 31));
printf("\n%d",test(11, 22, 33));
}
int test(int x, int y, int z)
{
return x % 10 == y % 10 || x % 10 == z % 10 || y % 10 == z % 10;
}
17. Write a C program to find the larger from two given integers. However if the two integers
have the same remainder when divided by 5, then the return the smaller integer. If the two
integers are the same, return 0.
18. Write a C program to check whether it is possible to add two integers to get the third integer
from three given integers.

#include <stdio.h>
#include <stdlib.h>
int main(void){
int test(int x, int y, int z);
printf("%d",test(1, 2, 3));
printf("\n%d",test(4, 5, 6));
printf("\n%d",test(-1, 1, 0));
}
int test(int x, int y, int z)
{
return x == y + z || y == x + z || z == x + y;
}
19. Write a C program to check whether y is greater than x, and z is greater than y from three
given integers x,y,z

#include <stdio.h>
#include <stdlib.h>
int main(void){
int test(int x, int y, int z);
printf("%d",test(1, 2, 3));
printf("\n%d",test(4, 5, 6));
printf("\n%d",test(-1, 1, 0));
}
int test(int x, int y, int z)
{
return x < y && y < z;
}
20. Write a C program to check two given integers, each in the range 10..99. Return true if a
digit appears in both numbers, such as the 3 in 13 and 33.

#include <stdio.h>
#include <stdlib.h>

int main(void){
int test(int x, int y);
printf("%d",test(11, 21));
printf("\n%d",test(11, 20));
printf("\n%d",test(10, 10));
}
int test(int x, int y)
{
return x / 10 == y / 10 || x / 10 == y % 10 || x % 10 == y / 10 || x % 10 == y % 10;
}

You might also like