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

C Programming

Uploaded by

swayamtyagi34
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)
34 views

C Programming

Uploaded by

swayamtyagi34
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

Aim: Write a program in c to display your credential on screen.

Program:
#include<stdio.h>
void main(){

printf("Name : Maheshwar Giri \nCource : BCA \nSemester : 2nd


\nSubject : C Programming ");

Output :
Aim: Write a program in c to calculate the area of circle.

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

float pi=3.141 ,r;

printf("Enter a Radius of Circle : ");


scanf("%f",&r);
printf("Area of Circle is %.2f",pi*r*r);
}

Output :
Aim: Write a program in c to calculate simple interest.

Program:
#include<stdio.h>
int main(){
float r,s;
int p,t;

printf("Enter Principle : ");


scanf("%d",&p);
printf("Enter Rate : ");
scanf("%f",&r);
printf("Enter Time : ");
scanf("%d",&t);
s=(p*r*t)/100;
printf("Simple Interest %.2f",s);
}

Output :
Aim: Write a program in c to find if a given year is leap year or not.

Program:
#include<stdio.h>
int main(){
int year;

printf("Enter the Year : ");


scanf("%d",&year);

if(year % 400 == 0){


printf("%d is a Leap Year.",year);
}
else if(year % 100 == 0){
printf("%d is not a Leap Year.",year);
}
else if(year % 4 ==0){
printf("%d is a Leap Year.",year);
}
else{
printf("%d is not Leap Year.",year);
}
}
Output :
Aim: Write a program in c to convert.
1. Mass from pounds to kg.

Program :
#include<stdio.h>
int main(){
float kg = 0.45359237;
float p ;

printf("Mass in Pounds : ");


scanf("%f",&p);

printf("\n%.1f Pounds = %f Kilograms.", p, p * kg );


}

Output :
2. Convert distance from miles to kilometer.

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

float km = 1.609344;
float m ;

printf("Distance in Miles : ");


scanf("%f",&m);

printf("\n%.1f Miles = %.f Kilometers.", m , m * km);


}

Output :
3. Temperature from degree Fahrenheit to degree Celsius.

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

float f,c;

printf("Temperature in Fahrenheit : ");


scanf("%f",&f);

c=((f-32)*5)/9;

printf("\n%.1f Fahrenheit = %f Celsius.",f,c);


}

Output :
Aim: Write a program in c to calculate the bill from the no
Of units consume.

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

float unit ;

printf(" Enter No. of unit Consumed : ");


scanf("%f",&unit);

if(unit>=0 && unit<=50){


printf("\nElectricity Bill is %.2f Rs.",unit * 3.50 );
}
else if(unit>50 && unit<=150){
printf("\nElectricity Bill is %.2f Rs.",unit * 3.50 + (unit - 50) *4 );
}
else if(unit>150 && unit<=250){
printf("\nElectricity Bill is %.2f Rs.",unit * 3.50 + 100 * 4 + (unit - 100) * 5.20 );
}
else if(unit > 250){
printf("\nElectricity Bill is %.2f Rs.",unit * 3.50 + 100 * 4 + 100 + 5.20 + (unit -
250) * 6.50 );
}
else{
printf("\nPlease Re-enter valid consumed units...");
}
}

Output :
Aim: Write a program in c to find the roots of a Quadratic
equation.

Program :
#include <stdio.h>
#include <math.h>
int main(){
float a, b, c;
float root1, root2, imaginary;
float discriminant;

printf("Enter values of a, b, c of quadratic equation (aX^2 + bX + c): ");


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("Two distinct and real roots exists: %.2f and %.2f", root1, root2);
}
else if(discriminant == 0){
root1 = root2 = -b / (2 * a);

printf("Two equal and real roots exists: %.2f and %.2f", root1, root2);
}
else if(discriminant < 0){
root1 = root2 = -b / (2 * a);
imaginary = sqrt(-discriminant) / (2 * a);

printf("Two distinct complex roots exists: %.2f + i%.2f and %.2f - i%.2f",
root1, imaginary, root2, imaginary);
}
}

Output :
Aim: Write a program in c to find the tallest of 3 friends.

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

char n1[10],n2[10],n3[10];
float h1,h2,h3,t;

printf("\nEnter Name And Height (in cm) : ");


scanf("%s%f",&n1,&h1);
printf("\nEnter Name And Height (in cm) : ");
scanf("%s%f",&n2,&h2);
printf("\nEnter Name And Height (in cm) : ");
scanf("%s%f",&n3,&h3);

if(h1>h2 && h1>h3){


printf("\n%s's Height is %.1f and he is Taller then %s and %s .",n1,h1,n2,n3);
}
else if(h2>h1 && h2>h3){
printf("\n%s's Height is %.1f and he is Taller then %s and %s .",n2,h2,n1,n3);
}
else if(h3>h1 && h3>h2){
printf("\n%s's Height is %.1f and he is Taller then %s and %s .",n3,h3,n1,n2);
}
else{
printf("Invalid Name or Height Please Re-enter");
}
}
Output :
Aim: Write a program in c to display the month of a year according to
a number using switch case statement.

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

int month ;

printf("Enter Month Number (1-12) : ");


