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

Input Output Practical For C Program

Uploaded by

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

Input Output Practical For C Program

Uploaded by

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

//1.

Display "Welcome"*

#include <stdio.h>

void main() {

printf("Welcome\n");

//2. Input and display integer variable*

#include <stdio.h>

void main() {

int num;

printf("Enter a number: ");

scanf("%d", &num);

printf("You entered: %d\n", num);

//3. Add two numbers*

#include <stdio.h>

void main() {

int num1, num2, sum;

printf("Enter two numbers: ");

scanf("%d %d", &num1, &num2);

sum = num1 + num2;

printf("Sum: %d\n", sum);

//4. Calculate circle area and perimeter*

#include <stdio.h>

#define PI 3.14159

void main() {

float radius, area, perimeter;

printf("Enter radius: ");

scanf("%f", &radius);

area = PI * radius * radius;

perimeter = 2 * PI * radius;

printf("Area: %.2f, Perimeter: %.2f\n", area, perimeter);

//5. Find maximum between two numbers*

#include <stdio.h>

void main() {

int num1, num2, max;


printf("Enter two numbers: ");

scanf("%d %d", &num1, &num2);

max = (num1 > num2) ? num1 : num2;

printf("Maximum: %d\n", max);

//6. Check divisibility by 5 and 11*

#include <stdio.h>

void main() {

int num;

printf("Enter a number: ");

scanf("%d", &num);

if (num % 5 == 0 && num % 11 == 0) {

printf("Divisible by both 5 and 11\n");

} else {

printf("Not divisible by both 5 and 11\n");

//7. Validate triangle angles*

#include <stdio.h>

void main() {

int angle1, angle2, angle3;

printf("Enter three angles: ");

scanf("%d %d %d", &angle1, &angle2, &angle3);

if (angle1 + angle2 + angle3 == 180) {

printf("Valid triangle\n");

} else {

printf("Invalid triangle\n");

//8. Check leap year*

#include <stdio.h>

void main() {

int year;

printf("Enter a year: ");

scanf("%d", &year);

if (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)) {

printf("Leap year\n");
} else {

printf("Not leap year\n");

//9. Calculate Gross salary*

#include <stdio.h>

void main() {

float basic_salary, gross_salary;

printf("Enter basic salary: ");

scanf("%f", &basic_salary);

if (basic_salary <= 10000) {

gross_salary = basic_salary * 1.8;

} else if (basic_salary <= 20000) {

gross_salary = basic_salary * 1.95;

} else {

gross_salary = basic_salary * 2.05;

printf("Gross salary: %.2f\n", gross_salary);

//10. Print "Welcome" 10 times*

#include <stdio.h>

void main() {

for (int i = 0; i < 10; i++) {

printf("Welcome\n");

//11. Print first n natural numbers*

#include <stdio.h>

void main() {

int n;

printf("Enter n: ");

scanf("%d", &n);

for (int i = 1; i <= n; i++) {

printf("%d ", i);

//12. Print odd numbers in range_


#include <stdio.h>

void main() {

int lower, upper;

printf("Enter range: ");

scanf("%d %d", &lower, &upper);

for (int i = lower; i <= upper; i++) {

if (i % 2 != 0) {

printf("%d ", i);

//13. Add first n numbers_

#include <stdio.h>

void main() {

int n, sum = 0;

printf("Enter n: ");

scanf("%d", &n);

for (int i = 1; i <= n; i++) {

sum += i;

printf("Sum: %d\n", sum);

//_14. Print numbers divisible by 3 or 5_

#include <stdio.h>

void main() {

int lower, upper;

printf("Enter range: ");

scanf("%d %d", &lower, &upper);

for (int i = lower; i <= upper; i++) {

if (i % 3 == 0 || i % 5 == 0) {

printf("%d ", i);

//_15. Add even numbers in range_

#include <stdio.h>

void main() {
int lower, upper, sum = 0;

printf("Enter range: ");

scanf("%d %d", &lower, &upper);

for (int i = lower; i <= upper; i++) {

if (i % 2 == 0) {

sum += i;

printf("Sum: %d\n", sum);

//_16. Find factorial_

#include <stdio.h>

void main() {

int num, factorial = 1;

printf("Enter number: ");

scanf("%d", &num);

for (int i = 1; i <= num; i++) {

factorial *= i;

printf("Factorial: %d\n", factorial);

//_17. Check prime number_

#include <stdio.h>

void main() {

int num;

printf("Enter number: ");

scanf("%d", &num);

int is_prime = 1;

for (int i = 2; i * i <= num; i++) {

if (num % i == 0) {

is_prime = 0;

break;

if (is_prime) {

printf("Prime number\n");

} else {
printf("Not prime number\n");

//_18. Print reverse of number_

#include <stdio.h>

void main() {

int num, reverse = 0;

printf("Enter number: ");

scanf("%d", &num);

while (num != 0) {

int remainder = num % 10;

reverse = reverse * 10 + remainder;

num /= 10;

printf("Reverse: %d\n", reverse);

//_19. Add digits of number_

#include <stdio.h>

void main() {

int num, sum = 0;

printf("Enter number: ");

scanf("%d", &num);

while (num != 0) {

sum += num % 10;

num /= 10;

printf("Sum of digits: %d\n", sum);

//_20. Print Fibonacci series_

#include <stdio.h>

void main() {

int n;

printf("Enter n: ");

scanf("%d", &n);

int a = 0, b = 1;

printf("%d %d ", a, b);


for (int i = 3; i <= n; i++) {

int next = a + b;

printf("%d ", next);

a = b;

b = next;

//21. Check Armstrong number

#include <stdio.h>

void main() {

int num;

printf("Enter number: ");

scanf("%d", &num);

int original = num;

int sum = 0;

while (num != 0) {

int digit = num % 10;

sum += digit * digit * digit;

num /= 10;

if (sum == original) {

printf("Armstrong number\n");

} else {

printf("Not Armstrong number\n");

//22. Find GCD and LCM

#include <stdio.h>

int gcd(int a, int b) {

if (b == 0) return a;

return gcd(b, a % b);

int lcm(int a, int b) {

return (a * b) / gcd(a, b);

void main() {

int num1, num2;


printf("Enter two numbers: ");

scanf("%d %d", &num1, &num2);

printf("GCD: %d, LCM: %d\n", gcd(num1, num2), lcm(num1, num2));

//23. Write to file

#include <stdio.h>

void main() {

FILE *fp = fopen("file.txt", "w");

if (fp == NULL) {

printf("Error opening file\n");

return 1;

fprintf(fp, "Hello File Handling\n");

fclose(fp);

//24. Read from file

#include <stdio.h>

void main() {

FILE *fp = fopen("file.txt", "r");

if (fp == NULL) {

printf("Error opening file\n");

return 1;

char c;

while ((c = fgetc(fp)) != EOF) {

printf("%c", c);

fclose(fp);

//25. Student structure and highest marks

#include <stdio.h>

struct Student {

char name[50];

int roll;

float marks;

};

void main() {
struct Student students[5];

for (int i = 0; i < 5; i++) {

printf("Enter student %d name: ", i + 1);

scanf("%s", students[i].name);

printf("Enter student %d roll: ", i + 1);

scanf("%d", &students[i].roll);

printf("Enter student %d marks: ", i + 1);

scanf("%f", &students[i].marks);

float highest = students[0].marks;

int index = 0;

for (int i = 1; i < 5; i++) {

if (students[i].marks > highest) {

highest = students[i].marks;

index = i;

printf("Highest marks: %.2f\n", highest);

printf("Name: %s, Roll: %d\n", students[index].name, students[index].roll);

You might also like