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

C Program To Swap Two Numbers: C Program To Find Size of Int, Float, Double and Char, Keyword Long of Your System

This document contains C code examples for several basic programs including: 1) A program to print the size of different data types like int, float, etc. 2) Programs to swap values, check if a number is even or odd, check if a character is a vowel or consonant. 3) Programs to find the largest of three numbers, check if a year is a leap year, and check if a number is positive, negative or zero. 4) Programs to calculate the sum and factorial of numbers, check if a number is a palindrome, reverse a number, generate Fibonacci series, and multiplication table.

Uploaded by

Suganya Selvaraj
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)
104 views

C Program To Swap Two Numbers: C Program To Find Size of Int, Float, Double and Char, Keyword Long of Your System

This document contains C code examples for several basic programs including: 1) A program to print the size of different data types like int, float, etc. 2) Programs to swap values, check if a number is even or odd, check if a character is a vowel or consonant. 3) Programs to find the largest of three numbers, check if a year is a leap year, and check if a number is positive, negative or zero. 4) Programs to calculate the sum and factorial of numbers, check if a number is a palindrome, reverse a number, generate Fibonacci series, and multiplication table.

Uploaded by

Suganya Selvaraj
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

C Program to Find Size of int, float, double and char, keyword long of Your System

#include <stdio.h>
int main()
{
int a;
float b;
double c;
char d;
long int b;
long long int c;

printf("Size of int: %d bytes\n",sizeof(a));
printf("Size of float: %d bytes\n",sizeof(b));
printf("Size of double: %d bytes\n",sizeof(c));
printf("Size of char: %d byte\n",sizeof(d));
printf("Size of long int = %ld bytes\n",sizeof(b));
printf("Size of long long int = %ld bytes",sizeof(c));

return 0;
}

C Program to Swap Two Numbers

#include <stdio.h>
int main(){
float a, b, temp;
printf("Enter value of a: ");
scanf("%f",&a);
printf("Enter value of b: ");
scanf("%f",&b);
temp = a;
a = b;
b = temp;
printf("\nAfter swapping, value of a = %.2f\n", a);
printf("After swapping, value of b = %.2f", b);
return 0;
}
C Program to Check Whether a Number is
Even or Odd

#include <stdio.h>
int main(){
int num;
printf("Enter an integer you want to check: ");
scanf("%d",&num);
if((num%2)==0) /* Checking whether remainder is 0 or not. */
printf("%d is even.",num);
else
printf("%d is odd.",num);
return 0;
}
C Program to Check Vowel or Consonant

#include <stdio.h>
int main(){
char c;
printf("Enter an alphabet: ");
scanf("%c",&c);

if(c=='a'||c=='A'||c=='e'||c=='E'||c=='i'||c=='I'||c=='o'||c=='O'||c=='u'||c=
='U')
printf("%c is a vowel.",c);
else
printf("%c is a consonant.",c);
return 0;
}

C Program to Find the Largest Number
Among Three Numbers
#include <stdio.h>
int main(){
float a, b, c;
printf("Enter three numbers: ");
scanf("%f %f %f", &a, &b, &c);
if(a>=b && a>=c)
printf("Largest number = %.2f", a);
if(b>=a && b>=c)
printf("Largest number = %.2f", b);
if(c>=a && c>=b)
printf("Largest number = %.2f", c);
return 0;
}

or using if else statement
/* C program to find largest number using if...else statement */

#include <stdio.h>
int main(){
float a, b, c;
printf("Enter three numbers: ");
scanf("%f %f %f", &a, &b, &c);
if (a>=b)
{
if(a>=c)
printf("Largest number = %.2f",a);
else
printf("Largest number = %.2f",c);
}
else
{
if(b>=c)
printf("Largest number = %.2f",b);
else
printf("Largest number = %.2f",c);
}
return 0;
}

