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

C Programs

Uploaded by

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

C Programs

Uploaded by

paramjot341
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

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. Program to find largest number using if statement

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

int num1, num2, num3;

printf("Enter first number: ");


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

if (num1 >= num2 && num1 >= num3)


printf("%d is the largest number.", num1);

if (num2 >= num1 && num2 >= num3)


printf("%d is the largest number.", num2);

if (num3 >= num1 && num3 >= num2)


printf("%d is the largest number.", num3);

return 0;
}
3. Factorial Program in C

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

int num, fact=1,i;

printf("Enter number: ");


scanf("%d", &num);
for(i=1;i<=num;i++)
{
fact=fact*i;

}
printf("Factorial of %d is %d",num,fact);

return 0;
}

4. Program to Print ASCII Value


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

5. C Program to swap two numbers without third variable

#include<stdio.h>
int main()
{
int a=10, b=20;
printf("Before swap a=%d b=%d",a,b);
a=a+b;
b=a-b;
a=a-b;
printf("\nAfter swap a=%d b=%d",a,b);
return 0;
}
6. Program to Check Leap Year
#include <stdio.h>

int main()
{
int year;
printf("Enter year:");
scanf("%d",&year);
if((year%4==0) && (year%100!=0)||(year%400==0))
printf("%d year is a Leap Year",year);
else
printf("%d year is not a Leap Year",year);
return 0;
}

7. Program to Print English Alphabets


#include <stdio.h>
#include <conio.h>
void main() {
char c;
for (c = 'A'; c <= 'Z'; ++c)
printf("%c ", c);
getch();
}

8. Program to Check Vowel or consonant


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

// evaluates to 1 if variable c is a lowercase vowel


