0% found this document useful (0 votes)
27 views

Assignment NO

The document discusses the differences between printf and scanf in C programming. It also covers format specifiers that can be used with printf and scanf and provides examples of using them. It then provides examples of C programs that perform basic tasks like input/output, calculations and conversions.

Uploaded by

Wajih Ul hasan
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)
27 views

Assignment NO

The document discusses the differences between printf and scanf in C programming. It also covers format specifiers that can be used with printf and scanf and provides examples of using them. It then provides examples of C programs that perform basic tasks like input/output, calculations and conversions.

Uploaded by

Wajih Ul hasan
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/ 7

PROGRAMING FUNDAMENTALS

Assignment NO. 01
SUBMITTED BY;
OBAID UL ISLAM
REGISTRATION NO.
FA23-BRG-053
SUBMITTED TO;
Ms Arjamand Fatima
PROGRAMING FUNDAMENTALS

What is difference between printf and scanf


printf and scanf are functions in the C programming language.
- printf: Used for formatted output, it displays information on
the screen or writes it to a file. It is mainly used to print values,
strings, or expressions.
- scanf: Used for formatted input, it reads data from the
standard input (keyboard) or from a specified input stream. It is
used to take input from the user based on a specified format.

What are the different format specifier that can be used


with printf and scanf
For printf:
- %d for integers
- %f for floating-point numbers
- %c for characters
- %s for strings
For scanf:
- %d for integers
- %f for floating-point numbers
- %c for characters
PROGRAMING FUNDAMENTALS

- %s for stringsUse of printf to print

An integer
printf("%d", integerVariable);
floting point number
printf("%f", floatVariable);
A double
- Double: printf("%lf", doubleVariable);
A character
- Character: printf("%c", charVariable);

Use of scanf to to read


- Integer:
scanf("%d", &integerVariable);
- Floating-point number:
scanf("%f", &floatVariable);
- Double:
scanf("%lf", &doubleVariable);
- Character:
scanf(" %c", &charVariable);
PROGRAMING FUNDAMENTALS

write a programme that user to enter two numbers, and then


print the sum of the two number of the console
#include <stdio.h>
int main() {
int num1, num2;
printf("Enter the first number: ");
scanf("%d", &num1);
printf("Enter the second number: ");
scanf("%d", &num2);
int sum = num1 + num2;
printf("Sum of %d and %d is: %d\n", num1, num2, sum);
return 0;
}

Write a programme that prompts the use to enter the


temperature in Celsius and then print the temperatre
convert to Fahrenheit to console

#include <stdio.h>
int main() {
float celsius, fahrenheit;
PROGRAMING FUNDAMENTALS

printf("Enter temperature in Celsius: ");


scanf("%f", &celsius);
fahrenheit = (celsius * 9 / 5) + 32;
printf("Temperature in Fahrenheit: %.2f\n", fahrenheit);
return 0;
}
Write a programme that prompts the user to enter a
distance in miles and then print the distance convert to
kilometer to console
#include <stdio.h>
int main() {
double miles, kilometers;
printf("Enter distance in miles: ");
scanf("%lf", &miles);
kilometers = miles * 1.609344;
printf("Distance in kilometers: %.2lf\n", kilometers);
return 0;
}
PROGRAMING FUNDAMENTALS

Write a C program to read two integers from the user


and print their sum,difference,product,and quotient
#include <stdio.h>
int main() {
int num1, num2
printf("Enter first integer: ");
scanf("%d", &num1);
printf("Enter second integer: ");
scanf("%d", &num2);
int sum = num1 + num2;
int difference = num1 - num2;
int product = num1 * num2;
double quotient = (double)num1 / num2; // Cast to double to handle
division properly
printf("Sum: %d\n", sum);
printf("Difference: %d\n", difference);
printf("Product: %d\n", product);
printf("Quotient: %.2f\n", quotient); // Use %.2f to format the
quotient to two decimal places
return 0;
}
PROGRAMING FUNDAMENTALS

Write a C programme to read a floting point number from the


user and print its square root
#include <stdio.h>
#include <math.h>
int main() {
double number, squareRoot;
printf("Enter a floating-point number: ");
scanf("%lf", &number);
squareRoot = sqrt(number);
printf("Square root of %.2f is: %.2f\n", number, squareRoot);
return 0;
}

You might also like