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

C Program

The document contains a collection of C programming examples covering basic operations such as addition, subtraction, multiplication, and division, as well as more complex tasks like palindrome checking, Fibonacci series generation, and leap year determination. Each example includes code snippets demonstrating the functionality and user interaction. Overall, it serves as a comprehensive guide for beginners to understand fundamental programming concepts in C.
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)
23 views22 pages

C Program

The document contains a collection of C programming examples covering basic operations such as addition, subtraction, multiplication, and division, as well as more complex tasks like palindrome checking, Fibonacci series generation, and leap year determination. Each example includes code snippets demonstrating the functionality and user interaction. Overall, it serves as a comprehensive guide for beginners to understand fundamental programming concepts in C.
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/ 22

C Program

1.Hello World:
#include <stdio.h>

int main()

printf("Hello, World!\n");

return 0;

2.Addition of two numbers:


// Header File

#include <stdio.h>

int main()

//Variable declartaion

int number1, number2, sum;

printf("Enter two integers: ");

scanf("%d %d", &number1, &number2);

// calculate the sum

sum = number1 + number2;

//Displya the addition

printf("%d + %d = %d", number1, number2, sum);

return 0;

}
C Program

3.subtraction:
#include <stdio.h>

int main()

int number1, number2, sub;

printf("Enter two integers: ");

scanf("%d %d", &number1, &number2);

4.Division:
#include <stdio.h>

int main()

int number1, number2, div;

printf("Enter two integers: ");

scanf("%d %d", &number1, &number2);

// calculate the sum

div = number1 / number2;

printf("%d / %d = %d", number1, number2, div);

return 0;

}
C Program

5.Module:
//header file

#include<stdio.h>

int main()

//variable delcleration

int num1, num2, rem;

//Asking for input

printf("Enter the dividend: ");

scanf("%d", &num1);

printf("Enter the divisor: ");

scanf("%d", &num2);

rem = num1 % num2;

printf("Remainder = %d", rem);

return 0;

6.Multiplication:
#include <stdio.h>

int main()

int number1, number2, mul;

printf("Enter two integers: ");

scanf("%d %d", &number1, &number2);

// calculate the sum

mul = number1 * number2;


C Program

printf("%d * %d = %d", number1, number2, mul);

return 0;

7.ASCII Value:

#include <stdio.h>

int main() {

char c;

int choice;

do

// Ask the user to enter a character

printf("Enter a character: ");

scanf(" %c", &c);

// Display the ASCII value of the character

printf("ASCII value of %c = %d\n", c, c);

// Ask the user if they want to continue

printf("Do you want to continue? (1 for Yes / 0 for No): ");

scanf("%d", &choice);

while (choice != 0);

printf("Exiting program...\n");

return 0;

}
C Program

8.calculator :
#include <stdio.h>

int main()

char operator;

char y;

double num1, num2, result;

// Ask the user to input operator

printf("Enter an operator (+, -, *, /): ");

scanf(" %c", &operator);

// Ask the user to input two numbers

printf("Enter two numbers: ");

scanf("%lf %lf", &num1, &num2);

// Perform calculation based on the operator