lowercase_vowel = (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u');

// evaluates to 1 if variable c is a uppercase vowel


uppercase_vowel = (c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U');

// evaluates to 1 (true) if c is a vowel


if (lowercase_vowel || uppercase_vowel)
printf("%c is a vowel.", c);
else
printf("%c is a consonant.", c);
return 0;
}

9. Program to find the area and circumference of a circle.


#include<stdio.h>

void main()
{
int r;
float PI = 3.14, area, ci;
printf("\nEnter the radius of the circle: ");
scanf("%d", &r);
area = PI*r*r;
printf("\n\n\n Area of the circle is: %f ", area);
ci = 2*PI*r;
printf("\n\n\n Circumference of the circle is: %f", ci);

10. C Program to find the Area of Triangle using Base and Height

#include<stdio.h>
void main()
{
int h, b;
float area;
printf("\n\nEnter the base and height of the Triangle: ");
scanf("%d%d",&b, &h);

area = 0.5*(h*b);
printf("\n\n\nThe area of the triangle is: %f", area);
}

11. C Program to convert Temperature in Celsius to Fahrenheit


#include<stdio.h>
void main()
{
float celsius, fahrenheit;
printf("\n\nEnter temperature in Celsius: ");
scanf("%f", &celsius);

fahrenheit = (1.8*celsius) + 32;

printf("\n\n\nTemperature in Fahrenheit is: %f ", fahrenheit);


}

12. C Program to convert Temperature in Fahrenheit to Celsius

#include<stdio.h>
void main()
{
float cel, fah;
printf("\n\nEnter temperature in fah: ");
scanf("%f", &fah);

cel = (fah - 32)*5/9;

printf("\n\n\nTemperature in cel is: %f ", cel);


}

13. Kilometre to Meter


#include<stdio.h>

int main()
{
float km, m;

printf("Enter distance in kilometer: ");


scanf("%f", &km);

m = km * 1000;

printf("%0.3f Kilometer = %0.3f Meter", km, m);

return 0;
}

14. Program to print ODD numbers from 1 to N using while loop


#include <stdio.h>
void main()
{
int i=1, n;
printf("Enter the value of n: ");
scanf("%d",&n);
printf("Odd Numbers from 1 to %d:\n",n);
while(i<=n)
{
if(i%2 != 0)
printf("%d ",i);
i++;
}
}

15. C program to find sum of all natural numbers.


Series: 1+2+3+4+….N
#include<stdio.h>
void main()
{
int i,N,sum=0;
printf("Enter the value of N: ");
scanf("%d",&N);
for(i=1;i<=N;i++)
sum= sum + i;
printf("Sum of the series is: %d\n",sum);
}

16. C program to find sum of the square of all natural numbers from 1 to N.
Series: 1^2+2^2+3^2+4^2+..N^2

#include<stdio.h>
int main()
{
int i,N;
int sum=0;
printf("Enter the value of N: ");
scanf("%d",&N);
for(i=1;i<=N;i++)
sum= sum+ (i*i);
printf("Sum of the series is: %d\n",sum);
return 0;
}

17. C program to find sum of following series


1+ 1/2 + 1/3 + 1/4 + 1/5 + .. 1/N
#include<stdio.h>
int main()
{
int N;
float i, sum=0.0f;
printf("Enter the value of N: ");
scanf("%d",&N);
for(i=1;i<=N;i++)
sum = sum + (1/i);
printf("Sum of the series is: %f\n",sum);
return 0;
}

18. C program to print Perfect square of number within given range.

#include<stdio.h>
#include<math.h>
int main()
{
int n1,n2,i,p;
printf("Enter limit");
scanf("%d%d",&n1,&n2);
for(i=n1;i<=n2;i++){
p=sqrt(i);
if(p*p==i)
{
printf("%d\n",i);
}
}
return 0;
}

19. Reverse of a number.


#include<stdio.h>
int main()
{
int x, y=0, r;
printf("Enter number:");
scanf("%d",&x);
while(x!=0){
r=x%10;
y=y*10+r;
x=x/10;
}
printf("Reverse is %d",y);
return 0;
}

20. WAP in C to check whether a number is Prime or not?


#include <stdio.h>
int main()
{
int n, count=0;
printf("Enter Number");
scanf("%d",&n);
for(int i=1;i<=n;i++){
if(n%i==0){
count++;
}
}
if(count==2)
printf("Prime Number");
else
printf("Not a Prime Number");
return 0;
}

21. WAP in C to check whether a number is Palindrome or not?


#include <stdio.h>
int main() {
int n, n1, rev = 0, rem;

printf("Enter any number: ");


scanf("%d", &n);
n1 = n;

while (n > 0){


rem = n % 10;
rev = rev * 10 + rem;
n = n / 10;
}

if (n1 == rev){
printf("Given number is a palindrome number");
}
else{
printf("Given number is not a palindrome number");
}
return 0;
}

22. Fibonacci Series program in C: 0, 1, 1, 2, 3, 5, 8, 13,….


#include <stdio.h>
int main() {
int n, i, a=0,b=1,c;
printf("Enter Fibonacci Range: ");
scanf("%d", &n);
for(i=1;i<=n;i++){
printf("%d ",a);
c=a+b;
a=b;
b=c;
}

return 0;
}

23. WAP to check number is Armstrong or not?


#include<stdio.h>
int main()
{
int n, r, sum=0, temp;
printf("enter the number=");
scanf("%d",&n);
temp=n;
while(n>0)
{
r=n%10;
sum=sum+(r*r*r);
n=n/10;
}
if(temp==sum)
printf("armstrong number ");
else
printf("not armstrong number");
return 0;
}

24. Percentage and Grades Program in C

#include<stdio.h>
void main()
{
int hindi, english, science,math,computer,sum ;
float per;
printf("Enter marks of Hindi=");
scanf("%d",&hindi);
printf("Enter marks of English=");
scanf("%d",&english);
printf("Enter marks of Science=");
scanf("%d",&science);
printf("Enter marks of Math=");
scanf("%d",&math);
printf("Enter marks of Computer=");
scanf("%d",&computer);

sum=hindi+english+science+math+computer;
printf("\nSum of marks=%d",sum);

per=(float)sum/5;
printf("\nPercentage of marks=%f",per);

if(per>=90&&per<=100)
{
printf("\nGrade A");
}
else if(per>=80&&per<90)
{
printf("\nGrade B");
}
else if(per>=60&&per<80)
{
printf("\nGrade C");
}
else if(per<60)
{
printf("\nGrade D");
}
}

25. WAP to find square root of a number.


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

int main() {
double number, squareRoot;

printf("Enter a number: ");


scanf("%lf", &number);

squareRoot = sqrt(number);

printf("Square root of %.2lf = %.2lf", number, squareRoot);

return 0;
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy