Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
115 views

C Programming

The document contains 20 C programs that demonstrate various programming concepts like: 1) Checking if a number is positive, negative or zero. 2) Finding the greatest of three numbers. 3) Displaying the Fibonacci series. 4) Checking if a number is a palindrome. 5) Converting a lowercase string to uppercase. 6) Arranging numbers in ascending order. 7) Checking if a character is a vowel or consonant. 8) Finding the sum of natural numbers using a for loop.

Uploaded by

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

C Programming

The document contains 20 C programs that demonstrate various programming concepts like: 1) Checking if a number is positive, negative or zero. 2) Finding the greatest of three numbers. 3) Displaying the Fibonacci series. 4) Checking if a number is a palindrome. 5) Converting a lowercase string to uppercase. 6) Arranging numbers in ascending order. 7) Checking if a character is a vowel or consonant. 8) Finding the sum of natural numbers using a for loop.

Uploaded by

Alia Rand
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 56

1)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");
}
2) C Program to find greatest of three numbers
BY CHAITANYA SINGH | FILED UNDER: C PROGRAMS
#include<stdio.h>
int main()
{
int num1,num2,num3;

//Ask user to input any three integer numbers


printf("\nEnter value of num1, num2 and num3:");
//Store input values in variables for comparsion
scanf("%d %d %d",&num1,&num2,&num3);

if((num1>num2)&&(num1>num3))
printf("\n Number1 is greatest");
else if((num2>num3)&&(num2>num1))
printf("\n Number2 is greatest");
else
printf("\n Number3 is greatest");
return 0;
}
3) C Program to display Fibonacci series
#include<stdio.h>
int main()
{
int count, first_term = 0, second_term = 1, next_term, i;
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;
}
4) 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;
}
#include<stdio.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;
}
5) C Program to convert lowercase string to uppercase string
#include<stdio.h>
#include<string.h>
int main(){
char str[25];
int i;

printf("Enter the string:");


scanf("%s",str);

for(i=0;i<=strlen(str);i++){
if(str[i]>=97&&str[i]<=122)
str[i]=str[i]-32;
}
printf("\nUpper Case String is: %s",str);
return 0;
}
6) C Program to arrange numbers in ascending order
#include <stdio.h>

void sort_numbers_ascending(int number[], int count)


{
int temp, i, j, k;
for (j = 0; j < count; ++j)
{
for (k = j + 1; k < count; ++k)
{
if (number[j] > number[k])
{
temp = number[j];
number[j] = number[k];
number[k] = temp;
}
}
}
printf("Numbers in ascending order:\n");
for (i = 0; i < count; ++i)
printf("%d\n", number[i]);
}
void main()
{
int i, count, number[20];

printf("How many numbers you are gonna enter:");


scanf("%d", &count);
printf("\nEnter the numbers one by one:");

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


scanf("%d", &number[i]);

sort_numbers_ascending(number, count);
}
7) C Program to Check whether an Alphabet is 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;}
8) 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;
}

9) C Program to find the Sum of First n Natural numbers


