Programming File
Programming File
Print an integer
#include <stdio.h>
int main() {
int number;
// displays output
printf("You entered: %d", number);
return 0;
}
Output
// calculating sum
sum = number1 + number2;
Output
In this program, the user is asked to enter two integers. These two integers are
stored in variables number1 and number2 respectively.
// Calculating product
product = a * b;
return 0;
}
Output
Product = 2.69n this program, the user is asked to enter two numbers which are
Then, the product of a and b is evaluated and the result is stored in product .
product = a * b;
// Computes quotient
quotient = dividend / divisor;
// Computes remainder
remainder = dividend % divisor;
Output
In this program, the user is asked to enter two integers (dividend and divisor).
They are stored in variables dividend and divisor respectively.
Then the quotient is evaluated using / (the division operator), and stored
in quotient .
#include <stdio.h>
int main() {
int num;
printf("Enter an integer: ");
scanf("%d", &num);
return 0;
}
Output\
In the program, the integer entered by the user is stored in the variable num .
Then, whether num is perfectly divisible by 2 or not is checked using the
modulus % operator.
6. Program to swap two
numbers
#include<stdio.h>
int main() {
double first, second, temp;
printf("Enter first number: ");
scanf("%lf", &first);
printf("Enter second number: ");
scanf("%lf", &second);
Output
Output
The character entered by the user is stored in variable c .
The lowercase_vowel variable evaluates to 1 (true) if c is a lowercase vowel and 0
(false) for any other characters.
return 0;
}
Output
9. Program to calculate the
sum of natural number
#include <stdio.h>
int main() {
int n, i, sum = 0;
printf("Enter a positive integer: ");
scanf("%d", &n);
i = 1;
while (i <= n) {
sum += i;
++i;
}
Output
while (1) {
if (max % n1 == 0 && max % n2 == 0) {
printf("The LCM of %d and %d is %d.", n1, n2, max);
break;
}
++max;
}
return 0;
}
Output
return 0;
}
Output
if (n == 1) {
printf("1 is neither prime nor composite.");
}
else {
if (flag == 0)
printf("%d is a prime number.", n);
else
printf("%d is not a prime number.", n);
}
return 0;
}
Output
Output
switch (operator) {
case '+':
printf("%.1lf + %.1lf = %.1lf", first, second, first + second);
break;
case '-':
printf("%.1lf - %.1lf = %.1lf", first, second, first - second);
break;
case '*':
printf("%.1lf * %.1lf = %.1lf", first, second, first * second);
break;
case '/':
printf("%.1lf / %.1lf = %.1lf", first, second, first / second);
break;
// operator doesn't match any case constant
default:
printf("Error! operator is not correct");
}
return 0;
}
Output
#include <stdio.h>
int main() {
int rows, coef = 1, space, i, j;
printf("Enter the number of rows: ");
scanf("%d", &rows);
for (i = 0; i < rows; i++) {
for (space = 1; space <= rows - i; space++)
printf(" ");
for (j = 0; j <= i; j++) {
if (j == 0 || i == 0)
coef = 1;
else
coef = coef * (i - j + 1) / j;
printf("%4d", coef);
}
printf("\n");
}
return 0;
}
Output
#include <stdio.h>
int main() {
double n1, n2, n3;
printf("Enter three numbers: ");
scanf("%lf %lf %lf", &n1, &n2, &n3);
return 0;
}
Output
if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))
printf("%c is an alphabet.", c);
else
printf("%c is not an alphabet.", c);
return 0;
}
Output
In the program, 'a' is used instead of 97 and 'z' is used instead of 122 .
Similarly, 'A' is used instead of 65 and 'Z' is used instead of 90 .
while (exp != 0) {
result *= base;
--exp;
}
printf("Answer = %lld", result);
return 0;
}
Output
originalNumber = number;
return 0;
}
OUTPUT
return 0;
}
Output
if (flag == 1)
printf("%d ", i);
}
return 0;
}
Output
#include <stdio.h>
int addNumbers(int n);
int main() {
int num;
printf("Enter a positive integer: ");
scanf("%d", &num);
printf("Sum = %d", addNumbers(num));
return 0;
}
int addNumbers(int n) {
if (n != 0)
return n + addNumbers(n - 1);
else
return n;
}
Output
#include <stdio.h>
int power(int n1, int n2);
int main() {
int base, a, result;
printf("Enter base number: ");
scanf("%d", &base);
printf("Enter power number(positive integer): ");
scanf("%d", &a);
result = power(base, a);
printf("%d^%d = %d", base, a, result);
return 0;
}
Output
Output
25 .Program to Check Prime or
Armstrong Number Using User-
defined Function
#include <math.h>
#include <stdio.h>
int convert(long long n);
int main() {
long long n;
printf("Enter a binary number: ");
scanf("%lld", &n);
printf("%lld in binary = %d in decimal", n, convert(n));
return 0;
}
Output
26. Program to Check Prime or
Armstrong Number Using User-
defined Function
#include <math.h>
#include <stdio.h>
int main() {
int n, flag;
printf("Enter a positive integer: ");
scanf("%d", &n);
Output
27. Program to Calculate Average
Using Arrays
#include <stdio.h>
int main() {
int n, i;
float num[100], sum = 0.0, avg;
avg = sum / n;
printf("Average = %.2f", avg);
return 0;
}
Output
28 Program to Find Largest
Element in an Array
#include <stdio.h>
int main() {
int i, n;
float arr[100];
printf("Enter the number of elements (1 to 100): ");
scanf("%d", &n);
return 0;
}
Output
29. Program to Calculate Standard
Deviation
#include <math.h>
#include <stdio.h>
float calculateSD(float data[]);
int main() {
int i;
float data[10];
printf("Enter 10 elements: ");
for (i = 0; i < 10; ++i)
scanf("%f", &data[i]);
printf("\nStandard Deviation = %.6f", calculateSD(data));
return 0;
}
Output
30. Program to Find Transpose of a
Matrix
#include <stdio.h>
int main() {
int a[10][10], transpose[10][10], r, c, i, j;
printf("Enter rows and columns: ");
scanf("%d %d", &r, &c);
Output
return 0;
}
Output
This program takes n number of elements from the user and stores it in arr[] .
return 0;
}
Output
In this program, the user is asked to enter the number of rows r and
columns c . Then, the user is asked to enter the elements of the two
int main() {
int values[5];
Output
Here, we have used a for loop to take 5 inputs from the user and store them
in an array. Then, using another for loop, these elements are displayed on
the screen
#include <stdio.h>
int main()
{
int marks[10], i, n, sum = 0, average;
average = sum/n;
printf("Average = %d", average);
return 0;
}
Output
Here, we have computed the average of n numbers entered by the user.
int main()
{
int ageArray[] = {2, 8, 4, 12};
Output
Here, the string entered by the user is stored in the line variable.
Initially, the variables vowel , consonant , digit, and space are initialized to 0.
Output
Here, using a for loop, we have iterated over characters of the string from i =
Output
// storing information
for (i = 0; i < 5; ++i) {
s[i].roll = i + 1;
printf("\nFor roll number%d,\n", s[i].roll);
printf("Enter first name: ");
scanf("%s", s[i].firstName);
printf("Enter marks: ");
scanf("%f", &s[i].marks);
}
printf("Displaying Information:\n\n");
// displaying information
for (i = 0; i < 5; ++i) {
printf("\nRoll number: %d\n", i + 1);
printf("First name: ");
puts(s[i].firstName);
printf("Marks: %.1f", s[i].marks);
printf("\n");
}
return 0;
}
Output
.
40 Program to Sort Elements in
Dictionary Order
#include <stdio.h>
#include <string.h>
int main() {
char str[5][50], temp[50];
printf("Enter 5 words: ");
// Getting strings input
for (int i = 0; i < 5; ++i) {
fgets(str[i], sizeof(str[i]), stdin);
}
Output
41. Program to Add Two Distances
(in inch-feet system) using
Structures
#include <stdio.h>
struct Distance {
int feet;
float inch;
} d1, d2, result;
int main() {
// take first distance input
printf("Enter 1st distance\n");
printf("Enter feet: ");
scanf("%d", &d1.feet);
printf("Enter inch: ");
scanf("%f", &d1.inch);
// adding distances
result.feet = d1.feet + d2.feet;
result.inch = d1.inch + d2.inch;
int main() {
complex n1, n2, result;
Output
int main() {
struct TIME startTime, stopTime, diff;
Output
int main() {
struct course *ptr;
int i, noOfRecords;
printf("Enter the number of records: ");
scanf("%d", &noOfRecords);
printf("Displaying Information:\n");
for (i = 0; i < noOfRecords; ++i)
printf("%s\t%d\n", (ptr + i)->subject, (ptr + i)->marks);
return 0;
}
Output
int main() {
complex n1, n2, result;
Output
int main()
{
struct person *personPtr, person1;
personPtr = &person1;
printf("Enter age: ");
scanf("%d", &personPtr->age);
printf("Displaying:\n");
printf("Age: %d\n", personPtr->age);
printf("weight: %f", personPtr->weight);
return 0;
}
Output
int main()
{
int n1,n2,sum;
return 0;
}
Output
int main()
{
int num;
FILE *fptr;
fscanf(fptr,"%d", &num);
return 0;
}
Output
int main()
{
int num;
FILE *fptr;
if(fptr == NULL)
{
printf("Error!");
exit(1);
}
fprintf(fptr,"%d",num);
fclose(fptr);
return 0;
}
Output