switch(operator) {

case '+': // Addition

result = num1 + num2;

break;

case '-': // Subtraction

result = num1 - num2;

break;

case '*': // Multiplication

result = num1 * num2;

break;

case '/': // Division


C Program

if (num2 != 0)

result = num1 / num2;

else

printf("Error: Division by zero\n");

return 1; // Exit program with error code

break;

default: // Invalid operator

printf("Error: Invalid operator\n");

return 1; // Exit program with error code

// Output the result of the calculation

printf("Result: %.2lf\n", result);

return 0;

9.Largest & Smallest:


#include<stdio.h>

int main()

int i, n, lar,sm, elem;

printf ("Enter total number of elements n");


C Program

scanf ("%d", &elem);

printf ("Enter first number n");

scanf ("%d", &n);

lar = n;

sm=n;

for (i=1; i<= elem -1 ; i++)

printf ("n Enter another number n");

scanf ("%d",&n);

if (n>lar)

lar=n;

if (n<sm)

sm=n;

printf ("n The largest number is %d", lar);

printf ("n The smallest number is %d", sm);

return 0;

10.Star Pattern :
//heeader file

#include <stdio.h>

//main function

int main()

{
C Program

//variable decleration

int rows, type;

// Get user input for number of rows

printf("Enter the number of rows: ");

if (scanf("%d", &rows) != 1 || rows <= 0)

printf("Invalid input: Please enter a positive integer.\n");

return 1;

// Get user input for pattern type

printf("Select pattern type (1 for right-angled triangle, 2 for inverted triangle, 3 for pyramid): ");

if (scanf("%d", &type) != 1 || type < 1 || type > 3)

printf("Invalid input: Please enter a number between 1 and 3.\n");

return 1;

// Print pattern based on user selection

switch (type) {

case 1:

// Right-angled triangle

for (int i = 1; i <= rows; i++)

for (int j = 1; j <= i; j++)

printf("*");
C Program

printf("\n");

break;

case 2:

// Inverted triangle

for (int i = rows; i >= 1; i--)

for (int j = 1; j <= i; j++)

printf("*");

printf("\n");

break;

case 3:

// Pyramid

for (int i = 1; i <= rows; i++)

// Print spaces for leading spaces

for (int j = 1; j <= rows - i; j++)

printf(" ");

// Print stars for the current row


C Program

for (int j = 1; j <= 2 * i - 1; j++)

printf("*");

printf("\n");

break;

default:

// Should not reach here due to input validation

printf("Invalid pattern type.\n");

break;

return 0;

// calculate the sum

sub = number1 - number2;

printf("%d - %d = %d", number1, number2, sub);

return 0;

11.palindrome:
#include <stdio.h>

int main()

int n, reversed = 0, remainder, original;


C Program

// Ask the user to enter an integer

printf("Enter an integer: ");

scanf("%d", &n);

original = n; // Store the original number

// Reverse the number

while (n != 0)

remainder = n % 10; // Get the last digit of the number

reversed = reversed * 10 + remainder; // Build the reversed number

n /= 10; // Remove the last digit from the original number

// Check if the original number is equal to its reverse

if (original == reversed)

printf("%d is a palindrome.", original); // If equal, it's a palindrome

else

printf("%d is not a palindrome.", original); // If not equal, it's not a palindrome

return 0;

12.Reversed Number:
#include <stdio.h>

int main() {

int n, reversed = 0, remainder;


C Program

// Ask the user to enter a number

printf("Enter a number: ");

scanf("%d", &n);

// Reverse the number

while (n != 0)

remainder = n % 10; // Get the last digit of the number

reversed = reversed * 10 + remainder; // Build the reversed number

n /= 10; // Remove the last digit from the original number

13.Fibonacci Series:
#include <stdio.h>

int main() {

int n, first = 0, second = 1, next, i;

// Ask the user for the number of terms in the Fibonacci series

printf("Enter the number of terms: ");

scanf("%d", &n);

printf("Fibonacci Series: ");

for (i = 0; i < n; i++)

if (i <= 1)

next = i;

else

{
C Program

next = first + second;

first = second;

second = next;

printf("%d ", next);

return 0;

// Display the reversed number

printf("Reversed number: %d", reversed);

return 0;

14.Even And Odd:


#include <stdio.h>

int main()

int num;

// Prompt user to enter an integer

printf("Enter an integer: ");

scanf("%d", &num);

// Check if the number is even or odd

if (num % 2 == 0) {

// Print if the number is even

printf("%d is an even number.\n", num);


C Program

} else

// Print if the number is odd

printf("%d is an odd number.\n", num);

return 0;

15.Prime number:
#include <stdio.h>

int main()

int num, i;

int isPrime = 1; // Assume the number is prime initially

printf("Enter a positive integer: ");

scanf("%d", &num);

if (num <= 1)

printf("%d is not a prime number.\n", num);

} else {

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

if (num % i == 0) {

isPrime = 0; // Set isPrime to false if number is divisible by any other number

break;
C Program

if (isPrime == 1)

printf("%d is a prime number.\n", num);

} else {

printf("%d is not a prime number.\n", num);

return 0;

16. Reverse String:

#include <stdio.h>

#include <string.h>

// Function to reverse the input string

void reverseString(char str[])

int length = strlen(str);

int start = 0;

int end = length - 1;

char temp;
C Program

// Swap characters from start and end of the string

while (start < end)

temp = str[start];

str[start] = str[end];

str[end] = temp;

start++;

end--;

int main() {

char str[100];

// Prompt the user to enter a string

printf("Enter a string: ");

fgets(str, sizeof(str), stdin);

// Remove newline character if present

if (str[strlen(str) - 1] == '\n')

str[strlen(str) - 1] = '\0';

// Display the original string

printf("Original String: %s\n", str);

// Reverse the string

reverseString(str);
C Program

// Display the reversed string

printf("Reversed String: %s\n", str);

return 0;

17. Swapping Numbers :


#include <stdio.h>

int main()

int num1, num2, temp;

// Input two numbers from the user

printf("Enter first number: ");

scanf("%d", &num1);

printf("Enter second number: ");

scanf("%d", &num2);

// Display the numbers before swapping

printf("Before swapping: num1 = %d, num2 = %d\n", num1, num2);

// Swap the numbers using a temporary variable

temp = num1;

num1 = num2;

num2 = temp;

// Display the numbers after swapping

printf("After swapping: num1 = %d, num2 = %d\n", num1, num2);

return 0;

}
C Program

18. Repeated Numbers :


#include <stdio.h>

#include <string.h>

int main() {

char str[100];

int count[10] = {0}; // Array to store count of each digit (0-9)

int len, i;

printf("Enter a string of numbers: ");

fgets(str, sizeof(str), stdin);

// Remove newline character if present

if (str[strlen(str) - 1] == '\n')

str[strlen(str) - 1] = '\0';

len = strlen(str);

// Count the occurrence of each digit in the string

for (i = 0; i < len; i++) {

if (str[i] >= '0' && str[i] <= '9') {

count[str[i] - '0']++;

printf("Repeated numbers in the string: ");

// Display the repeated numbers


C Program

for (i = 0; i < 10; i++) {

if (count[i] > 1) {

printf("%d ", i);

printf("\n");

return 0;

19. Percentage of 5 subjects:

#include <stdio.h>

int main()

float subject1, subject2, subject3, subject4, subject5;

float totalMarks = 500; // Total marks for 5 subjects

float obtainedMarks, percentage;

// Input marks for each subject

printf("Enter marks for Subject 1: ");

scanf("%f", &subject1);

printf("Enter marks for Subject 2: ");

scanf("%f", &subject2);

printf("Enter marks for Subject 3: ");

scanf("%f", &subject3);

printf("Enter marks for Subject 4: ");


C Program

scanf("%f", &subject4);

printf("Enter marks for Subject 5: ");

scanf("%f", &subject5);

// Calculate total obtained marks

obtainedMarks = subject1 + subject2 + subject3 + subject4 + subject5;

// Calculate percentage

percentage = (obtainedMarks / totalMarks) * 100;

// Display the percentage

printf("Percentage: %.2f%%\n", percentage);

return 0;

20. Leap year:


#include <stdio.h>

int main()

int year;

// Input the year from the user

printf("Enter a year: ");

scanf("%d", &year);

// Check if it's a leap year

if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))

printf("%d is a leap year.\n", year);

} else
C Program

printf("%d is not a leap year.\n", year);

return 0;

21.Area of Rectangle :
#include <stdio.h>

int main()

float length, width, area;

// Input the length and width of the rectangle

printf("Enter the length of the rectangle: ");

scanf("%f", &length);

printf("Enter the width of the rectangle: ");

scanf("%f", &width);

// Calculate the area of the rectangle

area = length * width

// Display the calculated area

printf("The area of the rectangle is: %f\n", area);

return 0;

}
C Program

You might also like