C Programming
C Programming
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;
scanf("%d", &ndays);
y = (int) ndays/365;
ndays = ndays-(365*y);
m = (int)ndays/30;
d = (int)ndays-(m*30);
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;
scanf("%d", &numbers[0]);
scanf("%d", &numbers[1]);
scanf("%d", &numbers[2]);
scanf("%d", &numbers[3]);
scanf("%d", &numbers[4]);
if((numbers[j]%2) != 0)
total += numbers[j];
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;
scanf("%d", &mno);
switch(mno) {
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];
scanf("%f", &numbers[0]);
scanf("%f", &numbers[1]);
scanf("%f", &numbers[2]);
scanf("%f", &numbers[3]);
scanf("%f", &numbers[4]);
if(numbers[j] > 0)
pctr++;
nctr++;
}
}
#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;
scanf("%d", &numbers[0]);
scanf("%d", &numbers[1]);
scanf("%d", &numbers[2]);
scanf("%d", &numbers[3]);
scanf("%d", &numbers[4]);
if((numbers[j]%2) != 0)
total += numbers[j];
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() {
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);
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));
}
#include <stdio.h>
int hrs;
int mins;
int tot_mins;
char line_text[50];
int main() {
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;
#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));
}
#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));
}
#include <stdio.h>
#include <stdlib.h>
int main(void){
int test(int x,int y,int 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 + 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;
}