#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;
}
10) Program to find quotient and remainder based on the dividend &
divisor values entered by user.
#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;
}
11) Program to multiply two floating point numbers and display the
product as output.
#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;
}
12)C Program to Swap two numbers
#include <stdio.h>
int main()
{
double num1, num2, temp;
printf("Enter First Number: ");
scanf("%lf", &num1);
printf("Enter Second Number: ");
scanf("%lf",&num2);

printf("Before swapping:\n");
//%.2lf is to have two digits after the decimal point
printf("num1 is: %.2lf and num2 is: %.2lf\n", num1, num2);
temp = num1;
num1 = num2;
num2 = temp;
printf("After swapping:\n");
printf("num1 is: %.2lf and num2 is: %.2lf", num1, num2);
return 0;
}
13) C program to find the number of elements in a given array
include <stdio.h>
int main()
{
double arr[] = {11, 22, 33, 44, 55, 66};
int n;
n = sizeof(arr) / sizeof(arr[0]);
printf("Size of the array is: %d\n", n);
return 0;
}
14) C Program to calculate Area and Circumference of Circle
#include <stdio.h>
int main()
{
int circle_radius;
float PI_VALUE=3.14, circle_area, circle_circumf;
printf("\nEnter radius of circle: ");
scanf("%d",&circle_radius);
circle_area = PI_VALUE * circle_radius * circle_radius;
printf("\nArea of circle is: %f",circle_area);
circle_circumf = 2 * PI_VALUE * circle_radius;
printf("\nCircumference of circle is: %f",circle_circumf);

return(0);
}
15) C Program to calculate Area of Equilatral triangle
#include<stdio.h>
#include<math.h>
int main()
{
int triangle_side;
float triangle_area, temp_variable;
printf("\nEnter the Side of the triangle:");
scanf("%d",&triangle_side);
temp_variable = sqrt(3) / 4 ;
triangle_area = temp_variable * triangle_side * triangle_side ;
printf("\nArea of Equilateral Triangle is: %f",triangle_area);
return(0);
}

16) C program check if an integer is prime or not


#include <stdio.h>

int main()
{
int n, c;

printf("Enter a number\n");
scanf("%d", &n);

if (n == 2)
printf("Prime number.\n");
else
{
for (c = 2; c <= n - 1; c++)
{
if (n % c == 0)
break;
}
if (c != n)
printf("Not prime.\n");
else
printf("Prime number.\n");
}
return 0;
}
17) C Program to find Roots of a Quadratic Equation
#include <stdio.h>
#include<math.h>
int main()
{
float a, b, c;
float root1, root2, imaginary, discriminant;

printf("\n Please Enter values of a, b, c of Quadratic Equation :


");
scanf("%f%f%f", &a, &b, &c);

discriminant = (b * b) - (4 * a *c);

if(discriminant > 0)
{
root1 = (-b + sqrt(discriminant) / (2 * a));
root2 = (-b - sqrt(discriminant) / (2 * a));
printf("\n Two Distinct Real Roots Exists: root1 = %.2f
and root2 = %.2f", root1, root2);
}
else if(discriminant == 0)
{
root1 = root2 = -b / (2 * a);
printf("\n Two Equal and Real Roots Exists: root1 = %.2f
and root2 = %.2f", root1, root2);
}
else if(discriminant < 0)
{
root1 = root2 = -b / (2 * a);
imaginary = sqrt(-discriminant) / (2 * a);
printf("\n Two Distinct Complex Roots Exists: root1 =
%.2f+%.2f and root2 = %.2f-%.2f", root1, imaginary, root2,
imaginary);
}

return 0;
}
18) C Program to Print Day Name of Week using Else If Statement
#include <stdio.h>

int main()
{
int weekday;
printf(" Please Enter the Day Number 1 to 7 (Consider 1= Monday,
and 7 = Sunday) : ");
scanf("%d", &weekday);

if (weekday == 1)
{
printf("\n Today is Monday");
}
else if ( weekday == 2 )
{
printf("\n Today is Tuesday");
}
else if ( weekday == 3 )
{
printf("\n Today is Wednesday");
}
else if ( weekday == 4 )
{
printf("\n Today is Thursday");
}
else if ( weekday == 5 )
{
printf("\n Today is Friday");
}
else if ( weekday == 6 )
{
printf("\n Today is Saturday");
}
else if ( weekday == 7 )
{
printf("\n Today is Sunday");
}
else
printf("\n Please enter Valid Number between 1 to 7");

return 0;
}
19) C Program to Print Alphabets from a to z

#include <stdio.h>

int main()
{
char ch;

printf("\n List of Alphabets from a to z are : \n");


for(ch = 'a'; ch <= 'z'; ch++)
{
printf(" %c\t", ch);
}

return 0;
}
20) C program to Compare Two Strings without using strcmp()

