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

Sample Programs

The document contains 12 code samples that demonstrate basic input/output and mathematical operations in C programming. The samples include programs to: 1) print a number input by the user, 2) input and output variables of different data types, 3) directly assign and output variables of different data types, 4) input variables of different data types and output them, 5) add two numbers, 6) add three numbers, 7) subtract two numbers, 8) multiply two numbers, 9) divide two numbers, 10) calculate the area of a triangle, 11) calculate the distance between two points, and 12) swap two numbers. The code samples demonstrate basic use of input/output statements like scanf and printf and perform fundamental mathematical operations

Uploaded by

Tanu U
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Sample Programs

The document contains 12 code samples that demonstrate basic input/output and mathematical operations in C programming. The samples include programs to: 1) print a number input by the user, 2) input and output variables of different data types, 3) directly assign and output variables of different data types, 4) input variables of different data types and output them, 5) add two numbers, 6) add three numbers, 7) subtract two numbers, 8) multiply two numbers, 9) divide two numbers, 10) calculate the area of a triangle, 11) calculate the distance between two points, and 12) swap two numbers. The code samples demonstrate basic use of input/output statements like scanf and printf and perform fundamental mathematical operations

Uploaded by

Tanu U
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Sample Programs

1. Printing the number


#include<stdio.h>
int main()
{
int n;
printf(“enter the number:”);
scanf(“%d”,&n);
printf(“the number is %d”,n);
return 0;
}

2. Write a program to demonstrate the use of printf and scanf


statement to print the values of variables of different data types.
#include<stdio.h>
int main()
{
int num;
float fnum;
char ch;
char str[10];
double dnum;
printf(“enter the values:”);
scanf(“%d %f %c %s %lf”, &num, &fnum, &ch, str, &dnum);
printf(“\n num = %d \n fnum=%f \n ch = %c \n str=%s \n dnum
= %lf \n”, num, fnum, ch, str, dnum);
}
3. Write a program to demonstrate the use of printf and scanf
statement to print the values of variables of different data types.

#include<stdio.h>
int main()
{
int num=7;
float amt=123.45;
char code=’A’;
char msg[10]=”hello”;
double pi=3.145678;
printf(“\n num = %d \n amt=%f \n code = %c \n msg=%s \n pi =
%lf \n”, num, amt, code, msg, pi);
return 0;
}

4. Write a program to demonstrate the use of printf and scanf


statement to print the values of variables of different data types.

#include<stdio.h>
int main()
{
int num;
float amt;
char code;
char msg[10];
double pi;
printf(“\nEnter the value of num:”);
scanf(“%d”,&num);
printf(“\nEnter the value of amt:”);
scanf(“%f”,&amt);
printf(“\nEnter the value of code:”);
scanf(“%c”,&code);
printf(“\nEnter the message:”);
scanf(“%s”,msg);
printf(“\nEnter the value of pi:”);
scanf(“%lf”,&pi);
printf(“\n num = %d \n amt=%f \n code = %c \n msg=%s \n pi =
%lf \n”, num, amt, code, msg, pi);
return 0;
}

5. Write a program to add two numbers.

#include <stdio.h>
int main()
{
int A, B, sum = 0;
printf("Enter two numbers A and B : \n");
scanf("%d%d", &A, &B);
sum = A + B;
printf("Sum of A and B is: %d", sum);
return 0;
}

6. Write a program to add three numbers.


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

int a,b,c, sum;


printf("Enter three integers to add: ");
scanf("%d %d %d", &a, &b, &c);
sum = a + b+c;
printf("Sum of three numbers is %d", sum);
return 0;
}
7. Write a program to subtraction of two numbers.
8. Write a program to multiplication of two numbers.
9. Write a program to division of two numbers.
10. Write a program to calculate the area of triangle using
formula.
11. Write a program to calculate the distance between
two points.
12. Write a program to swap two numbers.

You might also like