0% found this document useful (0 votes)
23 views7 pages

C Program to Determine Data Type Sizes

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)
23 views7 pages

C Program to Determine Data Type Sizes

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

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;
}

Common questions

Powered by AI

'For' loops explicitly handle initialization, condition checking, and incrementing in a single line, offering clear structure ideal for fixed iteration counts. 'While' loops provide more flexibility where iteration count is less known a priori, but can result in less concise code and must manage state variables more manually. Choosing between them often hinges on readability and specific iteration needs .

The program checks if a year is divisible by 4 to determine a basic leap year. Then, it checks if the year is a century (divisible by 100), requiring it to also be divisible by 400 to remain a leap year. This additional check accommodates for exceptions such as century years which are not leap years unless divisible by 400 .

The program handles floating-point input and could misuse equality checks with floating points due to precision issues. Addressing this by using a range check (e.g., for zero) can improve accuracy. Furthermore, input validation isn’t implemented, exposing potential errors if non-numeric or unexpected data types are entered .

All shown programs declare main-scoped variables, limiting lifecycle and visibility to the containing function, enhancing memory management and reducing side effects—important for debugging by confining unintended changes. Mismanaging scope, such as using globals unnecessarily, can introduce hard-to-find bugs due to unintentional shared state .

The program uses a temporary variable to hold one of the values, allowing numbers to be swapped without losing original data. The process is straightforward but dependent on additional memory for storage and doesn't generalize well to more complex swaps or data structures without extra manipulation .

To reverse a number, the program continually extracts the last digit and appends it to a new reverse integer, which is multiplied by ten each iteration to shift prior digits. For palindrome checks, the reversal process is the same, but it concludes by comparing the reversed number with the original, confirming a palindrome if equal .

The 'if' statements check each condition independently, which can lead to multiple statements being true, potentially outputting more than one result if used improperly. In contrast, 'if...else' statements ensure that only one block of code will execute, as it selects through conditions hierarchically and stops checking further once a true condition is found .

The iterative approach uses a simple loop, preserving memory by storing only the current factorial value. A recursive solution, however, involves multiple function calls, leading to stack memory growth proportional to the input size. Iterative solutions generally perform better for large numbers due to reduced memory overhead, while recursion offers a more direct problem-solving approach at the cost of efficiency .

The program employs a simple conditional to check if a character matches any specified vowels, uppercase or lowercase, defining any other character as a consonant. Improvements might include extending checks to accommodate accented vowels and non-Latin alphabets, which would involve adding more conditions or using more sophisticated character libraries .

The program iterates using variables to track the last two Fibonacci numbers, adding them to obtain the next. A recursive approach could be simpler in terms of understanding but exponentially increases stack calls, leading to poor performance for large n, which iteration efficiently handles with constant space .

You might also like