#include <stdio.h>
#include <string.h>

int main()
{
char Str1[100], Str2[100];
int result, i;

printf("\n Please Enter the First String : ");


gets(Str1);

printf("\n Please Enter the Second String : ");


gets(Str2);

for(i = 0; Str1[i] == Str2[i] && Str1[i] == '\0'; i++);

if(Str1[i] < Str2[i])


{
printf("\n str1 is Less than str2");
}
else if(Str1[i] > Str2[i])
{
printf("\n str2 is Less than str1");
}
else
{
printf("\n str1 is Equal to str2");
}

return 0;
}
21) C Program to Count Total Number of Words in a String
#include <stdio.h>
#include <string.h>

int main()
{
char str[100];
int i, totalwords;
totalwords = 1;

printf("\n Please Enter any String : ");


gets(str);

for(i = 0; str[i] != '\0'; i++)


{
if(str[i] == ' ' || str[i] == '\n' || str[i] == '\t')
{
totalwords++;
}
}
printf("\n The Total Number of Words in this String %s = %d",
str, totalwords);
return 0;
}
22) C Program to Perform Arithmetic Operations on Arrays
#include<stdio.h>

int main()
{
int Size, i, a[10], b[10];
int Addition[10], Subtraction[10], Multiplication[10], Module[10];
float Division[10];

printf("\n Please Enter the Size of the Array\n");


scanf("%d", &Size);

printf("\nPlease Enter the First Array Elements\n");


for(i = 0; i < Size; i++)
{
scanf("%d", &a[i]);
}

printf("\n Please Enter the Second Array Elements\n");


for(i = 0; i < Size; i ++)
{
scanf("%d", &b[i]);
}

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


{
Addition [i]= a[i] + b[i];
Subtraction [i]= a[i] - b[i];
Multiplication [i]= a[i] * b[i];
Division [i] = a[i] / b[i];
Module [i] = a[i] % b[i];
}

printf("\n Add\t Sub\t Multi\t Div\t Mod");


for(i = 0; i < Size; i++)
{
printf("\n%d \t ", Addition[i]);
printf("%d \t ", Subtraction[i]);
printf("%d \t ", Multiplication[i]);
printf("%.2f \t ", Division[i]);
printf("%d \t ", Module[i]);
}

return 0;
}

23) 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;
}
24) C program to calculate and print the value of nPr
#include <stdio.h>

void main()
{
int n, r, npr_var;

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


scanf("%d", &n);
printf("\nEnter the value of r:");
scanf("%d", &r);

npr_var = fact(n) / fact(n - r);


printf("\nThe value of P(%d,%d) is: %d",n,r,npr_var);
}
int fact(int num)
{
int k = 1, i;
// factorial of 0 is 1
if (num == 0)
{
return(k);
}
else
{
for (i = 1; i <= num; i++)
{
k = k * i;
}
}
return(k);
}
25) Program to calculate simple interest
#include <stdio.h>

int main()
{
float principle, time, rate, SI;

printf("Enter principle (amount): ");


scanf("%f", &principle);

printf("Enter time: ");


scanf("%f", &time);

printf("Enter rate: ");


scanf("%f", &rate);

SI = (principle * time * rate) / 100;

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

return 0;
}
26) C Program to Remove all Characters in a String Except Alphabets
#include <stdio.h>
int main() {
char line[150];

printf("Enter a string: ");


fgets(line, sizeof(line), stdin);

for (int i = 0, j; line[i] != '\0'; ++i) {


while (!(line[i] >= 'a' && line[i] <= 'z') && !(line[i] >= 'A' &&
line[i] <= 'Z') && !(line[i] == '\0')) {
for (j = i; line[j] != '\0'; ++j) {
line[j] = line[j + 1];
}
line[j] = '\0';
}
}
printf("Output String: ");
puts(line);
return 0;
}
27) C program to convert given number of days in terms of Years,
Weeks and Days
int main()
{
int days, years, weeks;
printf("Enter days: ");
scanf("%d", &days);
// Ignoring leap year
years = (days / 365);
weeks = (days % 365) / 7;
days = (days % 365) %7;

printf("YEARS: %d\n", years);


printf("WEEKS: %d\n", weeks);
printf("DAYS: %d", days);
return 0;
}
28) C Program to Store Information of a Student Using Structure