scanf("%d",&month);
switch (month){

case 1:
printf("\n January ");
break;

case 2:
printf("\n February ");
break;

case 3:
printf("\n March ");
break;

case 4:
printf("\n April ");
break;

case 5:
printf("\n May ");
break;

case 6:
printf("\n June ");
break;
case 7:
printf("\n July ");
break;

case 8:
printf("\n Augest ");
break;

case 9:
printf("\n September ");
break;

case 10:
printf("\n October ");
break;

case 11:
printf("\n November ");
break;

case 12:
printf("\n December ");
break;

default:
printf("\n Invalid Month Number Please re-enter");
}
}
Output :
Aim: Write a program in c to display the table of a number using loops.

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

int n,i;

printf("Enter Number : ");


scanf("%d",&n);

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


printf("\n%d X %d = %d \n",n,i,i*n);
}
}

Output :
Aim: Write a program in c to swaps two number using function.

Program:
#include<stdio.h>
int swap(int x,int y){
int temp;

temp = x ;
x=y;
y = temp ;

printf("\nAfter Swaping Numbers a = %d and b = %d.",x,y);


}

void main(){
int a,b ;

printf("Enter a Number a : ");


scanf("%d",&a);
printf("Enter a Number b : ");
scanf("%d",&b);

printf("\nBefore Swaping Numbers a = %d and b = %d.",a,b);


swap(a,b);
}

Output :
Aim : Write a program in c to find it a number is prime or not.

Program :
#include<stdio.h>
int main(){
int num,flag,i;

printf("Enter a Integer Number : ");


scanf("%d",&num);

if(num==0 || num==1){
flag = 1;
}
for(i=2; i<=num/2; i++){
if( num % i == 0 ){
flag = 1;
break;
}
}
if(flag == 1){
printf("\n%d is Not a prime Number.",num);
}
else{
printf("\n%d is a Prime Number. ",num);
}
}

Output :
Aim : Write a program in c to generate a pyramid of stars.

Program :
#include<stdio.h>
int main(){
int n,i,j,s,k,l,S;

printf("Enter a Number : ");


scanf("%d",&n);

for(i=1; i<=n; i++){


for(s=n-1; s>=i; s--){
printf(" ");
}
for(j=i; j>=1; j--){
printf(" * ");
}
printf("\n");
}
}

Output :
Aim: Write a program in c to find if a sequence is Palindrone or not.

Program :
#include<stdio.h>
int main(){
int num,temp,rem,rev=0;

printf("Enter a Integer Number : ");


scanf("%d",&num);
temp=num;

while(temp>0){
rem = temp % 10 ;
rev = (rev * 10) + rem ;
temp = temp / 10 ;
}
if(num == rev){
printf("\n%d is a Palindrone Number.",num);
}
else{
printf("\n%d is Not a Palindrone Number.",num);
}
}

Output :
Aim: Write a program in c to create a calculator that can perform basic
arithmetic operation.

Program :
#include<stdio.h>
int add(int a,int b){
int c;

c=a+b;

return c;
}
int sub(int a, int b){
int c;

c=a-b;

return c;
}
int mult(int a, int b){
int c;

c=a*b;

return c;
}
int div(int a, int b){
int c;

c=a/b;

return c;
}
int main(){
int a,b,ch;
printf("Calculator :---\n\t 1. Addition\n\t 2. Subtraction\n\t 3.
Multiplication\n\t 4. Divide\n\t 5. Exit\n");

while (1){

printf("\nEnter Your Problem (1-5): ");


scanf("%d",&ch);

if(ch == 1){
printf("\nEnter Two Number : ");
scanf("%d %d",&a,&b);
printf("\nAddition : %d + %d = %d\n",a,b,add(a,b));}
else if(ch == 2){
printf("\nEnter Two Number : ");
scanf("%d %d",&a,&b);
printf("\nSubtraction : %d - %d = %d\n",a,b,sub(a,b));}
else if(ch == 3){
printf("\nEnter Two Number : ");
scanf("%d %d",&a,&b);
printf("\nMulitiplication : %d x %d = %d\n",a,b,mult(a,b));}
else if(ch == 4){
printf("\nEnter Two Number : ");
scanf("%d %d",&a,&b);
printf("\nDivide : %d / %d = %d\n",a,b,div(a,b));}
else if(ch == 5){
printf("\n\tEXIT\n");
break;}
else
printf("\nInvalid Input\n");
}
}
Output :
Aim: Write a program in c to allies grades to a student according to
their percentage.

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

int marks;

printf("\n\nEnter your Marks in Percentage : ");


scanf("%d",&marks);

if(marks<0 || marks>100){
printf("\n Wrong Entry \n");
}
else if(marks<30){
printf("\n Grade F \n");
}
else if(marks<50){
printf("\n Grade E \n");
}
else if(marks>=50 && marks<60){
printf("\n Grade D \n");
}
else if(marks>=60 && marks<70){
printf("\n Grade C \n");
}
else if(marks>=70 && marks<80){
printf("\n Grade B \n");
}
else if(marks>=80 && marks<90){
printf("\n Grade A \n");
}
else{
printf("\n Grade A+ \n");
}
}
Output :

You might also like