Or using nested if
#include <stdio.h>
int main(){
float a, b, c;
printf("Enter three numbers: ");
scanf("%f %f %f", &a, &b, &c);
if(a>=b && a>=c)
printf("Largest number = %.2f", a);
else if(b>=a && b>=c)
printf("Largest number = %.2f", b);
else
printf("Largest number = %.2f", c);
return 0;
}

C program to check whether a year is leap year or not using if else statement

#include <stdio.h>
int main(){
int year;
printf("Enter a year: ");
scanf("%d",&year);
if(year%4 == 0)
{
if( year%100 == 0) /* Checking for a century year */
{
if ( year%400 == 0)
printf("%d is a leap year.", year);
else
printf("%d is not a leap year.", year);
}
else
printf("%d is a leap year.", year );
}
else
printf("%d is not a leap year.", year);
return 0;
}

C Program to Check Whether a Number is
Positive or Negative or Zero.

#include <stdio.h>
int main()
{
float num;
printf("Enter a number: ");
scanf("%f",&num);
if (num<=0)
{
if (num==0)
printf("You entered zero.");
else
printf("%.2f is negative.",num);
}
else
printf("%.2f is positive.",num);
return 0;
}

C Program to Calculate Sum of Natural
Numbers
#include <stdio.h>
int main()
{
int n, count, sum=0;
printf("Enter an integer: ");
scanf("%d",&n);
count=1;
while(count<=n) /* while loop terminates if count>n */
{
sum+=count; /* sum=sum+count */
++count;
}
printf("Sum = %d",sum);
return 0;
}

/* This program is solve using for loop. */

#include <stdio.h>
int main()
{
int n, count, sum=0;
printf("Enter an integer: ");
scanf("%d",&n);
for(count=1;count<=n;++count) /* for loop terminates if count>n */
{
sum+=count; /* sum=sum+count */
}
printf("Sum = %d",sum);
return 0;
}
C Program to Find Factorial of a Number

#include <stdio.h>
int main()
{
int n, count;
unsigned long long int factorial=1;
printf("Enter an integer: ");
scanf("%d",&n);
if ( n< 0)
printf("Error!!! Factorial of negative number doesn't exist.");
else
{
for(count=1;count<=n;++count) /* for loop terminates if count>n */
{
factorial*=count; /* factorial=factorial*count */
}
printf("Factorial = %lu",factorial);
}
return 0;
}
C Program to Check Whether a Number is
Palindrome or Not
#include <stdio.h>
int main()
{
int n, reverse=0, rem,temp;
printf("Enter an integer: ");
scanf("%d", &n);
temp=n;
while(temp!=0)
{
rem=temp%10;
reverse=reverse*10+rem;
temp/=10;
}
/* Checking if number entered by user and it's reverse number is equal. */
if(reverse==n)
printf("%d is a palindrome.",n);
else
printf("%d is not a palindrome.",n);
return 0;
}

C Program to Reverse a Number

#include <stdio.h>
int main()
{
int n, reverse=0, rem;
printf("Enter an integer: ");
scanf("%d", &n);
while(n!=0)
{
rem=n%10;
reverse=reverse*10+rem;
n/=10;
}
printf("Reversed Number = %d",reverse);
return 0;
}

C Program to Display Fibonacci Series

#include <stdio.h>
int main()
{
int count, n, t1=0, t2=1, display=0;
printf("Enter number of terms: ");
scanf("%d",&n);
printf("Fibonacci Series: %d+%d+", t1, t2); /* Displaying first two terms
*/
count=2; /* count=2 because first two terms are already displayed. */
while (count<n)
{
display=t1+t2;
t1=t2;
t2=display;
++count;
printf("%d+",display);
}
return 0;
}

C program to Generate Multiplication Table
#include <stdio.h>
int main()
{
int n, i;
printf("Enter an integer to find multiplication table: ");
scanf("%d",&n);
for(i=1;i<=10;++i)
{
printf("%d * %d = %d\n", n, i, n*i);
}
return 0;
}

You might also like