#include <stdio.h>
struct student {
char name[50];
int roll;
float marks;
} s;

int main() {
printf("Enter information:\n");
printf("Enter name: ");
fgets(s.name, sizeof(s.name), stdin);

printf("Enter roll number: ");


scanf("%d", &s.roll);
printf("Enter marks: ");
scanf("%f", &s.marks);

printf("Displaying Information:\n");
printf("Name: ");
printf("%s", s.name);
printf("Roll number: %d\n", s.roll);
printf("Marks: %.1f\n", s.marks);

return 0;
}

29) C Program to Write a Sentence to a File


#include <stdio.h>
#include <stdlib.h>

int main() {
char sentence[1000];

FILE *fptr;
fptr = fopen("program.txt", "w");

if (fptr == NULL) {
printf("Error!");
exit(1);
}
printf("Enter a sentence:\n");
fgets(sentence, sizeof(sentence), stdin);
fprintf(fptr, "%s", sentence);
fclose(fptr);
return 0;
}
30) C Program to Print Pyramids and Patterns
#include <stdio.h>
int main() {
int i, j, rows;
printf("Enter the number of rows: ");
scanf("%d", &rows);
for (i = 1; i <= rows; ++i) {
for (j = 1; j <= i; ++j) {
printf("* ");
}
printf("\n");
}
return 0;
}
31)C Program to Convert Octal Number to Decimal
#include <stdio.h>
#include <math.h>

int convertDecimalToOctal(int decimalNumber);


int main()
{
int decimalNumber;

printf("Enter a decimal number: ");


scanf("%d", &decimalNumber);

printf("%d in decimal = %d in octal", decimalNumber,


convertDecimalToOctal(decimalNumber));

return 0;
}

int convertDecimalToOctal(int decimalNumber)


{
int octalNumber = 0, i = 1;
while (decimalNumber != 0)
{
octalNumber += (decimalNumber % 8) * i;
decimalNumber /= 8;
i *= 10;
}

return octalNumber;
}

32)C Program to Make a Simple Calculator Using switch...case


#include <stdio.h>
int main() {
char operator;
double first, second;
printf("Enter an operator (+, -, *,): ");
scanf("%c", &operator);
printf("Enter two operands: ");
scanf("%lf %lf", &first, &second);

switch (operator) {
case '+':
printf("%.1lf + %.1lf = %.1lf", first, second, first + second);
break;
case '-':
printf("%.1lf - %.1lf = %.1lf", first, second, first - second);
break;
case '*':
printf("%.1lf * %.1lf = %.1lf", first, second, first * second);
break;
case '/':
printf("%.1lf / %.1lf = %.1lf", first, second, first / second);
break;
default:
printf("Error! operator is not correct");
}

return 0;
}
33)C Program to Calculate the Power of a Number
#include <stdio.h>
int main() {
int base, exp;
long long result = 1;
printf("Enter a base number: ");
scanf("%d", &base);
printf("Enter an exponent: ");
scanf("%d", &exp);

while (exp != 0) {
result *= base;
--exp;
}
printf("Answer = %lld", result);
return 0;
}
34) C Program to Find LCM of two Numbers
#include <stdio.h>
int main() {
int n1, n2, max;
printf("Enter two positive integers: ");
scanf("%d %d", &n1, &n2);

// maximum number between n1 and n2 is stored in min


max = (n1 > n2) ? n1 : n2;

while (1) {
if (max % n1 == 0 && max % n2 == 0) {
printf("The LCM of %d and %d is %d.", n1, n2, max);
break;
}
++max;
}
return 0;
}

