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

Basic C Programs

The document contains C code examples for various basic programs including printing values, adding two numbers, swapping variables, calculating simple and compound interest, checking even/odd numbers, checking positive/negative numbers, and calculating factorials. The programs demonstrate fundamental C programming concepts like input/output, conditional statements, loops, functions, and arithmetic operations.

Uploaded by

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

Basic C Programs

The document contains C code examples for various basic programs including printing values, adding two numbers, swapping variables, calculating simple and compound interest, checking even/odd numbers, checking positive/negative numbers, and calculating factorials. The programs demonstrate fundamental C programming concepts like input/output, conditional statements, loops, functions, and arithmetic operations.

Uploaded by

Ganesh
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 6

// C program to demonstrate printing of our own name using printf()//

-----------------------------------------------------------------------------------
----------

-----------------------------------------------------------------------------------
----------------
// C Program to Print Integer value //
-----------------------------------------------------------------------------------
----------------
#include <stdio.h>

int main()
{
// Declaring integer
int x = 5;

// Printing values
printf("Printing Integer value %d", x);
return 0;
}
-----------------------------------------------------------------------------------
----------
// C program to add two numbers
-----------------------------------------------------------------------------------
----------
#include <stdio.h>

int main()
{
int A, B, sum = 0;

//Ask user to enter the two numbers


printf("Enter two numbers A and B : \n");

// Read two numbers from the user || A = 2, B = 3


scanf("%d%d", &A, &B);

// Calculate the addition of A and B


// using '+' operator
sum = A + B;

// Print the sum


printf("Sum of A and B is: %d", sum);

return 0;
}
-----------------------------------------------------------------------------------
-----------
// C program to swap two variables
-----------------------------------------------------------------------------------
-----------
#include <stdio.h>

int main()
{
int x, y;
printf("Enter Value of x ");
scanf("%d", &x);
printf("\nEnter Value of y ");
scanf("%d", &y);

int temp = x;
x = y;
y = temp;

printf("\nAfter Swapping: x = %d, y = %d", x, y);


return 0;
}
-----------------------------------------------------------------------------------
---------
// C Program to convert // Fahrenheit to Celsius
-----------------------------------------------------------------------------------
----------
#include <stdio.h>

int main()
{
float d ,far;
printf("enter the degrees");
far=((d*9)/5)+32;
printf("The Fahrenheit is %d",far);
return 0;
}
-----------------------------------------------------------------------------------
------------
/ C Program to convert //Celsius to Fahrenheit
-----------------------------------------------------------------------------------
-----------
#include <stdio.h>

int main()
{
float f ,cel;
printf("enter the Fahrenheit");
cel=((f-32)*5)/9;
printf("The Celsius is %d",cel);
return 0;
}
-----------------------------------------------------------------------------------
----------
// C program to implement Simple Interest
-----------------------------------------------------------------------------------
----------
// the above approach
#include <stdio.h>

// Driver code
int main()
{
// We can change values here for
// different inputs
float P = 1, R = 1, T = 1;
// Calculate simple interest
float SI = (P * T * R) / 100;

// Print Simple Interest


printf("Simple Interest = %f\n", SI);

return 0;
}
-----------------------------------------------------------------------------------
-----------
// For using pow function we must
-----------------------------------------------------------------------------------
-------------
// include math.h
#include<math.h>

// Driver code
int main()
{
// Principal amount
double principal = 10000;

// Annual rate of interest


double rate = 5;

// Time
double time = 2;

// Calculating compound Interest


double Amount = principal * ((pow((1 + rate / 100),time)));
double CI = Amount - principal;
printf("Compound Interest is : %lf",CI);
return 0;
}
-----------------------------------------------------------------------------------
------------
// C program to demonstrate the area and perimeter of rectangle
-----------------------------------------------------------------------------------
-------------
#include <stdio.h>

int main()
{

int l = 10, b = 10;


printf("Area of rectangle is : %d", l * b);
printf("\nPerimeter of rectangle is : %d", 2 * (l + b));
return 0;
}
-----------------------------------------------------------------------------------
----
A program to check whether the input integer number is positive or negative.
-----------------------------------------------------------------------------------
------
#include <stdio.h>
#inlcude <conio.h>

int main()
{
int num;

printf("Enter a number: \n");


scanf("%d", &num);
if (num > 0)
{
printf("%d is a positive number \n", num);
}
else
{
printf("%d is a negative number \n", num);
}

return 0;
}
-----------------------------------------------------------------------------------
--------
Program to check whether the input integer number
-----------------------------------------------------------------------------------
---------
is even or odd using the modulus operator (%)
#include<stdio.h>
#inlcude <conio.h>
int main()
{
// This variable is to store the input number
int num;

printf("Enter an integer: ");


scanf("%d",&num);

// Modulus (%) returns remainder


if ( num%2 == 0 )
{
printf("%d is an even number", num);
}
else
{
printf("%d is an odd number", num);
}

return 0;
}
-----------------------------------------------------------------------------------
---------
To Check Leap Year or Not
-----------------------------------------------------------------------------------
----------
#include <stdio.h>
#inlcude <conio.h>
int main()
{
int y;

printf("Enter year: ");


scanf("%d",&y);

if(y % 4 == 0)
{
printf("%d is a Leap Year", y);
}
else
{
printf("%d is not a Leap Year", y);
}
return 0;

}
--------------------------------------------------------------------------
#include <stdio.h>
int main()
{
int n, s=0, r;

printf(“\n Enter the Number”);


scanf(“%d”, &n);
for(;n>0;n/=10)
{
r=n%10;
s=s+r;
}
printf(“\n Sum of digits %d”, s);
return 0;
}

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#include <stdio.h>
int main()
{
int x = 1;
int count = 0;
do {
scanf(“%d”, &x);
if(x >= 0) count += 1;
} while(x >= 0);
return 0;
}

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

#include <stdio.h>
int main()
{
int n, c;
long int f=1;
printf(“\n Enter the number:”);
scanf(“%d”,&n);
if(n<0)
goto end;
for(c=1; c<=n; c++)
f*=c;
printf(“\n FACTORIAL IS %ld”, f);
end:
return 0;
}

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#include <stdio.h>
int main()
{
int row, col;
for(row=1;row<=4;++row)
{
for(col=1;col<=row;++col)
printf(“%d \t”, row);
printf(“\n”);
}
return 0;
}

You might also like