0% found this document useful (0 votes)
21 views10 pages

Simple C Programs - Sivanthi Iya

The document contains a series of C programming examples demonstrating various fundamental concepts, including input/output operations, arithmetic calculations, control structures, and functions. Each example is accompanied by code snippets that illustrate how to implement specific tasks such as calculating averages, checking for prime numbers, and finding the greatest common divisor. This serves as a practical guide for beginners to learn programming in C.

Uploaded by

sivanthiiya9342
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views10 pages

Simple C Programs - Sivanthi Iya

The document contains a series of C programming examples demonstrating various fundamental concepts, including input/output operations, arithmetic calculations, control structures, and functions. Each example is accompanied by code snippets that illustrate how to implement specific tasks such as calculating averages, checking for prime numbers, and finding the greatest common divisor. This serves as a practical guide for beginners to learn programming in C.

Uploaded by

sivanthiiya9342
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Department of Computer Science and Engineering

Programming in C Laboratory
1.Hello World Program in C:
#include <stdio.h>
int main()
{
/* printf function displays the content that is
* passed between the double quotes.
*/
printf("Hello World");
return 0;
}

2. C Program to find the Size of int, float, double and char:


#include<stdio.h>
int main()
{
printf("Size of char: %ld byte\n",sizeof(char));
printf("Size of int: %ld bytes\n",sizeof(int));
printf("Size of float: %ld bytes\n",sizeof(float));
printf("Size of double: %ld bytes", sizeof(double));
return 0;
}

3. C Program to Find ASCII value of a Character:


#include <stdio.h>
int main()
{
char ch;
printf("Enter any character:");
scanf("%c", &ch);
printf("ASCII value of character %c is: %d", ch, ch);
return 0;
}

4.C Program to Find the Average of 2 Numbers:


#include <stdio.h>
int main()
{
int num1, num2;
float avg;

printf("Enter first number: ");


scanf("%d",&num1);
printf("Enter second number: ");
scanf("%d",&num2);

avg= (float)(num1+num2)/2;

//%.2f is used for displaying output upto two decimal places


printf("Average of %d and %d is: %.2f",num1,num2,avg);

return 0;
}

5. C Program to find quotient and Remainder:


#include <stdio.h>
int main(){
int num1, num2, quot, rem;

printf("Enter dividend: ");


scanf("%d", &num1);

printf("Enter divisor: ");


scanf("%d", &num2);
quot = num1 / num2;
rem = num1 % num2;

printf("Quotient is: %d\n", quot);


printf("Remainder is: %d", rem);

return 0;
}

6. C Program to Multiply two Float Numbers:


#include <stdio.h>
int main(){
float num1, num2, product;
printf("Enter first Number: ");
scanf("%f", &num1);
printf("Enter second Number: ");
scanf("%f", &num2);

//Multiply num1 and num2


product = num1 * num2;
// Displaying result up to 3 decimal places.
printf("Product of entered numbers is:%.3f", product);
return 0;
}

7. C Program to Addition of two integers:


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

int num1, num2, sum;

printf("Enter two integers: ");


//Storing user input into variable num1 & num2
scanf("%d %d", &num1, &num2);

// Adding two input numbers


sum = num1 + num2;

printf("Sum of %d and %d is: %d", num1, num2, sum);


return 0;
}

8. C Program to calculate power of a number using pow()


function
#include <math.h>
#include <stdio.h>

int main() {
int number, exp, result;
printf("Enter the number: ");
scanf("%d", &number);
printf("Enter exponent: ");
scanf("%d", &exp);

// Calculating power using pow() function


result = pow(number, exp);

//displaying result
printf("%d to the power %d is: %d", number, exp, result);
return 0;
}

9. C Program to check Leap Year or not:


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

printf("Enter year: ");


scanf("%d",&y);

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

return 0;
}

10. C Program to check if number is even or odd:


#include<stdio.h>
int main()
{
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;
}

11. C Program to check Vowel or Consonant:


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

if(ch=='a'||ch=='A'||ch=='e'||ch=='E'||ch=='i'||ch=='I'
||ch=='o'||ch=='O'||ch=='u'||ch=='U')
{
isVowel = true;

}
if (isVowel == true)
printf("%c is a Vowel", ch);
else
printf("%c is a Consonant", ch);
return 0;
}

12. C Program to find greatest of three numbers:


#include <stdio.h>

int main() {

double num1, num2, num3;

printf("Enter first number: ");


scanf("%lf", &num1);
printf("Enter second number: ");
scanf("%lf", &num2);
printf("Enter third number: ");
scanf("%lf", &num3);

// if num1 is greater than num2 & num3, num1 is the largest


if (num1 >= num2 && num1 >= num3)
printf("%lf is the largest number.", num1);

// if num2 is greater than num1 & num3, num2 is the largest


if (num2 >= num1 && num2 >= num3)
printf("%lf is the largest number.", num2);

// if num3 is greater than num1 & num2, num3 is the largest


if (num3 >= num1 && num3 >= num2)
printf("%lf is the largest number.", num3);

return 0;
}
13. C Program to check whether the given integer is positive or
negative:
#include <stdio.h>

