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

CP1&2

C- programming basic programs
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)
10 views6 pages

CP1&2

C- programming basic programs
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/ 6

WEEK-1

i).Program to Print a Message:

#include <stdio.h>
int main()
{
printf("Hello, World!");
return 0;
}
Output:
Hello, World!

ii).Read and Print an Integer

#include <stdio.h>
int main()
{
int num;
printf("Enter an integer: ");
scanf("%d", &num);
printf("You entered: %d\n", num);
return 0;
}
Output:
Enter an integer: 5
You entered: 5
iii).Read Two Integers and Add Them

#include <stdio.h>
int main()
{
int a, b, sum;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
sum = a + b;
printf("Sum = %d\n", sum);
return 0;
}
Output:
Enter two numbers: 4 5
Sum = 9
iv).Read and Print a Float

#include <stdio.h>
int main()
{
float a;
printf("Enter a float value: ");
scanf("%f", &a);
printf("You entered: %.2f\n", a);
return 0;
}
Output:
Enter a float value: 4.5
You entered: 4.50
v).Read and Print a Character

#include <stdio.h>
int main()
{
char ch;
printf("Enter a character: ");
scanf("%c", &ch);
printf("You entered: %c\n", ch);
return 0;
}
Output:
Enter a character: s
You entered: s
WEEK-2

i). Sum and average of 3 numbers:

#include <stdio.h>

int main()

float num1, num2, num3, sum, average;

// Input three numbers

printf("Enter three numbers: ");

scanf("%f %f %f", &num1, &num2, &num3);

// Calculate sum and average

sum = num1 + num2 + num3;

average = sum / 3;

// Output results

printf("Sum = %.2f\n", sum);

printf("Average = %.2f\n", average);

return 0;

Output:

Enter three numbers: 3 5 6

Sum = 14.00

Average = 4.67
ii).Conversion of Fahrenheit to Celsius and vice versa

----------Conversion of Fahrenheit to Celsius-------

#include <stdio.h>
int main() {
float fahrenheit, celsius;
printf("Enter temperature in Fahrenheit: ");
scanf("%f", &fahrenheit);
// Conversion formula
celsius = (fahrenheit - 32) * 5.0 / 9.0;
// Output result
printf("Temperature in Celsius: %.2f\n", celsius);
return 0;
}
Output:
Enter temperature in Fahrenheit: 35
Temperature in Celsius: 1.67
-----------Conversion of Celsius to Fahrenheit-----------
#include <stdio.h>
int main() {
float celsius, fahrenheit;
printf("Enter temperature in Celsius: ");
scanf("%f", &celsius);
// Conversion formula
fahrenheit = (celsius * 9.0 / 5.0) + 32;
printf("Temperature in Fahrenheit: %.2f\n", fahrenheit);
return 0;
}
Output:
Enter temperature in Celsius: 3
Temperature in Fahrenheit: 37.40

iii).Simple interest calculation

#include <stdio.h>

int main()

float principal, rate, time, interest;

printf("Enter Principal amount (P): ");

scanf("%f", &principal);

printf("Enter Rate of interest (R): ");

scanf("%f", &rate);

printf("Enter Time (T in years): ");

scanf("%f", &time);

// Calculate Simple Interest

interest = (principal * rate * time) / 100;

printf("Simple Interest = %.2f\n", interest);

return 0;

Output:

Enter Principal amount (P): 10000

Enter Rate of interest (R): 5

Enter Time (T in years): 2

Simple Interest = 1000.00

You might also like