0% found this document useful (0 votes)
4 views4 pages

CS Programming

Uploaded by

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

CS Programming

Uploaded by

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

PROBLEM 1. Write a program to calculate the sum of the 3-digit number.

#include <stdio.h>

int main(){

char num[4];

int sum=0;

printf("Enter a 3-digit number: ");

scanf("%3s",num);

for(int i=0;i<3;i++) sum += num[i] - '0';

printf("The sum of 3-digit number %s is %d", num, sum);

return 0;

PROBLEM 2. Write a program to merge three numbers into one.

#include <stdio.h>

int main(){

int a,b,c;

scanf("%d%d%d",&a,&b,&c);

int merged = a*100+b*10+c;

printf("The merged number is: %d", merged);

return 0;

PROBLEM 3. Write a program to print the reverse of a 3-digit number

#include <stdio.h>

int main(){

char num[4];

printf("Enter a 3-digit number: ");

scanf("%3s",num);

for(int i=2;i>=0;i--) printf("%c",num[i]);

PROBLEM 4. Newton’s law

#include <stdio.h>

#include <math.h>
int main(){

double mass1, mass2, d;

printf("Enter the mass of object 1 in kg: ");

scanf("%lf",&mass1);

printf("Enter the mass of object 2 in kg: ");

scanf("%lf",&mass2);

printf("Enter the distance in meter: ");

scanf("%lf",&d);

const double G = 6.67*pow(10,-11);

double Force = G*(mass1*mass2)/pow(d,2);

printf("The force between 2 bodies is: %.4elf Newtons", Force);

return 0;

PROBLEM 5. If you buy a 40 GB hard drive, then chances are that the actual storage
on the hard drive is not 40 GB. This is due to the fact that, typically, a manufacturer
uses 1000 bytes as the value of 1K bytes, 1000 K bytes as the value of 1 MB, 1 000
MB as the value of 1 GB. Therefore, a 40 GB hard drive contains 40 000 000 000
bytes. However, in computer memory, 1 KB = 1024 bytes, 1MB = 1024 KB and so on.
The units from the smallest to the largest are Byte, Kilobyte (KB), Megabyte (MB),
Gigabyte (GB), Terabyte (TB), Petabyte (PT), Exabyte (EB), Zettabyte (ZB). So the
actual storage on a 40 GB hard drive is approximately 37.25 GB. (You might like to
read the fine print next time you buy a hard drive.) Write a program that prompts
the user to enter the size of the hard drive specified by the manufacturer, on the
hard drive box, and outputs the actual storage capacity of the hard drive.

#include <stdio.h>

#include <math.h>

int main(){

double manufacturerGB, manufacturerBytes, actualBytes, actualGB;

printf("Enter size of the hard drive specified by the manufacturer (in GB): ");
scanf("%lf",&manufacturerGB);

manufacturerBytes = manufacturerGB*pow(1000,3); // 1GB = 1000^3 bytes

actualBytes = manufacturerBytes;

actualGB = actualBytes / pow(1024,3); // 1GB = 1024^3 bytes

printf("The actual storage capacity of the hard drive is approximately %.2f GB.\n", actualGB);

return 0;

PROBLEM 6. Solve quadric equation

#include <stdio.h>

#include <math.h>

int main(){

double a,b,c,discriminant,x1,x2,realpart,imaginarypart;

// Input a, b, c

printf("Enter a: ");

scanf("%lf",&a);

printf("Enter b: ");

scanf("%lf",&b);

printf("Enter c: ");

scanf("%lf",&c);

// Calculate discriminant

discriminant = b*b-4*a*c;

// Cases in solving the quadratic equation

if(discriminant>0){

x1 = (-b + sqrt(discriminant))/(2*a);

x2 = (-b - sqrt(discriminant))/(2*a);

printf("Roots are real and different: %.2lf and %.2lf",x1,x2);


}

else if(discriminant==0){

x1 = -b/2*a;

printf("Roots are real and the same: %.2lf",x1);

else{

realpart = -b/(2*a);

imaginarypart = sqrt(-discriminant)/(2*a);

printf("Roots are complex: %.2lf+%.2lfi and %.2lf-


%.2lfi",realpart,imaginarypart,realpart,imaginarypart);

return 0;

PROBLEM 7.

You might also like