void main()
{
int num;

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


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

14. C Program to generate multiplication table:


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

15. C Program to find sum of natural numbers using for loop:


#include <stdio.h>
int main()
{
int n, count, sum = 0;

printf("Enter the value of n(positive integer): ");


scanf("%d",&n);

for(count=1; count <= n; count++)


{
sum = sum + count;
}

printf("Sum of first %d natural numbers is: %d",n, sum);


return 0;
}

16. C Program to check whether a number is prime or not


#include <stdio.h>

int main() {

//Here flag value 0 means prime number and


//1 means non-prime number
int num, i, flag = 0;
printf("Enter a number to check whether prime or not: ");
scanf("%d", &num);

// 0 and 1 are not the prime numbers so setting


// the flag to 1 if entered number is 0 or 1
if (num == 0 || num == 1)
flag = 1;

for (i = 2; i <= num / 2; ++i) {

// if num is divisible by i, then num is not prime


// change the flag value to 1
if (num % i == 0) {
flag = 1;
break;
}
}

// If flag is 0 it means the number is prime


if (flag == 0)
printf("Entered number %d is a prime number.", num);
else
printf("Entered number %d is not a prime number.", num);

return 0;
}

17. C Program to Find LCM of two Numbers:


#include <stdio.h>
int main() {
int num1, num2, max;
printf("Enter two positive integers: ");
scanf("%d %d", &num1, &num2);

// greater number between num1 and num2 is stored in max


max = (num1 > num2) ? num1 : num2;
while (1) {
if (max % num1 == 0 && max % num2 == 0) {
printf("LCM of input numbers %d and %d is %d.", num1, num2, max);
break;
}
++max;
}
return 0;
}

18. C Program to Find GCD of two Numbers:


#include <stdio.h>
int main()
{
int num1, num2, i, gcd;

printf("Enter two integers: ");


//Storing user input into num1 and num2
scanf("%d %d", &num1, &num2);

for(i=1; i <= num1 && i <= num2; ++i)


{
// Checks if the current value of i is
// factor of both the integers num1 & num2
if(num1%i==0 && num2%i==0)
gcd = i;
}

printf("GCD of input numbers %d and %d is: %d", num1, num2, gcd);

return 0;
}

19. C Program to display Fibonacci series:


#include<stdio.h>
int main()
{
int count, first_term = 0, second_term = 1, next_term, i;

//Ask user to input number of terms


printf("Enter the number of terms:\n");
scanf("%d",&count);

printf("First %d terms of Fibonacci series:\n",count);


for ( i = 0 ; i < count ; i++ )
{
if ( i <= 1 )
next_term = i;
else
{
next_term = first_term + second_term;
first_term = second_term;
second_term = next_term;
}
printf("%d\n",next_term);
}

return 0;
}

20. C Program to find factorial of number using Recursion:


#include<stdio.h>
int find_factorial(int);
int main()
{
int num, fact;
//Ask user for the input and store it in num
printf("\nEnter any integer number:");
scanf("%d",&num);

//Calling our user defined function


fact =find_factorial(num);

//Displaying factorial of input number


printf("\nfactorial of %d is: %d",num, fact);
return 0;
}
int find_factorial(int n)
{
//Factorial of 0 is 1
if(n==0)
return(1);

//Function calling itself: recursion


return(n*find_factorial(n-1));
}

21. C Program to find prime numbers in a given range:


#include <stdio.h>
int main()
{
int num1, num2, flag_var, i, j;

/* Ask user to input the from/to range


* like 1 to 100, 10 to 1000 etc.
*/
printf("Enter two range(input integer numbers only):");
//Store the range in variables using scanf
scanf("%d %d", &num1, &num2);

//Display prime numbers for input range


printf("Prime numbers from %d and %d are:\n", num1, num2);
for(i=num1+1; i<num2; ++i)
{
flag_var=0;
for(j=2; j<=i/2; ++j)
{
if(i%j==0)
{
flag_var=1;
break;
}
}
if(flag_var==0)
printf("%d\n",i);
}
return 0;
}

22. C Program to check if a number is palindrome or not:


#include <stdio.h>
int main()
{
int num, reverse_num=0, remainder,temp;
printf("Enter an integer: ");
scanf("%d", &num);

temp=num;
while(temp!=0)
{
remainder=temp%10;
reverse_num=reverse_num*10+remainder;
temp/=10;
}

if(reverse_num==num)
printf("%d is a palindrome number",num);
else
printf("%d is not a palindrome number",num);
return 0;
}

You might also like