C Workshop Programs
C Workshop Programs
h>
int main()
{
printf("Hello Welcome to C Programming");
return 0;
}
//-----------------------------------------------------------------------
int main()
{
printf("good day to all");
return 0;
}
//-------------------------------------------------------------------------
This program will displays the correct output but it is not in well formatted.
\n - Newline
\t - Horz. Tab
\v - Vert. Tab
\b - backspace
\r - Carrage return
\f - Form feed
\a - bell
\\ - to print \
\' - to print '
\" - to print "
\? - to print ?
%% - to print % sign
int main()
{
printf("\n *** Personal Details ***");
printf("\n Name: Amit Ajit Pol");
printf("\n Age: 21 \t Gender: \'M\' ");
printf("\n Address: 12,prarm society\\D, hadapsar, Pune");
printf("\n marks: SSC: 89%% \t HSC: 90.45%%");
printf("\n ruturaj is my good friend \r Ravi");
printf("\n\a\a ABCD\bEFGHIJK");
printf("\n what about you\?");
return 0;
}
//----------------------------------------------------------------------------------------------------------
// Using scanf();
return 0;
}
//------------------------------------------------------------------------------
int main()
{
int x,y,tmp;
tmp=x;
x=y;
y=tmp;
//------------------------------------------------------------------------------
// OR
int main()
{
int a,b;
printf("\n Enter any two nos: ");
scanf("%d %d",&a,&b); // 4 6
printf("Before swap a=%d b=%d",a,b);
a=a*b; // a=24
b=a/b; // b=4
a=a/b; // a=6
//-----------------------------------------------------------------------------------------
int main()
{
int x,y,z;
x=5;
y=9;
z=-x;
printf("\n x=%d \t y=%d \t z=%d",x,y,z); //5,9,-5
++x;
y--;
printf("\n x=%d \t y=%d \t z=%d",x,y,z); // 6,8,-5
z=++x+y--;
printf("\n x=%d \t y=%d \t z=%d",x,y,z); // 7, 7, 15
return 0;
}
//----------------------------------------------------------------------------------
int main()
{
int a=13,b=5;
float c,d=5;
c=a/b;
printf("\n c=%f",c);
c=a/d;
printf("\n c=%f",c);
c=(float)a/b;
printf("\n c=%f",c);
return 0;
}
//--------------------------------------------------------------------------
int main()
{
int x=343;
int t;
float y;
t=sizeof(x);
printf("\n t is %d",t);
t=sizeof(int);
printf("\n t is %d",t);
t=sizeof(y);
printf("\n t is %d",t);
t=sizeof(double);
printf("\n t is %d",t);
t=sizeof(char);
printf("\n t is %d",t);
return 0;
}
//--------------------------------------------------------------------------
// simple interest
int main()
{
int p,n;
floasi;t r,
si=(p*r*n)/100;
//------------------------------------------------------------------------
// WAP to convert the temp from fahrenheit to deg. formula [ (0°C × 9/5) + 32 = 32°F
]
(C*1.8)+32=32F - 9850678451
int main()
{
float fa, cel;
printf("temp is=%f",cel);
return 0;
}
//----------------------------------------------------------------------------------------------------
// Enter any 3 digit number and find the addition of all digits.
int main()
{
int no,rem,tot=0;
rem=no%10;
tot=tot+rem;
no=no/10;
rem=no%10;
tot=tot+rem;
no=no/10;
rem=no%10;
tot=tot+rem;
no=no/10;
printf("\n total is: %d",tot);
return 0;
}
//----------------------------------------------------------------------------------------------------
int main()
{
int x,y,ans;
x=23,y=5;
ans=(x>y);
printf("\n Ans: %d",ans);
printf("\n (x>y) is: %d",ans);
return 0;
}
/////---------------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------------------
int main()
{
int x,y,ans;
x=23,y=5;
ans=(x>y)&&(y%5==0);
printf("\n Ans: %d",ans);
return 0;
}
//-------------------------------------------------------------------------
int main()
{
int x=10,y=12;
int ans;
ans=x&y;
printf("\n x&y is %d",ans);
ans=x|y;
printf("\n x|y is %d",ans);
ans=x^y;
printf("\n x^y is %d",ans);
ans=x<<2;
printf("\n x<<2 is %d",ans);
ans=y>>2;
printf("\n y>>2 is %d",ans);
return 0;
}
//----------------------------------------------------------------------------------
int main()
{
int a,b;
return 0;
}
//---------------------------------------------------------------------------------------
int main()
{
int a,b;
int ans;
printf("\n Enter any two nos: ");
scanf("%d %d",&a,&b);
ans=(a>b) ? a : b ;
return 0;
}
//------------------------------------------------------------------------------------------
ans=(a>b) ? a : b ;
ans=(ans>c) ? ans : c ;
return 0;
}
//-------------------------------------------------------------------------------
int main()
{
int a,b,c;
int ans;
printf("\n Enter any three nos: ");
scanf("%d %d %d",&a,&b,&c);
return 0;
}
///--------------------------------------------------------------------------------------------------------
int main()
{
int x,y;
if(x>y)
{
printf("\n x=%d is max",x);
}
if(y>x)
{
printf("\n y=%d is max",y);
}
return 0;
}
//---------------------------------------------------------------------------------------
// WAP to calc the discount. [ 7% when total bill greater than 800 ]
int main()
{
float p1,p2,p3,tot,dis=0;
tot=p1+p2+p3;
if(tot>800)
{
dis=tot*0.07;
}
return 0;
}
///--------------------------------------------------------------------------------------------------------
// WAP to find the max from two nos using if() else
int main()
{
int x,y;
if(x>y)
{
printf("\n x=%d is max",x);
}
else
{
printf("\n y=%d is max",y);
}
return 0;
}
//---------------------------------------------------------------------------------------
// WAP to calc the discount. [ 5% upto bill=900 and 12% after that ]
int main()
{
float p1,p2,p3,tot,dis=0;
tot=p1+p2+p3;
if(tot<900)
{
dis=tot*0.05;
}
else
{
dis=tot*0.12;
}
return 0;
}
//---------------------------------------------------------------------------------------------------
int main()
{
int n1,n2,n3;
if(n1>n2)
{
if(n1>n3)
{
printf("\n N1=%d is Max",n1);
}
else
{
printf("\n N3=%d is max",n3);
}
}
else
{
if(n2>n3)
{
printf("\n N2=%d is Max",n2);
}
else
{
printf("\n N3=%d is max",n3);
}
}
return 0;
}
//--------------------------------------------------------------------------------------------
// Enter any no thw keyboard, and find it is div by 3 or 5 or both or not by both.
int main()
{
int no;
if(no%3==0)
{
if(no%5==0)
{
printf("\n No is div by 3 and 5 both");
}
else
{
printf("\n No is div by 3 only");
}
}
else
{
if(no%5==0)
{
printf("\n No is div by 5 only");
}
else
{
printf("\n No is not div by 3 and 5 both");
}
}
return 0;
}
//-----------------------------------------------------------------------------------------------------------------
// ladder:
int main()
{
int x,y;
if(x>0&&y>0)
{
printf("\n point present @ 1 st qd");
}
else if(x<0&&y>0)
{
printf("\n point present @ 2 nd qd");
}
else if(x<0&&y<0)
{
printf("\n point present @ 3 rd qd");
}
else if(x>0&&y<0)
{
printf("\n point present @ 4 th qd");
}
else if(x!=0&&y==0)
{
printf("\n point present @ x axis");
}
else if(x==0&&y!=0)
{
printf("\n point present @ y axis");
}
else
{
printf("\n point present @ org");
}
//----------------------------------------------------------------------------------------------------------
// Enter any character, and find it is ucase, lcase, digit or special symbol.
Every character has its own fix bit sequence, which is ASCII Code.
A - Z ==> 65 - 90
a - z ==> 97 - 122
0 - 9 ==> 48 - 57
int main()
{
char ch;
printf("\n Press any key: ");
scanf("%c",&ch);
if(ch>='A'&&ch<='Z')
{
printf("\n Ucase character");
}
else if(ch>='a'&&ch<='z')
{
printf("\n Lcase Character");
}
else if(ch>='0'&&ch<='9')
{
printf("\n Digits");
}
else
{
printf("Special Symbols");
}
return 0;
}
//-----------------------------------------------------------------------------------------------------------
int main()
{
char ch;
if(ch>=65&&ch<='Z')
{
printf("\n Ucase character");
}
else if(ch>='a'&&ch<=122)
{
printf("\n Lcase Character");
}
else if(ch>=48&&ch<=57)
{
printf("\n Digits");
}
else
{
printf("Special Symbols");
}
return 0;
}
//---------------------------------------------------------------------------------------------------
// Enter the sal of emp. and display his designation the the company
#include<stdio.h>
int main()
{
int salary;
printf("enter salary:");
scanf("%d",&salary);
if(salary<=1000)
{
printf("worker");
}
else if(salary>=1000 && salary<=3000)
{
printf("trainee");
}
else if(salary>3000 && salary<=7000)
{
printf("junior");
}
else if(salary>7000 && salary<=11000)
{
printf("senior");
}
else if(salary>11000 && salary<=15000)
{
printf("director");
}
else
{
printf("ceo");
}
return 0;
}
//------------------------------------------------------------------------
#include<stdio.h>
int main()
{
int salary;
printf("enter salary:");
scanf("%d",&salary);
if(salary<=1000)
{
printf("worker");
}
else if(salary<=3000)
{
printf("trainee");
}
else if(salary<=7000)
{
printf("junior");
}
else if(salary<=11000)
{
printf("senior");
}
else if(salary<=15000)
{
printf("director");
}
else
{
printf("ceo");
}
return 0;
}
//=================================================================
=========================
// Using Loops
//=================================================================
========================
int main()
{
int a;
for(a=35;a<45;a=a+1)
{
printf("\n hi");
}
printf("\n End of program");
return 0;
}
//-----------------------------------------------------------
int main()
{
int a;
for(a=5;a<=50;a=a+5)
{
printf("\n hi");
}
printf("\n End of program");
return 0;
}
//-----------------------------------------------------------
int main()
{
int i;
for(i=0;i<10;i++)
{
printf("\n hi");
}
printf("\n End of program");
return 0;
}
//-----------------------------------------------------------
int main()
{
int i;
for(i=10;i>0;i--)
{
printf("\n hi");
}
printf("\n End of program");
return 0;
}
//-----------------------------------------------------------------------------------------------
int main()
{
int i;
for(i=0;i<10;i++)
{
printf("\n %d",i);
}
return 0;
}
//------------------------------------------------------------
int main()
{
int i;
i=0;
for(;i<10;)
{
printf("\n %d",i);
i++;
}
return 0;
}
//-----------------------------------------------------------------------------------------------
int main()
{
int i;
for(i=0;i<10;i++);
{
printf("\n %d",i);
}
printf("\n End Of Program");
return 0;
}
//---------------------------------------------------------------------------------
- Int <init> multiple initializations are possible, but variables must be separated
by comma. same this is applicable to the <inc/dec> block.
- If you want to check the multiple conditions in for() loop the they must be joined
using && or ||
i.e.
for(i=0,j=10;(i<10&&j>2);i++,j--)
{
let see...
//-----------------------------------------------------------
i tot
0 0
0
1 1
2 3
3 6
4 10
5
....
int main()
{
int i,tot;
for(i=0,tot=0;i<10;i++)
{
tot=tot+i;
printf("\n Now Total is %d",tot);
}
printf("\n\n Final Total is %d",tot);
return 0;
}
//----------------------------------------------------------------------------------------
int main()
{
int i,no,tot;
tot=0;
printf("\n Enter any 5 Nos: ");
for(i=0;i<5;i++)
{
scanf("%d",&no);
tot=tot+no;
}
printf("\n Final Total is; %d",tot);
return 0;
}
//-------------------------------------------------------------------------------------
int main()
{
int Number, i, n,Sum = 0;
//--------------------------------------------------------------------------------
no=5
5x1=5
5 x 2 = 10
5 X 3 = 15
5 x 4 = 20
.
.
.
5 x 10 = 50
*/
//--------------------------------------------------------------------------------------
int main()
{
int n, i;
//-----------------------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------
int main()
{
int no,rem,sum;
int main()
{
int no,rem,cnt=0;
for(;no!=0;) //no>0
{
cnt++;
no=no/10;
}
//----------------------------------------------------------------------------------------------------
// WAP to count the even and add digits from the number entered thw keyboard.
int main()
{
int no,rem,edc=0,odc=0;
for(;no!=0;)
{
rem=no%10;
if(rem%2==0)
{
edc++;
}
else
{
odc++;
}
no=no/10;
}
printf("\n Even digit count: %d \t Odd digit count: %d",edc,odc);
return 0;
}
//---------------------------------------------------------------------------------------------------------
//WAP To Add Alternate digit from the number entered thw keyboard.
int main()
{
int no,sum1=0,sum2=0;
int rem,tmp;
for(tmp=1;no!=0;tmp++)
{
rem=no%10;
if(tmp%2==0)
{
sum1=sum1+rem;
}
else
{
sum2=sum2+rem;
}
no=no/10;
}
printf("\n Sum 1: %d \t Sum 2: %d",sum1,sum2);
return 0;
}
int main()
{
int no,sum1=0,sum2=0;
int rem,flag;
flag=0;
for(;no!=0;)
{
rem=no%10;
if(flag==0)
{
sum1=sum1+rem;
flag=1;
}
else
{
sum2=sum2+rem;
flag=0;
}
no=no/10;
}
printf("\n Sum 1: %d \t Sum 2: %d",sum1,sum2);
return 0;
}
//-------------------------------------------------------------------------------------------------
int main()
{
int power,number;
int result=1;
int i = 0;
printf("Power : ");
scanf("%d",&power);
for(i =0;i<power;i++)
{
result = result * number;
}
return 0;
}
//-----------------------------------------------------------------------------------------------------------
No=153 digit count: 3 so [ (3^3) + (5^3) + (1^3) ] ==> [ (27) + (125) + (1) ] ==> 153
int main()
{
int no,dc,tmp,rem,i,t,sum=0;
if(tmp==sum)
{
printf("\n %d is Armstrong Number",tmp);
}
else
{
printf("\n %d is not an Armstrong Number",tmp);
}
return 0;
}
//---------------------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------------------
The Aim of while is also avoid the rep. of code. It is perfect replacement
for any other loop. It is different in terms syntax only
syntax:
while(<cond>)
{
------------;
------------;
------------;
------------;
}
// Enter any no and add even and odd nos from range 1-No.
int main()
{
int no,et=0,ot=0,i;
i=1;
while(i<=no)
{
if(i%2==0)
{
et=et+i;
}
else
{
ot=ot+i;
}
i++;
}
printf("\n Even sum: %d \t Odd Sum: %d",et,ot);
return 0;
}
//-----------------------------------------------------------------------------------------------------
// WAP to Reverse entered No [ using while loop ]
No=456;
to reverse the No, take the single digit rh+ th left and multi. with 10^n, 10^n-1 ...
int main()
{
int no,rem,rev;
rev=0;
while(no!=0)
{
rem=no%10; // 6, 5, 4
rev=(rev*10)+rem; // 6, 65, 654
no=no/10; //45, 4, 0
}
//----------------------------------------------------------------------------------------
int main()
{
int no,rem,rev,tmp;
printf("\n Enter any No: ");
scanf("%d",&no);
tmp=no;
rev=0;
while(no!=0)
{
rem=no%10;
rev=(rev*10)+rem;
no=no/10;
}
if(rev==tmp)
printf("\n %d is palindrome number",tmp);
else
printf("\n %d is not palindrome number",tmp);
return 0;
}
//-------------------------------------------------------------------------------------------------------
int main()
{
int no,d,flg;
flg=0;
d=2;
while(d<=(no/2))
{
if(no%d==0)
{
flg=1;
break;
}
d++;
}
if(flg==0)
printf("\n No=%d is Prime",no);
else
printf("\n No=%d is Not Prime",no);
return 0;
}
//------------------------------------------------------------------------------
*/
int main()
{
int no,d,flg;
//----------------------------------------------------------------------------------------------
int main()
{
int no,dc,tmp,i,result,tot,rem;
printf("\n List Of Armstrong Numbers: ");
for(no=1;no<=10000;no++)
{
tmp=no;
no=tmp;
tot=0;
while(no!=0)
{
rem=no%10;
i=0;
result=1;
while(i<dc)
{
result=result*rem;
i++;
}
tot=tot+result;
no=no/10;
}
no=tmp;
if(tot==tmp)
printf("%6d",tmp);
}
return 0;
}
//--------------------------------------------------------------------------------------------------------------
syntax:
do
{
--------------;
--------------;
--------------;
--------------;
--------------;
}while(<cond>);
int main()
{
int n;
n=1;
do
{
printf("\n %6d --> %-6d --> %-6d",n,(n*n),(n*n*n));
n++;
}while(n<=15);
return 0;
}
//---------------------------------------------------------------------------------------------------
int main()
{
int i;
i=0;
do
{
printf("\n %c --> %d",i,i);
i++;
}while(i<256);
return 0;
}
//---------------------------------------------------------------------------------------------------------
5! = 5 * 4 * 3 * 2 * 1 ==> 120
int main()
{
int n,i=1,f=1;
printf("\n Enter The Number:");
scanf("%d",&n);
do
{
f=f*i;
i++;
}while(i<=n);
int main()
{
int n,f=1;
printf("\n Enter The Number:");
scanf("%d",&n);
do
{
f=f*n;
n--;
}while(n>1);
///------------------------------------------------------------------------------------------------
int main()
{
int x,n,a,b,i,j;
float ans,tot=1.0;
printf("\n Enter the value of x and N: ");
scanf("%d %d",&x,&n);
i=1;
do
{
//a=x^i;
a=1;
j=0;
while(j<i)
{
a=a*x;
j++;
}
// b=i!;
b=1;
j=1;
while(j<=i)
{
b=b*j;
j++;
}
printf(" %d/%d + ",a,b);
ans=(float)a/b;
tot=tot+ans;
i++;
}while(i<=n);
printf("\b\b=%0.3f",tot);
}
//---------------------------------------------------------------------------------------
// WAP to find the rep of entered digit in the number entered thw keyboard.
int main()
{
int no,dig,rem,cnt=0;
do
{
rem=no%10;
if(rem==dig)
{
cnt++;
}
no=no/10;
}while(no!=0);
printf("\n Digit %d is present %d times",dig,cnt);
return 0;
}
//---------------------------------------------------------------------
// Enter any no and display which digit present more than 1 time.
int main()
{
int no,dig,rem,cnt,tmp;
//---------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------
do
{
sum=0;
while(no!=0)
{
rem=no%10;
sum=sum+rem;
no=no/10;
}
no=sum;
}while(no>=10);
return 0;
}
//----------------------------------------------------------------------------
// Pattern Programs
void main()
{
printf("*");
return 0;
}
//----------------------------------------------------------------------
void main()
{
int i;
for(i=0;i<5;i++)
{
printf("*");
}
return 0;
}
//----------------------------------------------------------------------
***
***
***
***
***
void main()
{
int i,j;
for(i=0;i<5;i++) //this loop decides the number of lines
{
for(j=0;j<5;j++) // It will print 1 line
{
printf("*");
}
printf("\n");
}
return 0;
}
//----------------------------------------------------------------------------------
0*
1 **
i 2*
3 **
4 ***
5 **
6 ***
int main()
{
int i,j;
for(i=0;i<7;i++)
{
for(j=0;j<=i;j++)
{
printf("*");
}
printf("\n");
}
//--------------------------------------------------------------------------
0 ****
1 ***
2 ****
3 ***
4 **
5 ***
6 **
7*
8 **
9*
int main()
{
int i,j;
for(i=0;i<10;i++)
{
for(j=0;j<(10-i);j++)
{
printf("*");
}
printf("\n");
}
return 0;
}
//-------------------------------------------------------------------------
// WAP to display the following pattern
*** 0
-** 1
--*** 2
---** 3 i
----* 4
-----** 5
------* 6
0123456
int main()
{
int i,j;
for(i=0;i<7;i++)
{
for(j=0;j<7;j++)
{
if(j<i)
printf(" ");
else
printf("*");
}
printf("\n");
}
return 0;
}
//--------------------------------------
*0
** 1
*2
** 3 i
*** 4
** 5
*** 6
**** 7
int main()
{
int i,j;
for(i=0;i<8;i++)
{
for(j=0;j<8;j++)
{
if(j<(7-i))
{
printf(" ");
}
else
{
printf("*");
}
}
printf("\n");
}
}
//-------------------------------------------------------------------------
int main()
{
int i,j;
for(i=0;i<6;i++)
{
for(j=0;j<(6+i);j++)
{
if(j<(5-i))
printf(" ");
else
printf("*");
}
printf("\n");
}
}
//---------------------------------------------------------------------
*****
***
***
***
*
*
int main()
{
int i,j;
for(i=0;i<6;i++)
{
for(j=0;j<(11-i);j++)
{
if(j<i)
printf(" ");
else
printf("*");
}
printf("\n");
}
}
//--------------------------------------------------------------------
***
* *
* *
* *
***
int main()
{
int i,j;
for(i=0;i<5;i++)
{
for(j=0;j<7;j++)
{
if(i==0||i==4)
printf("*");
else if(j==0||j==6)
printf("*");
else
printf(" ");
}
printf("\n");
}
return 0;
}
//-----------------------------------
// WAP TO PRINT
*
**
* *
* *
* *
*****
int main()
{
int i,j;
for(i=0;i<6;i++)
{
for(j=0;j<(6+i);j++)
{
if(i==5)
printf("*");
else if(j==(5-i)||j==(5+i))
printf("*");
else
printf(" ");
}
printf("\n");
}
}
//-------------------------------------------------------------------------------------
aaaaaaaaaaa
bbbbbbbbbb
ccccccccc
dddddddd
eeeeeee
ffffff
ggggg
hhhh
iii
jj
k
int main()
{
int i,j,t;
t=97;
for(i=0;i<11;i++)
{
for(j=0;j<(11-i);j++)
{
printf("%c",t);
}
t++;
printf("\n");
}
return 0;
}
//------------------------------------------------------------------------
WAP to print following pattern
111111111
22222222
3333333
444444
55555
6666
777
88
9
int main()
{
int i,j,t;
t=1;
for(i=0;i<9;i++)
{
for(j=0;j<(9-i);j++)
{
printf("%d",t);
}
t++;
printf("\n");
}
return 0;
}
//---------------------------------------------------------------------
A
AB
ABC
ABCD
ABCDE
ABCDEF
ABCDEFG
ABCDEFGH
int main()
{
int i,j;
char ch;
for(i=0;i<8;i++)
{
ch='A';
for(j=0;j<8;j++)
{
if(j<(7-i))
{
printf(" ");
}
else
{
printf("%c",ch);
ch++;
}
}
printf("\n");
}
}
//---------------------------------------------------------------------
int main()
{
int i,j;
int t;
for(i=0;i<8;i++)
{
t=1;
for(j=0;j<8;j++)
{
if(j<(7-i))
{
printf(" ");
}
else
{
printf("%d",t);
t++;
}
}
printf("\n");
}
}
//----------------------------------------------------------
54321
4321
321
21
1
int main()
{
int i,j,t;
for(i=0;i<5;i++)
{
t=(5-i);
for(j=0;j<(5-i);j++)
{
printf("%d",t);
t++;
}
printf("\n");
}
return 0;
}
*/
//-------------------------------------------------------------------------
HW
1 A
121 ABA
12321 ABCBA
1234321 ABCDCBA
123454321 ABCDEDCBA
12345654321 ABCDEFEDCBA
//////////////////----------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------
1
121
12321
1234321
123454321
12345654321
int main()
{
int i,j,k,t;
for(i=0;i<6;i++)
{
for(t=1,j=0;j<6;j++)
{
if(j<(5-i))
{
printf(" ");
}
else
{
printf("%d",t);
t++;
}
}
for(t=t-1,j=0;j<i;j++)
{
t--;
printf("%d",t);
}
printf("\n");
}
}
//---------------------------------------------------------------------------
A
ABA
ABCBA
ABCDCBA
ABCDEDCBA
ABCDEFEDCBA
int main()
{
int i,j,k,t;
for(i=0;i<6;i++)
{
for(t=65,j=0;j<6;j++)
{
if(j<(5-i))
{
printf(" ");
}
else
{
printf("%c",t);
t++;
}
}
for(t=t-1,j=0;j<i;j++)
{
t--;
printf("%c",t);
}
printf("\n");
}
}
//------------------------------------------------------------------------------
1
121
12321
1234321
123454321
12345654321
123454321
1234321
12321
121
1
int main()
{
int i,j,t=1;
for(i=0;i<6;i++)
{
t=1;
for(j=0;j<6;j++)
{
if(j<(5-i))
{
printf(" ");
}
else
{
printf("%d",t);
t++;
}
}
for(t=t-1,j=0;j<i;j++)
{
t--;
printf("%d",t);
}
printf("\n");
}
for(i=0;i<5;i++)
{
t=1;
for(j=0;j<6;j++)
{
if(j<=i)
{
printf(" ");
}
else
{
printf("%d",t);
t++;
}
}
for(t=t-1,j=0;j<(4-i);j++)
{
t--;
printf("%d",t);
}
printf("\n");
}
return 0;
}
/*
2 3 4 5 ....10 x 1
4 6 8 10 ....20 x 2
. .
. .
. .
20 30 40 50 ...100 x 10
int main()
{
int i,j;
for(i=1;i<=10;i++)
{
for(j=2;j<=10;j++)
{
printf("%5d",i*j);
}
printf("\n");
}
return 0;
}
//------------------------------------------------------------------
128 64 32 16 8 4 2 1
2 | 38 0
--|----
2 | 19 1
--|----
2|9 1 ^
--|---- |
2|4 0 | 100110
--|---- |
2|2 0 |
--|----
2|1 1
--|----
|0
int main()
{
int num, bin = 0, rem = 0;
int main()
{
int a,b,tmp;
int i,cnt;
}while(--cnt);
}
//------------------------------------------------------------------------
1101 --> 13
*/
int main()
{
int bin,dec,rem,t=1;
printf("\n Enter the binary no: ");
scanf("%d",&bin);
while(bin)
{
rem=bin%10;
bin=bin/10;
if(rem==1)
dec=dec+t;
t=t*2;
}
printf("Decimal is %d",dec);
}
//=================================================================
// Using switch case: It is preferred when nesting grows more than 3 layers.
Syntax:
switch(<opt>)
{
case 1:
----------------;
----------------;
----------------;
break;
case 2:
----------------;
----------------;
----------------;
break;
case 3:
----------------;
----------------;
----------------;
break;
case 4:
----------------;
----------------;
----------------;
break;
default:
----------------;
----------------;
}
//------------------------------------------------------------------------------------------
int main()
{
int opt;
float a,b,ans=0.0;
switch(opt)
{
case 1:
printf("\n In Case 1: ");
ans=a+b;
break;
case 2:
printf("\n In Case 2: ");
ans=a-b;
break;
case 3:
printf("\n In Case 3: ");
ans=a*b;
break;
case 4:
printf("\n In Case 4: ");
ans=a/b;
break;
default:
printf("\n Incorrect Option");
}
printf("\n Ans is: %0.2f",ans);
return 0;
}
//------------------------------------------------------------------------------------------
int main()
{
char opt;
float a,b,ans=0.0;
switch(opt)
{
case 'a':
printf("\n In Case 1: ");
ans=a+b;
break;
case 'b':
printf("\n In Case 2: ");
ans=a-b;
break;
case 'c':
printf("\n In Case 3: ");
ans=a*b;
break;
case 'd':
printf("\n In Case 4: ");
ans=a/b;
break;
default:
printf("\n Incorrect Option");
}
printf("\n Ans is: %0.2f",ans);
return 0;
}
//---------------------------------------------------------------------
int main()
{
char opt;
float a,b,ans=0.0;
switch(opt)
{
case 'A':
case 'a':
printf("\n In Case 1: ");
ans=a+b;
break;
case 'B':
case 'b':
printf("\n In Case 2: ");
ans=a-b;
break;
case 'C':
case 'c':
printf("\n In Case 3: ");
ans=a*b;
break;
case 'D':
case 'd':
printf("\n In Case 4: ");
ans=a/b;
break;
default:
printf("\n Incorrect Option");
}
printf("\n Ans is: %0.2f",ans);
return 0;
}
//---------------------------------------------------------------------
int main()
{
int opt;
float a,b,ans=0.0;
if(opt>=1&&opt<=4)
{
printf("\n Enter any two nos: ");
scanf("%f %f",&a,&b);
switch(opt)
{
case 1:
printf("\n In Case 1: ");
ans=a+b;
break;
case 2:
printf("\n In Case 2: ");
ans=a-b;
break;
case 3:
printf("\n In Case 3: ");
ans=a*b;
break;
case 4:
printf("\n In Case 4: ");
ans=a/b;
break;
}
printf("\n Ans is: %0.2f",ans);
}
else
{
printf("\n Incorrect Option....!!!");
}
return 0;
}
//---------------------------------------------------------------------
int main()
{
int opt,i;
float a,b,ans=0.0;
while(i<3)
{
printf("\n ---------- Menu ------------\n");
printf("\n 1.add \n 2.sub \n 3.Multi \n 4.div");
printf("\n Select you option: ");
scanf("%d",&opt);
if(opt>=1&&opt<=4)
{
printf("\n Enter any two nos: ");
scanf("%f %f",&a,&b);
switch(opt)
{
case 1:
printf("\n In Case 1: ");
ans=a+b;
break;
case 2:
printf("\n In Case 2: ");
ans=a-b;
break;
case 3:
printf("\n In Case 3: ");
ans=a*b;
break;
case 4:
printf("\n In Case 4: ");
ans=a/b;
break;
}
printf("\n Ans is: %0.2f",ans);
}
else
{
printf("\n Incorrect Option....!!!");
}
i++;
}
return 0;
}
//---------------------------------------------------------------------
int main()
{
int cnt,opt,i;
float a,b,ans=0.0;
while(i<cnt)
{
printf("\n ---------- Menu ------------\n");
printf("\n 1.add \n 2.sub \n 3.Multi \n 4.div");
printf("\n Select you option: ");
scanf("%d",&opt);
if(opt>=1&&opt<=4)
{
printf("\n Enter any two nos: ");
scanf("%f %f",&a,&b);
switch(opt)
{
case 1:
printf("\n In Case 1: ");
ans=a+b;
break;
case 2:
printf("\n In Case 2: ");
ans=a-b;
break;
case 3:
printf("\n In Case 3: ");
ans=a*b;
break;
case 4:
printf("\n In Case 4: ");
ans=a/b;
break;
}
printf("\n Ans is: %0.2f",ans);
}
else
{
printf("\n Incorrect Option....!!!");
}
i++;
}
return 0;
}
//---------------------------------------------------------------------
int main()
{
int opt,i;
float a,b,ans=0.0;
while(1)
{
printf("\n ---------- Menu ------------\n");
printf("\n 1.add \n 2.sub \n 3.Multi \n 4.div \n 5.stop");
printf("\n Select you option: ");
scanf("%d",&opt);
if(opt==5)
{
printf("\n Thanks for using our Application.");
break;
}
else if(opt>=1&&opt<=4)
{
printf("\n Enter any two nos: ");
scanf("%f %f",&a,&b);
switch(opt)
{
case 1:
printf("\n In Case 1: ");
ans=a+b;
break;
case 2:
printf("\n In Case 2: ");
ans=a-b;
break;
case 3:
printf("\n In Case 3: ");
ans=a*b;
break;
case 4:
printf("\n In Case 4: ");
ans=a/b;
break;
}
printf("\n Ans is: %0.2f",ans);
}
else
{
printf("\n Incorrect Option....!!!");
}
i++;
}
return 0;
}
//---------------------------------------------------------------------
int main()
{
int opt,i;
float a,b,ans=0.0;
while(1)
{
printf("\n ---------- Menu ------------\n");
printf("\n 1.add \n 2.sub \n 3.Multi \n 4.div \n 5.stop");
printf("\n Select you option: ");
scanf("%d",&opt);
if(opt>=1&&opt<=5)
{
printf("\n Enter any two nos: ");
scanf("%f %f",&a,&b);
switch(opt)
{
case 1:
printf("\n In Case 1: ");
ans=a+b;
break;
case 2:
printf("\n In Case 2: ");
ans=a-b;
break;
case 3:
printf("\n In Case 3: ");
ans=a*b;
break;
case 4:
printf("\n In Case 4: ");
ans=a/b;
break;
case 5:
printf("\n Thank You..!! Visit Again..!!!");
exit(0);
}
printf("\n Ans is: %0.2f",ans);
}
else
{
printf("\n Incorrect Option....!!!");
}
i++;
}
return 0;
}
//=================================================================
=============================
- Using goto: It is not recommanded to use, coz it will disturb your logic.
syntax:
<tag>: ----------;
-------------;
-------------;
-------------;
goto <tag>;
//Enter the N nos and add even nos, keep doing this till user enters the 0.
int main()
{
int no,et;
et=0;
back: printf("\n Enter any no: ");
scanf("%d",&no);
if(no==0)
{
goto fwd;
}
else if(no%2==0)
{
et=et+no;
}
goto back;
fwd: printf("\n Answer is: %d",et);
return 0;
}
//----------------------------------------------------------------------------------
// Using the continue: It will keep loop in running state without considering
remaining
// body of loop.
int main()
{
int i,j;
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
{
printf("-");
if(j>i)
{
continue;
}
printf("%d",j);
}
printf("\n");
}
return 0;
}
Output:
-0----
-0-1---
-0-1-2--
-0-1-2-3-
-0-1-2-3-4
//----------------------------------------------------------------
// Using break: It will terminate the loop and pass the the control out of switch from
case.
int main()
{
int i,j;
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
{
printf("-");
if(j>i)
{
break;
}
printf("%d",j);
}
printf("\n");
}
return 0;
}
output;
-0-
-0-1-
-0-1-2-
-0-1-2-3-
-0-1-2-3-4
//--------------------------------------------------------------------------------------------
// return: It is used to pass back one value from called function to calling function.
// (will see in Function topic)
//-------------------------------------------------------------------------------------------------
Array
//-----------------------------------------------------------------------------------------------
Array is indexed collection of elements having same data type, continually arranged
in
the memory. It is derived type.
decl. of an array:
syntax:
<data_type> <ar_nm>[<index>];
e.g.
int x[10];
x 0 1 2 3 4 5 6 7 8 9
[][][][][][][][][][]
Note:
- Array counting is zero based.
- x[i] --> gives the value at ith location.
- &x[i] --> gives an address of i th location.
- init of array:
=================
int x[5]={110,390,4,23,78};
or
int x[]={110,390,4,23,78}; // only in case of initi.
x 0 1 2 3 4
[110] [390] [4] [23] [78]
int main()
{
int x[5]={110,390,4,23,78};
int i;
//-----------------------------------------------------
// Using %Nd to insert the space bet two element at the time of display.
int main()
{
int x[5]={110,390,4,23,78};
int i;
//-----------------------------------------------------------------------------
x
[] [] [] [] [] [] [] [] [] []
int main()
{
int x[10];
int i;
//----------------------------------------------------------------------------------------------
// WAP to Copy the array elements from one location to another location.
x
[23][90][65][12][8]
y
[][][][][]
int main()
{
// decl
int x[5],y[5],i;
// input
printf("\n Enter the 5 nos: ");
for(i=0;i<5;i++)
{
scanf("%d",&x[i]);
}
// process
for(i=0;i<5;i++)
{
y[i]=x[i];
}
// Display
printf("\n x elements: ");
for(i=0;i<5;i++)
{
printf("%5d",x[i]);
}
printf("\n y elements: ");
for(i=0;i<5;i++)
{
printf("%5d",y[i]);
}
return 0;
}
//------------------------------------------------------------------------------------
// Enter the 10 nos and count the prime nos from it.
int main()
{
int ar[10],d,flg,i;
//----------------------------------------------------------------------------------------
// Enter array any size and reverse each element of array and store it in same
location
//----------------------------------------------------------------------------------------------------
int main()
{
int x[10];
int i,no,cnt;
cnt=0;
i=0;
while(i<10)
{
if(x[i]==no)
{
cnt++;
}
i++;
}
printf("\n %d is present %d times",no,cnt);
return 0;
}
//------------------------------------------------------------------------------------
// array: 12 56 89 12 45 56 12 78
// Output
// 12 present 3 times
// 56 present 2 times
int main()
{
int x[10],i,j,cnt;
for(i=0;i<10;i++)
{
cnt=0;
for(j=0;j<10;j++)
{
if(x[i]==x[j])
{
if(i!=j)
x[j]=-1;
cnt++;
}
}
if(cnt>1&&x[i]!=-1)
printf("\n %d is present %d times",x[i],cnt);
}
return 0;
}
//------------------------------------------------------------------------------------------
int main()
{
int x[10],i,max,min;
printf("\n Enter the elements: ");
for(i=0;i<10;i++)
{
scanf("%d",&x[i]);
}
min=x[0];
max=x[0];
for(i=1;i<10;i++)
{
if(x[i]<min)
{
min=x[i];
}
else if(x[i]>max)
{
max=x[i];
}
}