35) wap to store the information entered by the user using dynamic
memory allocation.
#include <stdio.h>
#include <stdlib.h>
struct course {
int marks;
char subject[30];
};

int main() {
struct course *ptr;
int i, noOfRecords;
printf("Enter the number of records: ");
scanf("%d", &noOfRecords);
ptr = (struct course *)malloc(noOfRecords * sizeof(struct course));
for (i = 0; i < noOfRecords; ++i) {
printf("Enter the name of the subject and marks respectively:\n");
scanf("%s %d", (ptr + i)->subject, &(ptr + i)->marks);
}

printf("Displaying Information:\n");
for (i = 0; i < noOfRecords; ++i)
printf("%s\t%d\n", (ptr + i)->subject, (ptr + i)->marks);
return 0;
}
36) wap to take two complex numbers as structures and add them by
creating a user-defined function.
include <stdio.h>
typedef struct complex {
float real;
float imag;
} complex;

complex add(complex n1, complex n2);

int main() {
complex n1, n2, result;

printf("For 1st complex number \n");


printf("Enter the real and imaginary parts: ");
scanf("%f %f", &n1.real, &n1.imag);
printf("\nFor 2nd complex number \n");
printf("Enter the real and imaginary parts: ");
scanf("%f %f", &n2.real, &n2.imag);

result = add(n1, n2);


printf("Sum = %.1f + %.1fi", result.real, result.imag);
return 0;
}

complex add(complex n1, complex n2) {


complex temp;
temp.real = n1.real + n2.real;
temp.imag = n1.imag + n2.imag;
return (temp);
}
37)
#include <math.h>
#include <stdio.h>
float calculateSD(float data[]);
int main() {
int i;
float data[10];
printf("Enter 10 elements: ");
for (i = 0; i < 10; ++i)
scanf("%f", &data[i]);
printf("\nStandard Deviation = %.6f", calculateSD(data));
return 0;
}

float calculateSD(float data[]) {


float sum = 0.0, mean, SD = 0.0;
int i;
for (i = 0; i < 10; ++i) {
sum += data[i];
}
mean = sum / 10;
for (i = 0; i < 10; ++i)
SD += pow(data[i] - mean, 2);
return sqrt(SD / 10);
}
38) C Program to Find Transpose of a Matrix
#include <stdio.h>
int main() {
int a[10][10], transpose[10][10], r, c, i, j;
printf("Enter rows and columns: ");
scanf("%d %d", &r, &c);
printf("\nEnter matrix elements:\n");
for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j) {
printf("Enter element a%d%d: ", i + 1, j + 1);
scanf("%d", &a[i][j]);
}

printf("\nEntered matrix: \n");


for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j) {
printf("%d ", a[i][j]);
if (j == c - 1)
printf("\n");
}

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


for (j = 0; j < c; ++j) {
transpose[j][i] = a[i][j];
}

printf("\nTranspose of the matrix:\n");


for (i = 0; i < c; ++i)
for (j = 0; j < r; ++j) {
printf("%d ", transpose[i][j]);
if (j == r - 1)
printf("\n");
}
return 0;
}
39) wap for Factors of a Positive Integer
#include <stdio.h>
int main() {
int num, i;
printf("Enter a positive integer: ");
scanf("%d", &num);
printf("Factors of %d are: ", num);
for (i = 1; i <= num; ++i) {
if (num % i == 0) {
printf("%d ", i);
}
}
return 0;
}
40) wap to reverse the number entered by the user.
#include <stdio.h>
int main() {
int n, rev = 0, remainder;
printf("Enter an integer: ");
scanf("%d", &n);
while (n != 0) {
remainder = n % 10;
rev = rev * 10 + remainder;
n /= 10;
}
printf("Reversed number = %d", rev);
return 0;
}

You might also like