C programming
// WK1_2 : pgm to illustrate the use of different data types and verify their memory size
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c,d,e,f;
clrscr();
printf("the primitive data types used in C are as shown below\n");
printf("-------------------------------------------------------");
printf("\nData type \t\t\t Size (in Bytes)\n");
printf(" ------------------------------------------------------");
a=sizeof(int);
b=sizeof(float);
c=sizeof(char);
d=sizeof(double);
e=sizeof(short int);
f=sizeof(long int);
printf("\nint \t\t\t\t %d",a);
printf("\nfloat \t\t\t\t %d",b);
printf("\nchar \t\t\t\t %d",c);
printf("\ndouble \t\t\t\t %d",d);
printf("\nshort int \t\t\t %d",e);
printf("\nlong int \t\t\t %d",f);
getch();
}
Output:
//WK2_1A: PGM TO compute SI , given P,T,R
#include<stdio.h>
#include<conio.h>
void main()
{
float P,T,R,SI;
clrscr();
printf("Enter the principal amount\nTime duration and\nRate of Interest");
scanf("%f%f%f",&P,&T,&R);
SI=(P*T*R)/100;
printf("the simple interest for the given values= Rupees %f",SI);
getch();
}
//WK2_1B: COMPUTE COMPOUND INTEREST GIVEN P,T,R,N
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float A,P,r,n,t,x,y,final;
clrscr();
printf("\nEnter the initial principal balance amount: P");
scanf("%f",&P);
printf("\nEnter the interest rate: r");
scanf("%f",&r);
printf("\nEnter No.of times interest applied/time period: n");
scanf("%f",&n);
printf("\nEnter the elapsed no. of time periods: t");
scanf("%f",&t);
printf("\n\ncompound interest payable is\n");
A=P*(1+(r/n));
x=A;
y=n*t;
final=pow(x,y);
printf("Rupees %f",final);
getch();
}
// WK2_2A: compute area of cir,sq,rect,tri
#include<stdio.h>
#include<conio.h>
#define PI 3.14
void main()
{
float Area_cir,Area_sq,Area_rect,Area_tri,r,wid,len,side,b,h;
int choice;
clrscr();
printf("\n Press 1 to find area of circle");
printf("\n Press 2 to find area of square");
printf("\n Press 1 to find area of rectangle");
printf("\n Press 1 to find area of triangle");
printf("\n please enter your choice between 1 to 4");
scanf("%d",&choice);
switch(choice)
{
case 1: printf("enter the radius of a circle in mtrs: ");
scanf("%f",&r);
Area_cir=PI*r*r;
printf("\nArea of circle is= %0.2f mtrs",Area_cir);
break;
case 2: printf("enter the value of a side/edge in mtrs: ");
scanf("%f",&side);
Area_sq=side*side;
printf("\nArea of a square is= %0.2f mtrs",Area_sq);
break;
case 3: printf("enter the width and length of a rectangle in mtrs: ");
scanf("%f%f",&wid,&len);
Area_rect=wid*len;
printf("\nArea of a rectangle is= %0.2f mtrs",Area_rect);
break;
case 4: printf("enter the base and height of triangle in mtrs: r");
scanf("%f%f",&b,&h);
Area_tri=0.5*b*h;
printf("\nArea of triangle is= %0.2f mtrs",Area_tri);
break;
default: printf("\nplease enter correct choice between 1 to 4");
}
getch();
}
//wk2_2b: swap contents of 2 variables without using intermediate variable
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
clrscr();
printf("\nenter the values for a and b \n");
scanf("%d%d",&a,&b);
printf("\nthe values of a and b before swapping are %d and %d",a,b);
a=a+b;
b=a-b;
a=a-b;
printf("\nthe values of a and b after swapping are %d and %d",a,b);
getch();
}
/*wk3_1a: compute largest of 3 numbers using if-else and ternary operator
---------------------------------------------------------------------
using if else
-----------------------------------
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr();
printf("\nEnter the values for a,b and c");
scanf("%d%d%d",&a,&b,&c);
if(a>b && a>c)
printf("%d is big",a);
else if(b>a && b>c)
printf("%d is big",b);
else
printf("%d is big");
getch();
}
using ternary operator(?:)
---------------------------------
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c,large;
clrscr();
printf("\n enter the values of a,b and c");
scanf("%d%d%d",&a,&b,&c);
large=(a>b)?a:b;
large=(large>c)?large:c;
printf("largest amongst 3 number=%d",large);
getch();
}
//wk3_1b:compute result of a student using nested if
#include<stdio.h>
#include<conio.h>
void main()
{
char name;
int m1,m2,m3;
float total,per;
clrscr();
printf("Enter the name of student\n");
scanf("%s",&name);
printf("Enter the marks of three subjects m1,m2 and m3\n");
scanf("%d%d%d",&m1,&m2,&m3);
total=m1+m2+m3;
per=total/3;
printf("\nTotal marks obtained is=%f",total);
printf("\nPercentage obtained is=%0.2f",per);
printf("\nResult declared is\n\n");
if(m1<35 || m2<35 || m3<35)
printf("FAILED");
else
if(per>=75)
printf("DISTINCTION");
else
if(per>=60 && per<75)
printf("FIRST CLASS");
else
if(per>=50 && per<60)
printf("SECOND CLASS");
getch();
}
//WK3_2: PGM TO FIND RESISTANCE OF A RESISTOR,GIVEN COLORCODE AND TOLERANCE
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int fb,sb,tb;
char tol;
float res,x;
clrscr();
window(100,10,40,11);
textcolor(WHITE);
textbackground(BLACK);
cprintf("0: BLACK \r\n");
window(100,20,40,11);
textcolor(WHITE);
textbackground(BROWN);
cprintf("1: BROWN \r\n");
window(100,30,40,11);
textcolor(WHITE);
textbackground(RED);
cprintf("2: RED \r\n");
window(100,40,40,11);
textcolor(WHITE);
textbackground(YELLOW);
cprintf("3: ORANGE \r\n");
window(100,50,40,11);
textcolor(WHITE);
textbackground(YELLOW);
cprintf("4: YELLOW \r\n");
window(100,60,40,11);
textcolor(WHITE);
textbackground(GREEN);
cprintf("5: GREEN \r\n");
window(100,70,40,11);
textcolor(WHITE);
textbackground(BLUE);
cprintf("6: BLUE \r\n");
window(100,80,40,11);
textcolor(WHITE);
textbackground(MAGENTA);
cprintf("7: VIOLET \r\n");
window(100,80,40,11);
textcolor(WHITE);
textbackground(LIGHTGRAY);
cprintf("8: GRAY \r\n");
window(100,90,40,11);
textcolor(BLACK);
textbackground(WHITE);
cprintf("8: WHITE \r\n");
printf("enter the equivalent color number for FIRST BAND\n");
scanf("%d",&fb);
printf("enter the equivalent color number for SECOND BAND BAND\n");
scanf("%d",&sb);
printf("enter the equivalent color number for THIRD BAND\n");
scanf("%d",&tb);
res=fb*10+sb;
x=pow(10,tb);
printf("value of a resistor is:");
printf("%0.0f*%0.0fohms",res,x);
printf("\nEnter the tolerance color\n press G for Gold\n press S for Silver");
scanf("%s",&tol);
switch(tol)
{
case 'G':
case 'g':
printf("Tolerance is +5%");
break;
case 'S':
case 's':
printf("Tolerance is +10%");
break;
default:
printf("for colorless +20% Tolerance");
}
getch();
}
/*wk4_1a: factorial of a given number*/
#include<stdio.h>
#include<conio.h>
int factorial(int n);
void main()
{
int n,fact=1,i;
clrscr();
printf("Enter the value of n to find its Factorial!\n");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
fact=fact*i;
}
printf("The factorial of %d is=%d",n,fact);
getch();
}
//wk4_1b:compute sum of digits of a given 3 dig num reducing it to a single dig.
#include<stdio.h>
#include<conio.h>
void main()
{
int num,sum=0,rem,xs=0,x;
clrscr();
printf("enter the number\n");
scanf("%d",&num);
while(num>0)
{
rem=num%10;
sum=sum+rem;
num=num/10;
}
if(sum!=1||sum!=2||sum!=3||sum!=4||sum!=5||sum!=6||sum!=7||sum!=8||sum!=9)
{
x=sum;
while(x>0)
{
rem=x%10;
xs=xs+rem;
x=x/10;
}
}
printf("sum of digits of a given number=%d",sum);
printf("\nagain sum of digits of a given number=%d",xs);
getch();
}
//wk4_2: sort an array of numbers in ascending and descending order
#include<stdio.h>
#include<conio.h>
void main()
{
int num[100],i,j,n,temp;
clrscr();
printf("ENTER THE NUMBER OF ELEMENTS TO SORT \n");
scanf("%d",&n);
printf("\n enter %d numbers to sort",n);
for(i=0;i<n;i++)
scanf("%d",&num[i]);
printf("The elements after sorting in ascending order are\n");
printf("\n-------------------------------------------------------------\n");
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(num[i]>num[j])
{
temp=num[i];
num[i]=num[j];
num[j]=temp;
}
}
}
for(i=0;i<n;i++)
printf("%d\n",num[i]);
printf("\n-------------------------------------------------------------\n");
printf("The elements after sorting in ascending order are\n");
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(num[i]<num[j])
{
temp=num[i];
num[i]=num[j];
num[j]=temp;
}
}
}
for(i=0;i<n;i++)
printf("%d\n",num[i]);
getch();
}
//wk5_1a: comput length and reverse the string using string functions
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char name[100]="RJS";
clrscr();
printf("\nSize of string =%d characters",strlen(name));
printf("\nReverse of a given string is =%s ",strrev(name));
getch();
}
//sum of 2 matrices
#include<stdio.h>
#include<conio.h>
void main()
{
int A[10][10],B[10][10],SUM[10][10],i,j,r,c;
clrscr();
printf("\n Enter the size of matrix");
scanf("%d%d",&r,&c);
printf("\nEnter the elements for Matrix A\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&A[i][j]);
}
}
printf("\nEnter the elements for Matrix B\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&B[i][j]);
}
}
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
SUM[i][j]=A[i][j]+B[i][j];
}
}
printf("\nSum of 2 Matrices are\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("%d\t",SUM[i][j]);
}
printf("\n");
}
getch();
}
//wk5_2a: cube of num using func
#include<stdio.h>
#include<conio.h>
void cube(int num);
void main()
{
int num;
clrscr();
printf("\n Enter the number\n");
scanf("%d",&num);
cube(num);
getch();
}
void cube(int num)
{
int res=num*num*num;
printf("cube of a given number=%d",res);
}
//wk5_2b:store the details of employee using structure
#include<stdio.h>
#include<conio.h>
struct employee
{
char Name[50],ID[20],Address[200];
int age;
long salary;
}emp;
void main()
{
clrscr();
printf("Enter the details of Employee\n");
printf("\n Enter the name :");
scanf("%s",&emp.Name);
printf("\n Enter the employee ID :");
scanf("%s",&emp.ID);
printf("\n Enter the Residence/permanent Address :");
scanf("%s",&emp.Address);
printf("\n Enter the age :");
scanf("%d",&emp.age);
printf("\n Enter Gross Salary :");
scanf("%ld",&emp.salary);
printf("--------------------------------------------------\n");
printf("\nHere is the complete details of Employee\n");
printf("==================================================\n");
printf("Name : %s",emp.Name);
printf("\nID : %s",emp.ID);
printf("\nAddress : %s",emp.Address);
printf("\nAge : %d",emp.age);
printf("\nSalary : %ld",emp.salary);
getch();
}
Comments
Post a Comment