-----------------------------------------------------------------------------------------------------
Author: Zeeshan Ahmed, Mirpurkhas
ELECTRIFY 1999-2006
------------------------------------------------------------------------------------------------------
Introduction Of C Language
C is high level programming language with some low level features.
C developed by Dennis Ritchie in 1972 at AT&T bell Laboratories (usa).
C is case sensitive language.
Rules Of C Language
Start the program with void main () function.
Write the program in lower case.
You must type ( ) end-of statement.
Save the program .c OR .cpp extension.Write all statement in carly breaks {}.
------------------------------------------------------------------------------------------------------
#include<stdio.h>
#include<conio.h>
void main()
{
printf(“Electrify”);
}
void main()
{
clrscr ();
printf(“Electrify”);
printf(“\nS/O”);
printf(“\nBashir Ahmed”);
getch();
}
void main()
{
clrscr();
printf(“Electrify”);
getch(); clrscr();
printf(“\n PAKISTAN”);
getch(); clrscr();
printf(“\n MFTK”);
getch(); clrscr();
printf(“\n ISLAMABD”);
getch();
}
void main()
{
clrscr();
printf(“\n\t******************”);
printf(“\n\t Electrify”);
printf(“\n\t******************”);
printf(“\n1.\a Electrify”);
printf(“\n2.\a Zeeshan ahmed”);
printf(“\n3.\a Arsalan ahmed”);
printf(“\n4.\a Wijdan ahmed”);
printf(“\n5.\a Jibran ahmed”);
printf(“\n\t\t\t\a Electrify”);
printf(“\n\t\t\t\a Website”);
getch();
printf(“\n\r\t\at\ae\ah\as\ae\ae\an\aw\ad\a.\a4\at\a.\ac\ao\am”);
getch();
}
void main()
{
clrscr();
printf(“\’TEHSEEN\’”);
printf(“\n\”AHMED\””);
printf(“\n\\MFTK\\”);
getch();
}
void main()
{
clrscr();
printf(“\\xdb\xdb\xdb\xdb\xdb\xdb\xdb\xdb\xdb\xdb\xdb\xdb\xdb”);
printf(“\n\xdb Electrify\xdb”);
printf(“\n\xdb\xdb\xdb\xdb\xdb\xdb\xdb\xdb\xdb\xdb\xdb\xdb\xdb”);
getch();
}
DATA TYPE
Integer |
int |
%d |
Floating point |
float |
%f |
Character |
char |
%c |
String |
char |
%s |
void main()
{
clrscr();
no=2+2*2;
printf(“Answer=%d”,no);
getch();
}
For Text Color
void main()
{
clrscr();
textbackground(9);
textcolor(5);
gotoxy(35,12);
printf(“TEHSEEN”);
getch();
}
void main()
{
char name[22]={“TEHSEEN”},gender=’M’;
int age=19;
float height=5.9;
clrscr();
printf(“Name-----%s”,name);
printf(“\nAge-----%d”,age);
printf(“\nHeight------%f”,height);
printf(“\nGender------%c”,gender);
getch();
}
void main()
{
float num1,num2,num3,ans;
num1=9.0;
num2=25.0;
clrscr();
ans=num1,num2;
printf(“\n Sum Of %f+%f=%f”,num1,num2,ans);
ans=num1-num2;
printf(“\n %f-%f=%f”,num1,num2,ans);
ans=num1*num2;
printf(“\n %f*%f=%f”,num1,num2,ans);
ans=num1/num2;
printf(“\n %f/%f=%f”,num1,num2,ans);
ans=num1%num2;
printf(“\n %fmod%f=%f”,num1,num2,ans);
getch();
}
void main()
{
float usa=45,pak;
clrscr();
pak=usa*60;
printf(“%.2f Dollars=%.2f Pakistani Rupee”,usa,pak);
getch();
}
void main()
{
char name[25],g;
int age;
float height;
clrscr();
printf(“Your Name.....”);
scanf(“%s”,&name);
printf(“Your Age.....”);
scanf(“%d”,&age);
printf(“Your Gender.....”);
scanf(“%s”,&g);
printf(“Your Height.....”);
scanf(“%f”,&height);
printf(“\n Name==%s”,name);
printf(“\n Age==%d”,age);
printf(“\n Gender==%c”,g);
printf(“\n Height==%.2f”,height);
getch();
}
void main()
{
int no1,no2,ans;
clrscr();
printf(“Enter First Value....”);
scanf(“%d”,&no1);
printf(“Enter Second Value....”);
scanf(“%d”,&no2);
ans=no1+no2;
printf(“\n %d+%d=%d,”,no1,no2,ans);
getch();
}
void main()
{
char item[20];
int qty,discount;
float price,amount,dis_cash,net_amount;
clrscr();
printf(“Enter Product Name....”);
scanf(“%s”,&item);
printf(“Enter Quantity....”);
scanf(“%d”,&qty);
printf(“Enter Price....”);
scanf(“%f”,&price);
printf(“Enter Discount(%)....”);
scanf(“%d”,&discount);
clrscr();
printf(“product=%s”,item);
printf(“\nPrice=%.2f”,price);
printf(“\nQuantity=%d”,qty);
amount=price*qty;
printf(“\nAmount=%.2f”,amount);
printf(“\nDiscount=%d”,discount);
dis_cash=amount*discount/100;
printf(“\ndiscount in cash=%.2f”,dis_cash);
net_amount=amount-dis_cash;
printf(“\nNet Amount=%.2f”,net_amount);
getch();
}
Swaping/Inter Change The Values
void main()
{
int a=25,b=100,c;
clrscr();
printf(“A=%d”,a);
printf(“\nB=%d”,b);
c=a;
a=b;
b=c;
printf(“\nAfter Sweping”);
printf(“\nA=%d”,a);
printf(“\nB=%d”,b);
getch();
}
void main()
{
char ch;
clrscr();
printf(“Type Any Charater…”);
scanf(“%c”,&ch);
printf(“\n%c=%d”,ch,ch);
getch();
}
void main()
{
int a=10,b=5;
clrscr();
a=a+b;
b=a-b;
a=a-b;
printf(“A=%d”,a);
printf(“\nB=%d”,b);
getch();
}
void main()
char ch;
clrscr();
printf(“Type A Small Character....”);
scanf(“\n%c”,&ch);
printf(“\n%c=%c”,ch,ch-32);
grtch();
}
void main()
char ch;
clrscr();
printf(“Type A Capital Character....”);
scanf(“\n%c”,&ch);
printf(“\n%c=%c”,ch,ch+32);
grtch();
}
Ternary/Conditional Operators
1. |
True |
? |
2. |
False |
: |
void main()
{
int age;
clrscr();
printf(“Enter Age....”);
scanf(“%c”,age<=50?’y’:’o’);//scanf(“%s”,age<=50?"young":"Old");
getch();
}
void main()
{
char ch;
clrscr();
printf(“Enter a Char....”);
scanf(“%c”,&ch);
printf(“\n%c”,ch<90?ch+32:ch-32);
getch();
}
Check Even Odd Numbers
void main()
{
int no,mod;
clrscr();
printf(“Enter Number....”);
scanf(“%d”,&no);
mod=no%2;
printf(“%Number”,mod==0?”EVEN”:”ODD”);
getch();
}
void main()
{
int age;
clrscr();
printf(“Enter Your Age....”);
scanf(“%d”,&age);
if(age<=24)
printf(“You Are Fit This Job”);
else
printf(“You Are not Fit This Job”);
getch();
}
Logical Operators
1. |
&& |
and |
2. |
|| |
or |
3. |
! |
not |
void main()
{
int age;
clrscr();
printf(“Enter Your Age....”);
scanf(“%d”,&age);
if(age>=24&&age<=40)
printf(“You Are Fit This Job”);
else
printf(“You Are not Fit This Job”);
getch();
}
Calculator
void main()
{
int no1,no2,ans,choice;
clrscr();
printf(“ ****** CALCULATOR ******”);
printf(“\n 1.Multiply”);
printf(“\n 2.Divided”);
printf(“\n 3.Subtract”);
printf(“\n 4.Sum”);
printf(“\nEnter Your Choice Number....”);
scanf(“%d”,&choice);
printf(“Enter First Value....”);
scanf(“%d”,&no1);
printf(“Enter Second Value....”);
scanf(“%d”,&no2);
if(choice==1)
ans=no1*no2;
else
if(choice==2)
ans=no1/no2;
else
if(choice==3)
ans=no1-no2;
else
ans=no1+no2;
printf(“\nAnswer=%d”,ans);
getch();
}
Check Smallest Number
void main()
{
int a,b,c;
clrscr();
printf(“Type Three Numbers..”);
scanf(“%d%d%d”,&a,&b,&c);
if(a<b&&a<c)
printf(“\nSmallest Number Is !!=%d”,a);
else
if(b<a&&b<c)
printf(“Smallest Number Is !!=%d”,b);
else
printf(“Smallest Number Is !!=%d”,c);
getch();
}
Check Middle Number
void main()
{
int a,b,c;
clrscr();
printf(“Type Three Numbers..”);
scanf(“%d%d%d”,&a,&b,&c);
if(a>b&&a<c||a<b&&a>b)
printf(“\n Middle Number Is !!=%d”,a);
else
if(b>a&&b<c||b<a&&b>c)
printf(“Middle Number Is !!=%d”,b);
else
printf(“Middle Number Is !!=%d”,c);
getch();
}
void main()
{
int a,b,c;
clrscr();
printf(“Enter a First No...: “);
scanf(“%d”,&a);
printf(“Enter a Second No...: “);
scanf(“%d”,&b);
printf(“Enter a Third No...: “);
scanf(“%d”,&c);
printf(“\n%d%d%d”,a,b,c);
printf(“\n%d%d%d”,c,b,a);
if(a==c)
printf(“\nEqual”);
else
printf(“\nNot Equal”);
getch();
}
void main()
{
int no,left,right,sum;
clrscr();
printf(“Enter a No(Only 1 to 99)..: “);
scanf(“%d”,&no);
left=no/10;
right=no%10;
sum=left+right;
printf(“\nsum of %d=%d”,no,sum);
getch();
}
GOTO
void main()
{
clrscr();
printf(“TEHSEEN”);
goto ahmed;
printf(“\nTwinkle”);
printf(“\nKhanna”);
ahmed:
printf(“\nElectrify”);
getch();
}
void main()
{
int a=1;
clrscr();
next:
printf(“\n%d”,a);
a++;
goto next;
}
void main()
{
int a=1;
clrscr();
next:
printf(“\n%d”,a);
a++;
goto next;
if(<a=10)
getch();
}
MARKS SHEET
void main()
{
char name[20],fname[20],clas[6];
int seat,phy,chem,math,urdu,eng,total;
float per;
clrscr();
printf(“Enter Student Name..:”);
gets(name);
printf(“Enter Student Father Name..:”);
gets(fname);
printf(“Enter Student Seat Name..:”);
scanf(“%d”,&seat);
printf(“Enter Student Class..:”);
scanf(“%s”,&clas);
printf(“Enter Physics Marks..:”);
scanf(“%d”,&phy);
printf(“Enter Chemistry Marks..:”);
scanf(“%d”,&chem);
printf(“Enter Math Marks..:”);
scanf(“%d”,&math);
printf(“Enter Urdu Marks..:”);
scanf(“%d”,&urdu);
printf(“Enter English Marks..:”);
scanf(“%d”,&eng);
clrscr();
printf(“MARKS-SHEET”);
printf(“\nName==>%s”,name);
printf(“\nFather Name==>%s”,fname);
printf(“\nSeat Number==>%d”,seat);
printf(“\nClass==>%s”,clas);
printf(“\nPhysics==>%d”,phy);
printf(“\nChemistry==>%d”,chem);
printf(“\nUrdu==>%d”,urdu);
printf(“\nMath==>%d”,math);
printf(“\nEnglish==>%d”,eng);
printf(“\n\nTotal Marks==>%d”,total);
per=total/5;
printf(“\nPer==>%.2f”,per);
if(per>=80)
printf(“\nGrade=A1”);
else
if(per>=70)
printf(“\nGrade=A”);
else
if(per>=60)
printf(“\nGrade=B”);
else
if(per>=50)
printf(“\nGrade=C”);
else
if(per>=40)
printf(“\nGrade=D”);
else
printf(“\nGrade=Nill”);
if(phy<33||chem<33||math<33||urdu<33||eng<22)
printf(“\nRemarks=Fail”);
else
printf(“\nRemarks=Pass”);
getch();
}
Nested if
void main()
{
int choice;
char list;
clrscr();
printf(“Electrify”);
printf(“\n1.English Department”);
printf(“\n2.Computer Department”);
printf(“\n3.Enter Choice..:”);
scanf(“%d”,&choice);
printf(“\n\n\’M’.Male Students”);
printf(“\n’F’.Female Students”);
printf(“\nEnter Form..:”);
scanf(“%s”,&list);
clrscr();
if(choice==1)
if(list==’M’)
{
printf(“\nElectrify”);
printf(“\nEnglish Male Students”);
printf(“\nTehseen\nJibran\nWijdan”);
}
else
{
printf(“\nElectrify”);
printf(“\nEnglish Female Students”);
printf(“\nTwinkle Khanna\nSunali Bendere\nSana”);
}
else
if(list==’M’)
{
printf(“\nElectrify”);
printf(“\nComputer Male Students”);
printf(“\nTehseen\nFarhan\nArsalan\nAhsan”);
printf(“\nFaizan\nAsif\nIrfan\nNasir”);
}
else
{
printf(“\nElectrify”);
printf(“\nComputer Female Students”);
printf(“\n Aishwarya Rai\nTwinkle\n Raveena”);
printf(“\nSana\nJuhi Chawla\nRani”);
}
getch();
}
Armstrong Number
void main()
{
int left,right,mid,no,rem,ans;
clrscr();
printf(“Enter Three Digit No..:”);
scanf(“%d”,&no);
left=no/100;
rem=no%100;
mid=rem/10;
right=rem%10;
ans=(left*left*left)+(mid*mid*mid)+(right*right*right);
if(ans==no)
printf(“\nArmstrong Number”);
else
printf(“Not Armstrong Number”);
getch();
}
LOOPS
Use Loop
There are three types of loops. |
|
1 |
for loop |
2 |
while loop |
3 |
do while loop |
void main()
{
int a;
clrscr();
for(a=1;a<=20;a++)
{
printf(“\nTehseen”);
}
getch();
}
ODD and EVEN Numbers Between 50 to 80
void main()
{
int a;
clrscr();
for(a=51;a<=80;a=a+2)
{
printf(“\n%d”,a);
}
getch();
}
void main()
{
char name[25];
int a;
clrscr();
printf(“Enter Your Name..: “);
scanf(“%s”,&name);
for(a=1;a<=15;a++)
{
printf(“\n%s”,name);
}
getch();
}
void main()
{
int odd=1,even=2;
clrscr();
printf(“Odd Numbers\tEven Numbers”);
for(odd=1;odd<=30;odd=odd+2)
{
printf(“\n%d\t\t%d”,odd,even);
even=even+2;
}
getch();
}
void main()
{
int no;
clrscr();
printf(“Squire\nExample 3*3=9”);
for(no=1;no<=10;no++)
{
printf(“\n%d”,no*no);
}
getch();
}
void main()
{
int a,sqr=1,cube=1;
clrscr();
for(a=1;a<=5;a++)
{
printf(“\n%d=%d=%d”,a,sqr,cube);
sqr=a*a;
cube=a*a*a;
}
getch();
}
void main()
{
int no,a;
clrscr();
printf(“Enter Table No..: “);
scanf(“%d”,&no);
for(a=1;a<=10;a++)
{
printf(“\n%dx%d=%d”,no,a,no*a);
}
getch();
}
#include<dos.h>
void main()
{
int x;
clrscr();
for(x=1;x<=75;x++)
{
clrscr();
textcolor(2);
gotoxy(x,12);
cprintf(“Electrify”);
delay(100);
}
getch();
}
void main()
{
int x;
clrscr();
next:
for(x=1;x<=75;x++)
{
clrscr();
textcolor(x);
gotoxy(x,11);
cprintf(“**********”);
gotoxy(x,12);
cprintf(“Electrify”);
gotoxy(x,13);
cprintf(“**********”);
delay(70);
sound(600);delay(50);nosound();delay(30);
}
for(x=75;x>=1;x--)
{
clrscr();
textcolor(x);
gotoxy(x,11);
cprintf(“**********”);
gotoxy(x,12);
cprintf(“C Language”);
gotoxy(x,13);
cprintf(“**********”);
delay(70);
}
goto next;
}
void main()
{
int no,start,end,a;
clrscr();
printf(“Enter Table No..: “);
scanf(“%d”,&no);
printf(“Enter Starting Table No..: “);
scanf(“%d”,&start);
printf(“Enter End Table No..: “);
scanf(“%d”,&end);
for(a=start;a<=end;a++)
{
printf(“\n%dx%d=%d”,no,a,no*a);
}
getch();
}
void main()
{
int no,a,ans=1;
INTEGER |
|
Long int |
unsigned int |
For Load |
|
%ld | %u |
clrscr();
printf(“Enter a No..: “);
scanf(“%d”,&no);
for(a=1;a<=no;a++)
{
ans=ans*a;
printf(“%dx”,a);
}
printf(“\n%d!=%d”,no,ans);
getch();
}
void main()
{
int a=1,b=1;
clrscr();
for(a=1;a<=21;)
{
printf(“%d %d”,a,b);
a=a+b;
b=a+b;
}
getch();
}
Prime Number
void main()
{
int no,a;
clrscr();
printf(“Enter a No..: “);
scanf(“%d”,&no);
for(a=2;a<=no;a++)
{
if(no%a==0)
break;
}
if(no==a)
printf(“Prime Number”);
else
printf(“Not Prime Number”);
getch();
}
void main()
{
int no,a,large;
printf(“*** Enter 10 Numbers And See Largest Number ***”);
clrscr();
for(a=1;a<=10;a++)
{
printf(“\nEnter a No..” );
scanf(“%d”,&no);
if(no>large)
large=no;
}
printf(“Largest Number=%d”,large);
getch();
}
void main()
{
int a;
clrscr();
for(a=1;a<=20;a++)
{
if(a==10||a==13)
continue;
printf(“\n%d”,a);
}
getch();
}
Nested For Loops
void main()
{
int a,b;
clrscr();
for(a=1;a<=5;a++)
{
|
for(b=1;b<=3;b++)
{
printf(“\nA=%d,B=%d”,a,b);
}
}
getch();
}
void main()
{
int row,col;
clrscr();
for(row=1;row<=6;row++)
{
Outer Loop |
Inner Loop |
for(col=1;col<=row;col++)
{
printf(“%d”,col);
}
printf(“\n”);
}
getch();
}
void main()
{
int a;
clrscr();
for(a=65;a<=90;a++)
{
printf(“%d\t”,a);
}
getch();
}
void main()
{
int a,b,x=30,y=12,sp=1;
clrscr();
for(a=1;a<=6;a++)
{
for(b=1;b<=a;b++)
{
gotoxy(x,y);
printf(“%d”,b);
x--;
}
y--;
x=x-sp;
sp++;
}
getch();
}
void main()
{
int a,b,x=20,y=8,sp=1;
clrscr();
for(a=65;a<=72;a++)
{
for(b=65;b<=a;b++)
{
gotoxy(x,y);
printf(“%c”,b);
x++;
}
y++;
x=x-sp;
sp++;
}
x=25,y=8,sp=1;
for(a=65;a<=72;a++)
{
for(b=65;b<=a;b++)
{
gotoxy(x,y);
printf(“%c”,b);
x--;
}
y++;
x=x+sp;
sp++;
}
getch();
}
void main()
{
int a,b;
clrscr();
for(a=1;a<=3;a++)
{
for(b=1;b<=3;b++)
{
printf(“(%d+%d)=%d,”,a,b,a+b);
}
printf(“\n”);
}
getch();
}
Use While Loop
Void main()
{
int a=1;
clrscr();
while(a<=10)
{
printf(“\n%d”,a);
a++;
}
getch();
}
void main()
{
int no,a=1;
clrscr();
printf(“Enter Table No..: “);
scanf(“%d”,&no);
while(a<=10)
{
printf(“\n%d X %d=%d”,no,a,no*a);
a++;
}
getch();
}
void main()
{
char ch;
clrscr();
while((ch=getch())!=’\r’)
{
printf(“\n%d”,ch);
}
getch();
}
void main()
{
char ch;
ch=getch();
printf(“%c”,ch);
getch();
}
void main()
{
getchar();
getch();
}
Use Of Gets
Now! You Can Use Space in scanf (gets). Like! Zeeshan Ahmed Electrify. |
void main()
{
char name[25];
clrscr();
printf(“Enter Name Here..: “);
gets(name);
printf(“Name=%s”,name);
getch();
}
-----------------------------------------------------------------------------------------------------
Author: Zeeshan Ahmed, Mirpurkhas
ELECTRIFY 1999-2006
------------------------------------------------------------------------------------------------------