C-PRG
Coming soon with All the important program
Click here for download C file.
2. Find the area and perimeter of square and rectangle. Input the side(s) through the keyboard.
Unit 1 programs with solution and .c extention file..
1. Find the Simple Interest. Inputs are principal amount, period in year and rate of interest.
ANS )
#include<stdio.h>
#include<conio.h>
void main()
{
long int i=0,p=0,r=0,n=0;
clrscr();
printf("Enter Princpal Amount = ");
scanf("%ld",&p);
printf("Enter Rate of Interest = ");
scanf("%ld",&r);
printf("Enter Time period(In Months) = ");
scanf("%ld",&n);
i=(p*r*n)/100;
printf("Interest = %ld",i);
getch();
}
#include<stdio.h>
#include<conio.h>
void main()
{
long int i=0,p=0,r=0,n=0;
clrscr();
printf("Enter Princpal Amount = ");
scanf("%ld",&p);
printf("Enter Rate of Interest = ");
scanf("%ld",&r);
printf("Enter Time period(In Months) = ");
scanf("%ld",&n);
i=(p*r*n)/100;
printf("Interest = %ld",i);
getch();
}
Click here for download C file.
2. Find the area and perimeter of square and rectangle. Input the side(s) through the keyboard.
ANS :
#include<stdio.h>
#include<conio.h>
void main()
{
int l=0,w=0,spri=0,sarea=0,rpri=0,rarea=0;
clrscr();
printf("Enter value of l = ");
scanf("%d",&l);
printf("Enter value of w = ");
scanf("%d",&w);
spri=4*l;
sarea=l*l;
rpri=2*(l+w);
rarea=l*w;
printf("\nPrimeter of Square = %d",spri);
printf("\nArea of Square = %d",sarea);
printf("\nPrimeter of Rectangle = %d",rpri);
printf("\nArea of Rectengle = %d",rarea);
getch();
}
#include<stdio.h>
#include<conio.h>
void main()
{
int l=0,w=0,spri=0,sarea=0,rpri=0,rarea=0;
clrscr();
printf("Enter value of l = ");
scanf("%d",&l);
printf("Enter value of w = ");
scanf("%d",&w);
spri=4*l;
sarea=l*l;
rpri=2*(l+w);
rarea=l*w;
printf("\nPrimeter of Square = %d",spri);
printf("\nArea of Square = %d",sarea);
printf("\nPrimeter of Rectangle = %d",rpri);
printf("\nArea of Rectengle = %d",rarea);
getch();
}
ANS:
#include<stdio.h>
#include<conio.h>
void main()
{
int i,max=0,n,num;
clrscr();
printf("Enter how many number you want to compare = ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("Enter num %d = ",i);
scanf("%d",&num);
if(i==1)
{max=num;}
if(num>max)
{
max=num;
}
}
printf("\n Max = %d",max);
getch();
}
Click here for download C file.
4. Write a program to enter the temperature in Fahrenheit and convert it to Celsius.[C = ((F-32)*5)/9].
#include<stdio.h>
#include<conio.h>
void main()
{
int i,max=0,n,num;
clrscr();
printf("Enter how many number you want to compare = ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("Enter num %d = ",i);
scanf("%d",&num);
if(i==1)
{max=num;}
if(num>max)
{
max=num;
}
}
printf("\n Max = %d",max);
getch();
}
Click here for download C file.
4. Write a program to enter the temperature in Fahrenheit and convert it to Celsius.[C = ((F-32)*5)/9].
ANS :
#include<stdio.h>
#include<conio.h>
void main()
{
float f,c;
clrscr();
printf("Enter Temp in Fahrenheit = ");
scanf("%f",&f);
c=((f-32)*5)/9;
printf("Celsius = %f",c);
getch();
}
Click here for download C file.
#include<stdio.h>
#include<conio.h>
void main()
{
float f,c;
clrscr();
printf("Enter Temp in Fahrenheit = ");
scanf("%f",&f);
c=((f-32)*5)/9;
printf("Celsius = %f",c);
getch();
}
Click here for download C file.
5. Write a program to store and interchange two numbers in variables a and b.
ANS )
#include<stdio.h>
#include<conio.h>
void main()
{
int n1,n2,temp;
clrscr();
printf("Enter 2 numbers = ");
scanf("%d %d",&n1,&n2);
temp=n1;
n1=n2;
n2=temp;
printf("\nN1 = %d",n1);
printf("\nN2 = %d",n2);
getch();
}
Click here for download C file.
#include<stdio.h>
#include<conio.h>
void main()
{
int n1,n2,temp;
clrscr();
printf("Enter 2 numbers = ");
scanf("%d %d",&n1,&n2);
temp=n1;
n1=n2;
n2=temp;
printf("\nN1 = %d",n1);
printf("\nN2 = %d",n2);
getch();
}
Click here for download C file.
6. Write a program to accept an integer and display it in octal and hexadecimal formats.
ANS )
#include<stdio.h>
#include<conio.h>
void main()
{
int n1;
clrscr();
printf("Enter number = ");
scanf("%d",&n1);
printf("\nOctal form = %o",n1);
printf("\nHexadecimal form = %x",n1);
printf("\nHexadecimal form in Capital = %X",n1);
getch();
}
Click here for download C file.
#include<stdio.h>
#include<conio.h>
void main()
{
int n1;
clrscr();
printf("Enter number = ");
scanf("%d",&n1);
printf("\nOctal form = %o",n1);
printf("\nHexadecimal form = %x",n1);
printf("\nHexadecimal form in Capital = %X",n1);
getch();
}
Click here for download C file.
7. Write a program to enter text with gets() and display it using printf() statement also find the length of the text.
ANS )
#include<stdio.h>
#include<conio.h>
void main()
{
char text[20];
int i;
clrscr();
printf("Enter String = ");
gets(text);
for(i=0; text[i] !='\0';i++);
printf("Legnth of %s = %d",text,i);
getch();
}
Click here for download C file.
#include<stdio.h>
#include<conio.h>
void main()
{
char text[20];
int i;
clrscr();
printf("Enter String = ");
gets(text);
for(i=0; text[i] !='\0';i++);
printf("Legnth of %s = %d",text,i);
getch();
}
Click here for download C file.
8. Write a program to enter two numbers and find the smallest out of them. Use conditional operator.
ANS )
#include<stdio.h>
#include<conio.h>
void main()
{
int n1,n2,temp;
clrscr();
printf("Enter 2 numbers = ");
scanf("%d %d",&n1,&n2);
if(n1>n2)
printf("\n%d is smallest",n2);
else
printf("\n%d is smallest",n1);
getch();
}
#include<stdio.h>
#include<conio.h>
void main()
{
int n1,n2,temp;
clrscr();
printf("Enter 2 numbers = ");
scanf("%d %d",&n1,&n2);
if(n1>n2)
printf("\n%d is smallest",n2);
else
printf("\n%d is smallest",n1);
getch();
}
Click here for download C file.
9. Write a program to enter a number and carry out modular division operation by 2, 3 and 4 and display the remainders.
9. Write a program to enter a number and carry out modular division operation by 2, 3 and 4 and display the remainders.
ANS )
#include<stdio.h>
#include<conio.h>
void main()
{
int num,i,rem;
clrscr();
printf("Enter a number = ");
scanf("%d",&num);
for(i=2;i<5;i++)
{
rem=num%i;
printf("\nReminder when %d modulas by %d = %d",num,i,rem);
}
getch();
}
Click here for download C file.
#include<stdio.h>
#include<conio.h>
void main()
{
int num,i,rem;
clrscr();
printf("Enter a number = ");
scanf("%d",&num);
for(i=2;i<5;i++)
{
rem=num%i;
printf("\nReminder when %d modulas by %d = %d",num,i,rem);
}
getch();
}
Click here for download C file.
10. Write a program to find the average temperature of five sunny days. Assume the temperature in Celsius.
ANS )
#include<stdio.h>
#include<conio.h>
void main()
{
int i;
float d,avg;
clrscr();
for(i=1;i<6;i++)
{
printf("Enter Temp of day %d= ",i);
scanf("%f",d);
avg=avg+d;
}
printf("Average Temp of five sunny days is = %f",avg/5);
getch();
}
Click here for download C file.
1.Write a program to accept number of seconds and display its corresponding hours, minutes and seconds .
Ans)
#include<stdio.h>
#include<conio.h>
void main()
{
int sec,min,hour,temp;
clrscr();
printf("Enter secounds = ");
scanf("%d",&temp);
hour=(temp/3600);
min=(temp-(3600*hour))/60;
sec=(temp-(3600*hour)-(60*min));
printf("Hours= %d \nMinutes = %d \nSecounds = %d",hour,min,sec);
getch();
}
Click here for download C file.
2. Write a C program to find the maximum from given three numbers (Using Nested IF).
Ans)
#include<stdio.h>
#include<conio.h>
void main()
{
int n1,n2,n3;
clrscr();
printf("Enter 3 numbers = ");
scanf("%d %d %d",&n1,&n2,&n3);
if(n1>n2)
{
if(n1>n3)
{
printf("%d is largest",n1);
}
else
{
printf("%d is largest",n3);
}
}
else
{
if(n2>n3)
{
printf("%d is largest",n2);
}
else
{
printf("%d is largest",n3);
}
}
getch();
}
Click here for download C file.
3. Write a C program to find that the accepted no is Negative, Positive or Zero.
Ans)
// no is nagative zero or positive
#include<stdio.h>
#include<conio.h>
void main()
{
int n1;
clrscr();
printf("Enter number = ");
scanf("%d",&n1);
if(n1==0)
{
printf("Entered Num is Zero");
else if(n1>0)
{
printf("%d is Positive",n1);
}
else if
{
printf("%d is Nagtive",n1);
}
}
getch();
}
4. Write a program to check given year is a Leap year or not.
Ans)
//Leap year or not
#include<stdio.h>
#include<conio.h>
void main()
{
int n1;
clrscr();
printf("Enter year = ");
scanf("%d",&n1);
if( n1%2 == 0)
{
printf("%d is leap Year",n1);
}
else
{
printf("%d is not leap year",n1);
}
getch();
}
Click here for download C file.
5. Write a C program to find minimum from given 3 numbers (Using Conditional Operator).
Ans)
//largest out of 3 num using else-if ledder
#include<stdio.h>
#include<conio.h>
void main()
{
int n1,n2,n3;
clrscr();
printf("Enter 3 numbers = ");
scanf("%d %d %d",&n1,&n2,&n3);
if(n1>n2 && n1>n3)
{
printf("%d is largest",n1);
}
else if(n2>n1 && n2>n3)
{
printf("%d is largest",n2);
}
else
{
printf("%d is largest",n3);
}
getch();
}
6. Write a C program to find the maximum from given three numbers (Without using Nested if, or Logical
Operator, Or Conditional operators).
Ans)
//usinf ternary operators
#include<stdio.h>
#include<conio.h>
void main()
{
int n1,n2,n3,ans;
clrscr();
printf("Enter 3 numbers = ");
scanf("%d %d %d",&n1,&n2,&n3);
ans=n1>n2?(n1>n3?n1:n3):(n2>n3?n2:n3);
printf("Largest num is = %d ",ans);
getch();
}
Click here for download C file.
7. Take marks from the user and print grade accordingly( >=75 marks – Distinction, <75 and >=60 marks – First, <60 and >=50 – Second, <50 and >=35 – Pass, <35 – Fail) using if … else if….else statement and also by using logical operators).
Ans)
//take marks as inoput and print grade
#include<stdio.h>
#include<conio.h>
void main()
{
int sci,eng,maths,total;
float per;
clrscr();
printf("\nEnter the sci,maths and english marks : ");
scanf("%d %d %d",&sci,&maths,&eng);
total=eng+maths+sci;
printf("\nTotal marks = %d",total);
per=total/3;
printf("\nPercentage = %.2f\n",per);
if(per>=75)
{
printf("Distinction");
}
else if(per<75 && per>=60)
{
printf("First");
}
else if(per<60 && per>=50)
{
printf("Second");
}
else if(per<50 && per>=35)
{
printf("Pass");
}
else
{
printf("Fail");
}
getch();
}
Click here for download C file.
8. Take 2 numbers from the user and print the greater number (Number can be equal).
Ans)
//check num is equal or greater
#include<stdio.h>
#include<conio.h>
void main()
{
int no1,no2;
clrscr();
printf("\nEnter two num = ");
scanf("%d %d",&no1,&no2);
if(no1==no2)
{
printf("\nNumbers are Equal.");
}
else if(no1>no2)
{
printf("\n%d is greater than %d",no1,no2);
}
else
{
printf("%d is greater than %d",no2,no1);
}
getch();
}
Click here for download C file.
9. Write a program to check whether the blood donor is eligible or not for donating blood. The conditions laid down are as under. Use if statement. a) Age should be above 18 yrs but not more than 55 yrs.
Ans)
//Eligible for donate blood or not
#include<stdio.h>
#include<conio.h>
void main()
{
int age;
clrscr();
printf("\nEnter Your Age = ");
scanf("%d",&age);
if(age>18 && age<=55)
{
printf("\nYou are Eligible");
}
else
{
printf("You are not Eligible");
}
getch();
}
Click here for download C file.
10. Write a program to calculate bill of a job work done as follows. Use if else statement. a) Rate of typing 3 Rs/page b) Printing of 1st copy 5Rs/pages & later every copy 3Rs/page. The user should enter the number of pages and print out copies he/she wants.
Ans)
#include<stdio.h>
#include<conio.h>
void main()
{
int print=0,p_cost=0,type=0,t_cost=0;
clrscr();
printf("Enter how many pages you want to type ? ");
scanf("%d",&type);
printf("How many prints set you want =");
scanf("%d",&print);
t_cost=type*3;
p_cost=(type*print*3)-(type*3)+(type*5);
printf("\nYour Typing cost of %d pages = %d",type,t_cost);
printf("\nYour Printing cost of %d set = %d",print,p_cost);
printf("\nTotal Payable Amount is = %d",t_cost+p_cost);
getch();
}
Click here for download C file.
11. The ABC Insurance Company Ltd. Offers the following three categories of car insurance policy to car owners: • Category A, here the basic premium is calculated as 2% of the car’s value. • Category B, here the basic premium is calculated as 3% of the car’s value. • Category C, here the basic premium is calculated as 5% of the car’s value.
Ans)
//Premium value of insuarance
#include<stdio.h>
#include<conio.h>
void main()
{
char plan;
long int value;
clrscr();
printf("\nWelcome sir pls Enter your car's price = ");
scanf("%ld",&value);
printf("\nWe have 3 plans for you choose from a, b or c = ");
scanf("%s",&plan);
switch(plan)
{
case 'A':
case'a':
{
value=value*2/100;
break;
}
case 'B':
case'b':
{
value=value*3/100;
break;
}
case 'C':
case'c':
{
value=value*5/100;
break;
}
default:
{
printf("You choose wrong category");//this is optional
}
}
printf("\nYour Premium Amount is = %ld",value);
getch();
}
Click here for download C file.
12. Write a program to implement calculator using switch case.
Ans)
//calculator
#include<stdio.h>
#include<conio.h>
void main()
{
char opration;
int n1,n2;
clrscr();
printf("\nEnter any two number = ");
scanf("%d %d",&n1,&n2);
printf("\nEnter which opration u want to perform(+, -, *, /, %) = ");
scanf("%s",&opration);
switch(opration)
{
case'+':
{
printf("Additon = %d",n1+n2);
break;
}
case'-':
{
printf("Substraction = %d",n1-n2);
break;
}
case'*':
{
printf("Multiplication = %d",n1*n2);
break;
}
case'/':
{
printf("Division = %d",n1/n2);
break;
}
case'%':
{
printf("Modulas = %d",n1%n2);
break;
}
}
getch();
}
Click here for download C file.
1 Write a program to find sum of N numbers.
Ans :
#include<stdio.h>
#include<conio.h>
void main()
{
int n,no,sum=0,i;
clrscr();
printf("Enter how many number you want to sum = ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("Enter No %d = ",i);
scanf("%d",&no);
sum=sum+no;
}
printf("Sum = %d",sum);
gotoxy(60,20); //this is optional part
printf("By Aaftab");//this is optional part
getch();
}
Click here to downlaod .c file
2 Write a program to find factorial of given number.
Ans :
#include<stdio.h>
#include<conio.h>
void main()
{
int i,fact=1,num;
clrscr();
printf("Enter a number = ");
scanf("%d",&num);
for(i=num;i>0;i--)
{
fact=fact*i;
}
printf("\n Factorial = %d",fact);
gotoxy(60,20); //this is optional part
printf("By Aaftab");//this is optional part
getch();
}
Click here to downlaod .c file
3 Write a program to find maximum from given N inputs by user.
Ans :
#include<stdio.h>
#include<conio.h>
void main()
{
int i,max=0,n,num;
clrscr();
printf("Enter how many number you want to compare = ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("Enter num %d = ",i);
scanf("%d",&num);
if(i==1)
{max=num;}
if(num>max)
{
max=num;
}
}
printf("\n Max = %d",max);
gotoxy(60,20); //this is optional part
printf("By Aaftab");//this is optional part
getch();
}
Click here to downlaod .c file
4 Write a program to find reverse of a given number.
Ans :
#include<stdio.h>
#include<conio.h>
void main()
{
int no=0,rem,rev=0;
clrscr();
printf("Enter a num = ");
scanf("%d",&no);
while(no>0)
{
rem = no%10;
rev=(rev*10)+rem;
no=no/10;
}
printf("\n Reverse num is = %d",rev);
gotoxy(60,20); //this is optional part
printf("By Aaftab");//this is optional part
getch();
}
Click here to downlaod .c file
5 Write a program to find sum of the digits entered by the user.
Ans :
#include<stdio.h>
#include<conio.h>
void main()
{
int no=0,rem=0,sum=0;
clrscr();
printf("Enter a num = ");
scanf("%d",&no);
while(no>0)
{
rem=no%10;
sum=sum+rem;
no=no/10;
}
printf("\n Total of digits is = %d",sum);
gotoxy(60,20); //this is optional part
printf("By Aaftab");//this is optional part
getch();
}
Click here to downlaod .c file
6 Write a program to generate Fibonacci series up to N numbers.
Ans :
#include<stdio.h>
Click here to downlaod .c file
7 Write a program to find GCD and LCM of given 2 numbers.
Ans :
Coming soon
8 Write a program to find the sum of first 100 odd nos. and even nos.
Ans :
#include<stdio.h>
#include<conio.h>
void main()
{
int even=0,odd=0,i,no;
clrscr();
printf("\nEnter range = ");
scanf("%d",&no);
for(i=0;i<=100;i++)
{
if(i%2 == 0)
{
even=even+i;
}
else
{
odd=odd+i;
}
}
printf("\n\nSum of even num = %d",even);
printf("\n\nSum of odd num = %d",odd);
gotoxy(60,20); //this is optional part
printf("By Aaftab");//this is optional part
getch();
}
Click here to downlaod .c file
9 Write a program to check whether given number by the user is Palindrome or not.Ans :
ANS)
#include<stdio.h>
#include<conio.h>
void main()
{
int no=0,temp,rem=0,rev=0;
clrscr();
printf("Enter a num = ");
scanf("%d",&no);
temp=no;
while(no>0)
{
rem=no%10;
rev=(rev*10)+rem;
no=no/10;
}
if(temp==rev)
printf("\n %d is Pelidrome",temp);
else
printf("\n %d is not Pelidrome",temp);
gotoxy(60,20); //this is optional part
printf("By Aaftab");//this is optional part
getch();
}
Click here to downlaod .c file
10 Write a program to check whether the given number is Prime or not.
Ans :
#include<stdio.h>
#include<conio.h>
void main()
{
int i,no,flag=0;
clrscr();
printf("Enter number = ");
scanf("%d",&no);
for(i=no/2 ; i>1 ; i--)
{
if(no%i == 0)
{
flag=1;
}
}
if(flag==1)
{
printf("%d is not prime number",no);
}
else
printf("%d is prime numbers",no);
getch();
}
11 Write a program to print all the prime numbers ranging from 50 to 100.
Ans :
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,flag=0;
clrscr();
printf("\nPrime Numbers between 50 to 100 is = \n");
for(i=50;i<=100;i++)
{
for(j=i/2;j>1;j--)
{
if(i%j == 0)
{
flag=1;
break;
}
}
if(flag!=1)
{
printf("%d\t",i);
}
flag=0;
}
gotoxy(60,20); //this is optional part
printf("By Aaftab");//this is optional part
getch();
}
12 Write a C program to find x1+x2+x3+x4+ ….+xn.
Ans :
#include<stdio.h>
#include<conio.h>
void main()
{
int no,i,sum=0;
clrscr();
printf("Enter num = ");
scanf("%d",&no);
for(i=1;i<=no;i++)
{
if(i==no)
printf("x%d",i);
else
printf("x%d + ",i);
sum=sum+i;
}
printf(" = x%d",sum);
gotoxy(60,20); //this is optional part
printf("By Aaftab");//this is optional part
getch();
}
13 Write a C program to find 1+ 1/2 + 1/3 + 1/4 + …+1/n.
Ans :
14 Write a program to print following pyramid.
*
* *
* * *
* * * *
Ans :
#include<stdio.h>
#include<conio.h>
void main()
{
int num,row,col,space;
clrscr();
printf("Enter a num = ");
scanf("%d",&num);
for(row=1;row<num;row++)
{
for(space=num-1;space>row;space--)
{ printf(" "); }
for(col=1;col<=row;col++)
{
printf(" *");
}
printf("\n");
}
gotoxy(60,20); //this is optional part
printf("By Aaftab");//this is optional part
getch();
}
Click here to downlaod .c file
15 Write a program that accepts an integer N, if the integer N = 4,then print the pyramid :
1
121
12321
1234321
Ans :
#include<stdio.h>
#include<conio.h>
void main()
{
int row,col,k,num,a,b,c;
clrscr();
printf("Enter a num = ");
scanf("%d",&num);
for(row=1;row<num;row++)
{
for(col=1;col<=row;col++)
{
printf("%d",col);
}
for(k=row-1;k!=0;k--)
{
printf("%d",k);
}
printf("\n");
}
gotoxy(60,20); //this is optional part
printf("By Aaftab");//this is optional part
getch();
}
Click here to downlaod .c file
16 Write a program that accepts an integer N, if the integer N =4,then print the pyramid :
4 4 4 4
3 3 3
2 2
1
Ans :
#include<stdio.h>
#include<conio.h>
void main()
{
int row,col,no;
clrscr();
printf("Enter a num =");
scanf("%d",&no);
for(row=no;row>0;row--)
{
for(col=1;col<=row;col++)
{
printf("%d",row);
}
printf("\n");
}
gotoxy(60,20); //this is optional part
printf("By Aaftab");//this is optional part
getch();
}
Click Here to Download c file
17 Write a program to Print following:
A
B C
D E F
G H I J
Ans :
#include<stdio.h>
#include<conio.h>
void main()
{
char ch='A';
int row,col,no;
clrscr();
printf("Enter a num =");
scanf("%d",&no);
for(row=1;row<=no;row++)
{
for(col=1;col<=row;col++)
{
printf("%c",ch);
ch++;
}
printf("\n");
}
gotoxy(60,20); //this is optional part
printf("By Aaftab");//this is optional part
getch();
}
Click Here to Download c file
18 Write a program to Print following:
1
0 1
1 0 1
0 1 0 1
1 0 1 0 1
Ans :
#include<stdio.h>
#include<conio.h>
void main()
{
int i, j=1, rows,rem;
clrscr();
printf("Enter the number of rows = ");
scanf("%d", &rows);
for (i = 1; i <= rows; i++)
{
j=1;
for(; j <= i; j++)
{
rem=j%2;
printf("%d", rem);
}
printf("\n");
}
gotoxy(60,20); //this is optional part
printf("By Aaftab");//this is optional part
getch();
}
Click here to downlaod .c file
19 Write a program to Print following:
1
0 1
0 1 0
1 0 1 0
Ans :
20 Write a program to Print following:
A
ABA
ABCBA
ABCDCBA
Ans :
#include<stdio.h>
#include<conio.h>
int main()
{
int i,j,k,m,rows,ch='A';
clrscr();
printf("Enter the number of rows =");
scanf("%d",&rows);
for(i=1; i<=rows; i++)
{
for(k=1; k<=i; k++)
{
printf("%c",ch++);
}
ch--;
for(m=1; m<i; m++)
{
printf("%c",--ch);
}
printf("\n");
}
gotoxy(60,20); //this is optional part
printf("By Aaftab");//this is optional part
getch();
}
Click Here to Download c file
#include<stdio.h>
#include<conio.h>
void main()
{
int i;
float d,avg;
clrscr();
for(i=1;i<6;i++)
{
printf("Enter Temp of day %d= ",i);
scanf("%f",d);
avg=avg+d;
}
printf("Average Temp of five sunny days is = %f",avg/5);
getch();
}
Click here for download C file.
Unit 2 programs with solution and .c extention file..
1.Write a program to accept number of seconds and display its corresponding hours, minutes and seconds .
Ans)
#include<stdio.h>
#include<conio.h>
void main()
{
int sec,min,hour,temp;
clrscr();
printf("Enter secounds = ");
scanf("%d",&temp);
hour=(temp/3600);
min=(temp-(3600*hour))/60;
sec=(temp-(3600*hour)-(60*min));
printf("Hours= %d \nMinutes = %d \nSecounds = %d",hour,min,sec);
getch();
}
Click here for download C file.
2. Write a C program to find the maximum from given three numbers (Using Nested IF).
Ans)
#include<stdio.h>
#include<conio.h>
void main()
{
int n1,n2,n3;
clrscr();
printf("Enter 3 numbers = ");
scanf("%d %d %d",&n1,&n2,&n3);
if(n1>n2)
{
if(n1>n3)
{
printf("%d is largest",n1);
}
else
{
printf("%d is largest",n3);
}
}
else
{
if(n2>n3)
{
printf("%d is largest",n2);
}
else
{
printf("%d is largest",n3);
}
}
getch();
}
Click here for download C file.
3. Write a C program to find that the accepted no is Negative, Positive or Zero.
Ans)
// no is nagative zero or positive
#include<stdio.h>
#include<conio.h>
void main()
{
int n1;
clrscr();
printf("Enter number = ");
scanf("%d",&n1);
if(n1==0)
{
printf("Entered Num is Zero");
else if(n1>0)
{
printf("%d is Positive",n1);
}
else if
{
printf("%d is Nagtive",n1);
}
}
getch();
}
4. Write a program to check given year is a Leap year or not.
Ans)
//Leap year or not
#include<stdio.h>
#include<conio.h>
void main()
{
int n1;
clrscr();
printf("Enter year = ");
scanf("%d",&n1);
if( n1%2 == 0)
{
printf("%d is leap Year",n1);
}
else
{
printf("%d is not leap year",n1);
}
getch();
}
5. Write a C program to find minimum from given 3 numbers (Using Conditional Operator).
Ans)
//largest out of 3 num using else-if ledder
#include<stdio.h>
#include<conio.h>
void main()
{
int n1,n2,n3;
clrscr();
printf("Enter 3 numbers = ");
scanf("%d %d %d",&n1,&n2,&n3);
if(n1>n2 && n1>n3)
{
printf("%d is largest",n1);
}
else if(n2>n1 && n2>n3)
{
printf("%d is largest",n2);
}
else
{
printf("%d is largest",n3);
}
getch();
}
6. Write a C program to find the maximum from given three numbers (Without using Nested if, or Logical
Operator, Or Conditional operators).
Ans)
//usinf ternary operators
#include<stdio.h>
#include<conio.h>
void main()
{
int n1,n2,n3,ans;
clrscr();
printf("Enter 3 numbers = ");
scanf("%d %d %d",&n1,&n2,&n3);
ans=n1>n2?(n1>n3?n1:n3):(n2>n3?n2:n3);
printf("Largest num is = %d ",ans);
getch();
}
Click here for download C file.
7. Take marks from the user and print grade accordingly( >=75 marks – Distinction, <75 and >=60 marks – First, <60 and >=50 – Second, <50 and >=35 – Pass, <35 – Fail) using if … else if….else statement and also by using logical operators).
Ans)
//take marks as inoput and print grade
#include<stdio.h>
#include<conio.h>
void main()
{
int sci,eng,maths,total;
float per;
clrscr();
printf("\nEnter the sci,maths and english marks : ");
scanf("%d %d %d",&sci,&maths,&eng);
total=eng+maths+sci;
printf("\nTotal marks = %d",total);
per=total/3;
printf("\nPercentage = %.2f\n",per);
if(per>=75)
{
printf("Distinction");
}
else if(per<75 && per>=60)
{
printf("First");
}
else if(per<60 && per>=50)
{
printf("Second");
}
else if(per<50 && per>=35)
{
printf("Pass");
}
else
{
printf("Fail");
}
getch();
}
Click here for download C file.
8. Take 2 numbers from the user and print the greater number (Number can be equal).
Ans)
//check num is equal or greater
#include<stdio.h>
#include<conio.h>
void main()
{
int no1,no2;
clrscr();
printf("\nEnter two num = ");
scanf("%d %d",&no1,&no2);
if(no1==no2)
{
printf("\nNumbers are Equal.");
}
else if(no1>no2)
{
printf("\n%d is greater than %d",no1,no2);
}
else
{
printf("%d is greater than %d",no2,no1);
}
getch();
}
Click here for download C file.
9. Write a program to check whether the blood donor is eligible or not for donating blood. The conditions laid down are as under. Use if statement. a) Age should be above 18 yrs but not more than 55 yrs.
Ans)
//Eligible for donate blood or not
#include<stdio.h>
#include<conio.h>
void main()
{
int age;
clrscr();
printf("\nEnter Your Age = ");
scanf("%d",&age);
if(age>18 && age<=55)
{
printf("\nYou are Eligible");
}
else
{
printf("You are not Eligible");
}
getch();
}
Click here for download C file.
10. Write a program to calculate bill of a job work done as follows. Use if else statement. a) Rate of typing 3 Rs/page b) Printing of 1st copy 5Rs/pages & later every copy 3Rs/page. The user should enter the number of pages and print out copies he/she wants.
Ans)
#include<stdio.h>
#include<conio.h>
void main()
{
int print=0,p_cost=0,type=0,t_cost=0;
clrscr();
printf("Enter how many pages you want to type ? ");
scanf("%d",&type);
printf("How many prints set you want =");
scanf("%d",&print);
t_cost=type*3;
p_cost=(type*print*3)-(type*3)+(type*5);
printf("\nYour Typing cost of %d pages = %d",type,t_cost);
printf("\nYour Printing cost of %d set = %d",print,p_cost);
printf("\nTotal Payable Amount is = %d",t_cost+p_cost);
getch();
}
Click here for download C file.
11. The ABC Insurance Company Ltd. Offers the following three categories of car insurance policy to car owners: • Category A, here the basic premium is calculated as 2% of the car’s value. • Category B, here the basic premium is calculated as 3% of the car’s value. • Category C, here the basic premium is calculated as 5% of the car’s value.
Ans)
//Premium value of insuarance
#include<stdio.h>
#include<conio.h>
void main()
{
char plan;
long int value;
clrscr();
printf("\nWelcome sir pls Enter your car's price = ");
scanf("%ld",&value);
printf("\nWe have 3 plans for you choose from a, b or c = ");
scanf("%s",&plan);
switch(plan)
{
case 'A':
case'a':
{
value=value*2/100;
break;
}
case 'B':
case'b':
{
value=value*3/100;
break;
}
case 'C':
case'c':
{
value=value*5/100;
break;
}
default:
{
printf("You choose wrong category");//this is optional
}
}
printf("\nYour Premium Amount is = %ld",value);
getch();
}
Click here for download C file.
12. Write a program to implement calculator using switch case.
Ans)
//calculator
#include<stdio.h>
#include<conio.h>
void main()
{
char opration;
int n1,n2;
clrscr();
printf("\nEnter any two number = ");
scanf("%d %d",&n1,&n2);
printf("\nEnter which opration u want to perform(+, -, *, /, %) = ");
scanf("%s",&opration);
switch(opration)
{
case'+':
{
printf("Additon = %d",n1+n2);
break;
}
case'-':
{
printf("Substraction = %d",n1-n2);
break;
}
case'*':
{
printf("Multiplication = %d",n1*n2);
break;
}
case'/':
{
printf("Division = %d",n1/n2);
break;
}
case'%':
{
printf("Modulas = %d",n1%n2);
break;
}
}
getch();
}
Click here for download C file.
Unit 3 programs with solution and .c extention file..
1 Write a program to find sum of N numbers.
Ans :
#include<stdio.h>
#include<conio.h>
void main()
{
int n,no,sum=0,i;
clrscr();
printf("Enter how many number you want to sum = ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("Enter No %d = ",i);
scanf("%d",&no);
sum=sum+no;
}
printf("Sum = %d",sum);
gotoxy(60,20); //this is optional part
printf("By Aaftab");//this is optional part
getch();
}
Click here to downlaod .c file
2 Write a program to find factorial of given number.
Ans :
#include<stdio.h>
#include<conio.h>
void main()
{
int i,fact=1,num;
clrscr();
printf("Enter a number = ");
scanf("%d",&num);
for(i=num;i>0;i--)
{
fact=fact*i;
}
printf("\n Factorial = %d",fact);
gotoxy(60,20); //this is optional part
printf("By Aaftab");//this is optional part
getch();
}
Click here to downlaod .c file
3 Write a program to find maximum from given N inputs by user.
Ans :
#include<stdio.h>
#include<conio.h>
void main()
{
int i,max=0,n,num;
clrscr();
printf("Enter how many number you want to compare = ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("Enter num %d = ",i);
scanf("%d",&num);
if(i==1)
{max=num;}
if(num>max)
{
max=num;
}
}
printf("\n Max = %d",max);
gotoxy(60,20); //this is optional part
printf("By Aaftab");//this is optional part
getch();
}
Click here to downlaod .c file
4 Write a program to find reverse of a given number.
Ans :
#include<stdio.h>
#include<conio.h>
void main()
{
int no=0,rem,rev=0;
clrscr();
printf("Enter a num = ");
scanf("%d",&no);
while(no>0)
{
rem = no%10;
rev=(rev*10)+rem;
no=no/10;
}
printf("\n Reverse num is = %d",rev);
gotoxy(60,20); //this is optional part
printf("By Aaftab");//this is optional part
getch();
}
Click here to downlaod .c file
5 Write a program to find sum of the digits entered by the user.
Ans :
#include<stdio.h>
#include<conio.h>
void main()
{
int no=0,rem=0,sum=0;
clrscr();
printf("Enter a num = ");
scanf("%d",&no);
while(no>0)
{
rem=no%10;
sum=sum+rem;
no=no/10;
}
printf("\n Total of digits is = %d",sum);
gotoxy(60,20); //this is optional part
printf("By Aaftab");//this is optional part
getch();
}
Click here to downlaod .c file
6 Write a program to generate Fibonacci series up to N numbers.
Ans :
#include<stdio.h>
#include<conio.h>
void main()
{
int n,a=0,b=1,c=0,i;
clrscr();
printf("\nEnter number = ");
scanf("%d",&n);
printf("\n\nFibonacci Series :- \n\n");
if(n<=2)
{
printf("%d\t%d",a,b);
}
else
{
printf("%d\t%d",a,b);
for(i=1;i<=n;i++)
{
c=a+b;
a=b;
b=c;
printf("\t%d",c);
}
}
gotoxy(60,20); //this is optional part
printf("By Aaftab");//this is optional part
getch();
}
Click here to downlaod .c file
7 Write a program to find GCD and LCM of given 2 numbers.
Ans :
Coming soon
8 Write a program to find the sum of first 100 odd nos. and even nos.
Ans :
#include<stdio.h>
#include<conio.h>
void main()
{
int even=0,odd=0,i,no;
clrscr();
printf("\nEnter range = ");
scanf("%d",&no);
for(i=0;i<=100;i++)
{
if(i%2 == 0)
{
even=even+i;
}
else
{
odd=odd+i;
}
}
printf("\n\nSum of even num = %d",even);
printf("\n\nSum of odd num = %d",odd);
gotoxy(60,20); //this is optional part
printf("By Aaftab");//this is optional part
getch();
}
9 Write a program to check whether given number by the user is Palindrome or not.Ans :
ANS)
#include<stdio.h>
#include<conio.h>
void main()
{
int no=0,temp,rem=0,rev=0;
clrscr();
printf("Enter a num = ");
scanf("%d",&no);
temp=no;
while(no>0)
{
rem=no%10;
rev=(rev*10)+rem;
no=no/10;
}
if(temp==rev)
printf("\n %d is Pelidrome",temp);
else
printf("\n %d is not Pelidrome",temp);
gotoxy(60,20); //this is optional part
printf("By Aaftab");//this is optional part
getch();
}
Click here to downlaod .c file
10 Write a program to check whether the given number is Prime or not.
Ans :
#include<stdio.h>
#include<conio.h>
void main()
{
int i,no,flag=0;
clrscr();
printf("Enter number = ");
scanf("%d",&no);
for(i=no/2 ; i>1 ; i--)
{
if(no%i == 0)
{
flag=1;
}
}
if(flag==1)
{
printf("%d is not prime number",no);
}
else
printf("%d is prime numbers",no);
getch();
}
11 Write a program to print all the prime numbers ranging from 50 to 100.
Ans :
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,flag=0;
clrscr();
printf("\nPrime Numbers between 50 to 100 is = \n");
for(i=50;i<=100;i++)
{
for(j=i/2;j>1;j--)
{
if(i%j == 0)
{
flag=1;
break;
}
}
if(flag!=1)
{
printf("%d\t",i);
}
flag=0;
}
gotoxy(60,20); //this is optional part
printf("By Aaftab");//this is optional part
getch();
}
12 Write a C program to find x1+x2+x3+x4+ ….+xn.
Ans :
#include<stdio.h>
#include<conio.h>
void main()
{
int no,i,sum=0;
clrscr();
printf("Enter num = ");
scanf("%d",&no);
for(i=1;i<=no;i++)
{
if(i==no)
printf("x%d",i);
else
printf("x%d + ",i);
sum=sum+i;
}
printf(" = x%d",sum);
gotoxy(60,20); //this is optional part
printf("By Aaftab");//this is optional part
getch();
}
13 Write a C program to find 1+ 1/2 + 1/3 + 1/4 + …+1/n.
Ans :
14 Write a program to print following pyramid.
*
* *
* * *
* * * *
Ans :
#include<stdio.h>
#include<conio.h>
void main()
{
int num,row,col,space;
clrscr();
printf("Enter a num = ");
scanf("%d",&num);
for(row=1;row<num;row++)
{
for(space=num-1;space>row;space--)
{ printf(" "); }
for(col=1;col<=row;col++)
{
printf(" *");
}
printf("\n");
}
gotoxy(60,20); //this is optional part
printf("By Aaftab");//this is optional part
getch();
}
Click here to downlaod .c file
15 Write a program that accepts an integer N, if the integer N = 4,then print the pyramid :
1
121
12321
1234321
Ans :
#include<stdio.h>
#include<conio.h>
void main()
{
int row,col,k,num,a,b,c;
clrscr();
printf("Enter a num = ");
scanf("%d",&num);
for(row=1;row<num;row++)
{
for(col=1;col<=row;col++)
{
printf("%d",col);
}
for(k=row-1;k!=0;k--)
{
printf("%d",k);
}
printf("\n");
}
gotoxy(60,20); //this is optional part
printf("By Aaftab");//this is optional part
getch();
}
Click here to downlaod .c file
16 Write a program that accepts an integer N, if the integer N =4,then print the pyramid :
4 4 4 4
3 3 3
2 2
1
Ans :
#include<stdio.h>
#include<conio.h>
void main()
{
int row,col,no;
clrscr();
printf("Enter a num =");
scanf("%d",&no);
for(row=no;row>0;row--)
{
for(col=1;col<=row;col++)
{
printf("%d",row);
}
printf("\n");
}
gotoxy(60,20); //this is optional part
printf("By Aaftab");//this is optional part
getch();
}
Click Here to Download c file
17 Write a program to Print following:
A
B C
D E F
G H I J
Ans :
#include<stdio.h>
#include<conio.h>
void main()
{
char ch='A';
int row,col,no;
clrscr();
printf("Enter a num =");
scanf("%d",&no);
for(row=1;row<=no;row++)
{
for(col=1;col<=row;col++)
{
printf("%c",ch);
ch++;
}
printf("\n");
}
gotoxy(60,20); //this is optional part
printf("By Aaftab");//this is optional part
getch();
}
Click Here to Download c file
18 Write a program to Print following:
1
0 1
1 0 1
0 1 0 1
1 0 1 0 1
Ans :
#include<stdio.h>
#include<conio.h>
void main()
{
int i, j=1, rows,rem;
clrscr();
printf("Enter the number of rows = ");
scanf("%d", &rows);
for (i = 1; i <= rows; i++)
{
j=1;
for(; j <= i; j++)
{
rem=j%2;
printf("%d", rem);
}
printf("\n");
}
gotoxy(60,20); //this is optional part
printf("By Aaftab");//this is optional part
getch();
}
19 Write a program to Print following:
1
0 1
0 1 0
1 0 1 0
Ans :
20 Write a program to Print following:
A
ABA
ABCBA
ABCDCBA
Ans :
#include<stdio.h>
#include<conio.h>
int main()
{
int i,j,k,m,rows,ch='A';
clrscr();
printf("Enter the number of rows =");
scanf("%d",&rows);
for(i=1; i<=rows; i++)
{
for(k=1; k<=i; k++)
{
printf("%c",ch++);
}
ch--;
for(m=1; m<i; m++)
{
printf("%c",--ch);
}
printf("\n");
}
gotoxy(60,20); //this is optional part
printf("By Aaftab");//this is optional part
getch();
}
Click Here to Download c file
Comments
Post a Comment