0% found this document useful (0 votes)
110 views85 pages

C Workshop Programs

This document contains multiple C programs demonstrating basic concepts like: 1. Printing text to the screen using printf statements. 2. Using escape sequences to format output. 3. Taking user input using scanf and displaying output. 4. Simple math and logical operators. 5. Conditional statements like if/else. The document appears to be examples or practice programs for someone learning basic C programming. It covers many fundamental concepts through short standalone programs.

Uploaded by

sws wsxxw
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
110 views85 pages

C Workshop Programs

This document contains multiple C programs demonstrating basic concepts like: 1. Printing text to the screen using printf statements. 2. Using escape sequences to format output. 3. Taking user input using scanf and displaying output. 4. Simple math and logical operators. 5. Conditional statements like if/else. The document appears to be examples or practice programs for someone learning basic C programming. It covers many fundamental concepts through short standalone programs.

Uploaded by

sws wsxxw
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 85

#include<stdio.

h>
int main()
{
printf("Hello Welcome to C Programming");
return 0;
}

//-----------------------------------------------------------------------

prog. statment: xxxxx xxxxxx xxxxxxxxx xxxxx xxxx


written by: xxx xxxxxx xxxx
class: xxxxxxx
date: xx xx xx

// first program: write a program to display the "good day to all"

int main()
{
printf("good day to all");
return 0;
}

//-------------------------------------------------------------------------

// WAP to display the personal details


int main()
{
printf("*** Personal Details ***");
printf("Name: Amit Ajit Pol");
printf("Age: 21 Gender: M");
printf("Address: 12,prarm society, hadapsar, Pune");
printf("marks: SSC: 89% HSC: 90.45%");
printf("what about you?");
return 0;
}

This program will displays the correct output but it is not in well formatted.

so to display the output in well formatted manner, the escape sequence


characters are used.

List of escape sequence characters:


====================================

\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();

// In C, All decl. must be @ top in main()


int main()
{
float f1;
char ch;
int a,b,c,x;

printf("\n Enter the char int and fractional value: ");


scanf("%c %d %f",&ch,&x,&f1);
printf("\n ch is: %c \t x=%d val of f1 is %f",ch,x,f1);

printf("\n Enter the 3 int values: ");


scanf("%d %d %d",&a,&b,&c);
printf("\n ay%d \t b=%d \t c=%d",a,b,c);

return 0;
}

//------------------------------------------------------------------------------

// WAP to interchange the value of two variables

int main()
{
int x,y,tmp;

printf("\n Enter any 2 nos: ");


scanf("%d %d",&x,&y);

printf("\n Before Interchange: x=%d \t y=%d",x,y);

tmp=x;
x=y;
y=tmp;

printf("\n After Interchange: x=%d \t y=%d",x,y);


return 0;
}

//------------------------------------------------------------------------------

// WAP to interchange the value of two variables without using tmp


int main()
{
int x=10, y=20;
printf("Before swap x=%d y=%d",x,y);
x=x+y; // x=30
y=x-y; // y=10
x=x-y; // x=20
printf("\nAfter swap x=%d y=%d",x,y);
return 0;
}

// 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

printf("\nAfter swap a=%d b=%d",a,b);


return 0;
}

//-----------------------------------------------------------------------------------------

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,

printf("\n Enter the pa,roi and noy: ");


scanf("%d %f %d",&p,&r,&n);

si=(p*r*n)/100;

printf("\n Simple Interest is %f",si);


return 0;
}

//------------------------------------------------------------------------

// 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("Enter a temp in fa: ");


scanf("%f", &fa);

cel = (5/9.0) * (fa - 32);

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;

printf("\n Enter any 3 digit number: ");


scanf("%d",&no);

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);

printf("\n (x>y) is: %d",(x>y));

printf("\n (x!=y) is: %d",(x!=y));

printf("\n (x%%5==0) is: %d",(x%5==0));

printf("\n (x<100) is: %d",(x<100));

printf("\n (x==y) is: %d",(x==y));

return 0;
}
/////---------------------------------------------------------------------------------------------

//----------------------------------------------------------------------------------------------------

int main()
{
int x,y,ans;

x=23,y=5;
ans=(x>y)&&(y%5==0);
printf("\n Ans: %d",ans);

printf("\n (x>y)&&(y%%5==0) is: %d",(x>y)&&(y%5==0));


printf("\n (x<y)&&(y%%5==0) is: %d",(x<y)&&(y%5==0));

printf("\n (x<y)||(y%%5==0) is: %d",(x<y)||(y%5==0));


printf("\n (x<y)||(y%%5!=0) is: %d",(x<y)||(y%5!=0));

printf("\n (x==y) is: %d",(x==y));


printf("\n !(x==y) is: %d",!(x==y));

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;
}

//----------------------------------------------------------------------------------

// WAP to find max bet two nos.

int main()
{
int a,b;

printf("\n Enter any two nos: ");


scanf("%d %d",&a,&b);

(a>b) ? printf("\n a=%d is max") : printf("\b b=%d is max",b) ;

return 0;
}

//---------------------------------------------------------------------------------------

// WAP to find max bet two nos.

int main()
{
int a,b;
int ans;
printf("\n Enter any two nos: ");
scanf("%d %d",&a,&b);

ans=(a>b) ? a : b ;

printf("\n Max from two nos: %d",ans);

return 0;
}

//------------------------------------------------------------------------------------------

// WAP to find the max from 3 using conditional operator


int main()
{
int a,b,c;
int ans;
printf("\n Enter any three56 nos: ");
scanf("%d %d %d",&a,&b,&c);

ans=(a>b) ? a : b ;

ans=(ans>c) ? ans : c ;

printf("\n Max from three nos: %d",ans);

return 0;
}

//-------------------------------------------------------------------------------

// Using the Nesting of Conditional Operator

// WAP to find the max from 3 using conditional operator

int main()
{
int a,b,c;
int ans;
printf("\n Enter any three nos: ");
scanf("%d %d %d",&a,&b,&c);

ans=(a>b) ? (a>c)?a:c : (b>c)?b:c ;

printf("\n Max from three nos: %d",ans);

return 0;
}
///--------------------------------------------------------------------------------------------------------

// WAP to find the max from two nos using if()

int main()
{
int x,y;

printf("\n Enter any two nos: ");


scanf("%d %d",&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;

printf("\n Enter the price of 3 items: ");


scanf("%f %f %f",&p1,&p2,&p3);

tot=p1+p2+p3;

if(tot>800)
{
dis=tot*0.07;
}

printf("\n Total Bill: %f",tot);


printf("\n Discount: %f",dis);
printf("\n Pay Rs: %f",(tot-dis));

return 0;
}

///--------------------------------------------------------------------------------------------------------

// WAP to find the max from two nos using if() else

int main()
{
int x,y;

printf("\n Enter any two nos: ");


scanf("%d %d",&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;

printf("\n Enter the price of 3 items: ");


scanf("%f %f %f",&p1,&p2,&p3);

tot=p1+p2+p3;

if(tot<900)
{
dis=tot*0.05;
}
else
{
dis=tot*0.12;
}

printf("\n Total Bill: %f",tot);


printf("\n Discount: %f",dis);
printf("\n Pay Rs: %f",(tot-dis));

return 0;
}

//---------------------------------------------------------------------------------------------------

// WAP to find max from 3 using the if() else

int main()
{
int n1,n2,n3;

printf("\n Enter any 3 nos: ");


scanf("%d %d %d",&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;

printf("\n Enter the no: ");


scanf("%d",&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;

printf("\n Enter the x and y co-ordinates of point: ");


scanf("%d %d",&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");
}

printf("\n End of program");


return 0;
}

//----------------------------------------------------------------------------------------------------------

// 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;

printf("\n Press any key: ");


scanf("%c",&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

upto 1000 -> worker


1K to 3K --> Tr
3K - 7K --> Jr
7K - 11K --> sr
11k - 15K --> dr
15k+ ----> CEO

#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;
}

Note carefully that, condition is dependent on the initial value of iterator(a),which is


choosen
according to the no of iterations you want. the incr/dect in the iterator is also plays an
important
role.

//-----------------------------------------------------------

int main()
{
int i;
for(i=0;i<10;i++)
{
printf("\n hi");
}
printf("\n End of program");
return 0;
}

//-----------------------------------------------------------

Or It is possible to attend same output using

int main()
{
int i;
for(i=10;i>0;i--)
{
printf("\n hi");
}
printf("\n End of program");
return 0;
}

//-----------------------------------------------------------------------------------------------

// WAP to display first 10 nos

int main()
{
int i;

for(i=0;i<10;i++)
{
printf("\n %d",i);
}

return 0;
}

//------------------------------------------------------------

// Note that the <init> and <inc/dec> blocks are optional

int main()
{
int i;

i=0;
for(;i<10;)
{
printf("\n %d",i);
i++;
}

return 0;
}

//-----------------------------------------------------------------------------------------------

// Note that, ; does not present after the for() loop,


// if you write the ; then it will run as a single line loop.

int main()
{
int i;

for(i=0;i<10;i++);
{
printf("\n %d",i);
}
printf("\n End Of Program");
return 0;
}

//---------------------------------------------------------------------------------

// WAP to add first 10 nos

- 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;
}

//-------------------------------------------------------------------------------------

// WAP to add any N nos using loop

int main()
{
int Number, i, n,Sum = 0;

printf("\n Please Enter Element Count: ");


scanf("%d", &Number);

for(i = 1; i <= Number; i++)


{
printf("\n Enter n: ");
scanf("%d",&n);
Sum = Sum + n;
}

printf("Sum of Numbers = %d", Sum);


return 0;
}

//--------------------------------------------------------------------------------

// Enter any no, and display it multi table

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;

printf("Enter an integer: ");


scanf("%d", &n);

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


{
printf("%d * %d = %d \n", n, i, n * i);
}
return 0;
}

//-----------------------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------

// add all digits of no

int main()
{
int no,rem,sum;

printf("\n Enter the no: ");


scanf("%d",&no);

for(sum=0; no!=0;) //no>0


{
rem=no%10;
sum=sum+rem;
no=no/10;
}

printf("\n Addition of all digits: %d",sum);


}
//----------------------------------------------------------------------------------------

// WAP to Count the digits

int main()
{
int no,rem,cnt=0;

printf("\n Enter the no: ");


scanf("%d",&no);

for(;no!=0;) //no>0
{
cnt++;
no=no/10;
}

printf("\n Digit count: %d",cnt);


return 0;
}

//----------------------------------------------------------------------------------------------------

// WAP to count the even and add digits from the number entered thw keyboard.

no=13478 odd digits: 3 even digits: 2

int main()
{
int no,rem,edc=0,odc=0;

printf("\n Enter the no: ");


scanf("%d",&no);

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.

// 1563 ---> (3+5=8) (6+1=7)

int main()
{
int no,sum1=0,sum2=0;
int rem,tmp;

printf("\n Enter any No: ");


scanf("%d",&no);

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;
}

// OR Using the concept of flag

int main()
{
int no,sum1=0,sum2=0;
int rem,flag;

printf("\n Enter any No: ");


scanf("%d",&no);

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;
}

//-------------------------------------------------------------------------------------------------

// WAP to calc x^y


// e.g. 2^4 ==> 2*2*2*2 ==> 16

int main()
{
int power,number;
int result=1;
int i = 0;

printf("Enter a number : ");


scanf("%d",&number);

printf("Power : ");
scanf("%d",&power);

for(i =0;i<power;i++)
{
result = result * number;
}

printf("%d to the power of %d is = %d\n",number,power,result);

return 0;
}

//-----------------------------------------------------------------------------------------------------------

// Nesting of for() loops:


//-------------------------

// WAP to find entered number is Armstrong or Not

No=153 digit count: 3 so [ (3^3) + (5^3) + (1^3) ] ==> [ (27) + (125) + (1) ] ==> 153

No=1634 digit count: 4 s0 [ (4^4) + (3^4) + (6^4) + (1^4) ] ===> 1634

int main()
{
int no,dc,tmp,rem,i,t,sum=0;

printf("\n Enter the no: ");


scanf("%d",&no);
tmp=no;

for( dc=0 ; no!=0 ; no=no/10,dc++ );


// printf("\n No: %d \t Digit Count: %d",no,dc);

for( no=tmp ; no!=0 ; no=no/10 )


{
rem=no%10;

for(t=1,i=0 ; i<dc ; i++ )


{
t=t*rem;
}
// printf("\n t is %d",t);
sum=sum+t;
// printf("\n Sum: %d",sum);
}

if(tmp==sum)
{
printf("\n %d is Armstrong Number",tmp);
}
else
{
printf("\n %d is not an Armstrong Number",tmp);
}
return 0;
}

//---------------------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------------------

Using the while() loop:

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;

printf("\n Enter any no N for range(0-N): ");


scanf("%d",&no);

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;

(6*1) + (5*10) + (4*100) ==> 6+50+400 ==> 456

to reverse the No, take the single digit rh+ th left and multi. with 10^n, 10^n-1 ...

i.e. (6*100) (5*10) (4*1) ==> 654

int main()
{
int no,rem,rev;

printf("\n Enter any No: ");


scanf("%d",&no); // 456

rev=0;
while(no!=0)
{
rem=no%10; // 6, 5, 4
rev=(rev*10)+rem; // 6, 65, 654
no=no/10; //45, 4, 0
}

printf("\n Reverse No: %d",rev);


return 0;
}

//----------------------------------------------------------------------------------------

// Enter any number and find it is palindrome number or not.

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;
}

//-------------------------------------------------------------------------------------------------------

// WAP to check entered number is prime or not.

11,23 --> prime number 15,34 --> not prime

int main()
{
int no,d,flg;

printf("\n Enter any no: ");


scanf("%d",&no);

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;
}

//------------------------------------------------------------------------------

// Display the list of prime nos from 10-50

*/

int main()
{
int no,d,flg;

printf("\n Prime List from(10-50)");


for(no=10;no<=50;no++)
{
flg=0;
d=2;
while(d<=(no/2))
{
if(no%d==0)
{
flg=1;
break;
}
d++;
}
if(flg==0)
printf("%5d",no);
}
return 0;
}

//----------------------------------------------------------------------------------------------

// Nesting of for() and while() loop.


// Display the list of Armstrong Nos from range 1-1000.

int main()
{
int no,dc,tmp,i,result,tot,rem;
printf("\n List Of Armstrong Numbers: ");
for(no=1;no<=10000;no++)
{
tmp=no;

for( dc=0 ; no!=0 ; dc++,no=no/10 );

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;
}
//--------------------------------------------------------------------------------------------------------------

// Using do while loop

syntax:
do
{
--------------;
--------------;
--------------;
--------------;
--------------;
}while(<cond>);

// WAP to display the squares and cubes of number 1-15

int main()
{
int n;

n=1;
do
{
printf("\n %6d --> %-6d --> %-6d",n,(n*n),(n*n*n));
n++;
}while(n<=15);

return 0;
}

//---------------------------------------------------------------------------------------------------

// WAP to Display the List of characters with ASCII Values

int main()
{
int i;

i=0;
do
{
printf("\n %c --> %d",i,i);
i++;
}while(i<256);

return 0;
}

//---------------------------------------------------------------------------------------------------------

// Enter any Number thw keyboard and find its factorial.

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);

printf("\n The Factorial of %d is %d",n,f);


return 0;
}
// ----------- OR --------

int main()
{
int n,f=1;
printf("\n Enter The Number:");
scanf("%d",&n);

do
{
f=f*n;
n--;
}while(n>1);

printf("\n The Factorial is %d",f);


return 0;
}

///------------------------------------------------------------------------------------------------

ans=1 + x + (x^2/2!) + (x^3/3!) + (x^4/4!) + ....+ (x^N/N!)

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.

// No: 28422 Digit: 2


// 2 rep. 3 times in a number

int main()
{
int no,dig,rem,cnt=0;

printf("\n Enter the No: ");


scanf("%d",&no);
printf("\n Enter the Digit: ");
scanf("%d",&dig);

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.

// No: 12331 o/p: 1 present 2 times


// 3 present 2 times

int main()
{
int no,dig,rem,cnt,tmp;

printf("\n Enter the No: ");


scanf("%d",&no);
tmp=no;
for(dig=0;dig<10;dig++)
{
cnt=0;
no=tmp;
do
{
rem=no%10;
if(rem==dig)
{
cnt++;
}
no=no/10;
}while(no!=0);
if(cnt>1)
printf("\n Digit %d is present %d times",dig,cnt);
}
return 0;
}
//-----------------------------------------------------------------------

// WAP to display the missing digits in number.


No: 19553 Missing Digits: 0 2 4 6 7 8
int main()
{
int no,dig,rem,flg,tmp;

printf("\n Enter the No: ");


scanf("%d",&no);
tmp=no;
printf("\n List of Missing digits: ");
for(dig=0;dig<10;dig++)
{
flg=0;
no=tmp;
do
{
rem=no%10;
if(rem==dig)
{
flg=1;
break;
}
no=no/10;
}while(no!=0);
if(flg==0)
printf("%5d",dig);
}
return 0;
}

//---------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------

// WAP to print the digital sum of a number.

985 --> 5+9+8 = 22 --> 2+2 --> 4


*/
int main()
{
int no,rem,sum;

printf("\n Enter the no: ");


scanf("%d",&no);

do
{
sum=0;
while(no!=0)
{
rem=no%10;
sum=sum+rem;
no=no/10;
}
no=sum;
}while(no>=10);

printf("\n Digital sum is: %d",sum);

return 0;
}

//----------------------------------------------------------------------------

// Pattern Programs

// suppose i want to print *

void main()
{
printf("*");
return 0;
}

//----------------------------------------------------------------------

// Now I want to print ***

void main()
{
int i;
for(i=0;i<5;i++)
{
printf("*");
}
return 0;
}

//----------------------------------------------------------------------

// Now I want to print

***
***
***
***
***

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;
}

//----------------------------------------------------------------------------------

// WAP to Print the following pattern

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");
}

//--------------------------------------------------------------------------

// WAP to print the following pattern

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;
}
//--------------------------------------

// WAP to display following pattern

*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");
}
}

//-------------------------------------------------------------------------

WAP to print following pattern


*
*
***
***
***
*****

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");
}
}

//---------------------------------------------------------------------

WAP to print following pattern

*****
***
***
***
*
*

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");
}
}

//--------------------------------------------------------------------

// WAPP to display following pattern

***
* *
* *
* *
***

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");
}
}
//-------------------------------------------------------------------------------------

// Numeric and character patterns

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;
}

//---------------------------------------------------------------------

// WAP to print the following pattern

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");
}
}
//---------------------------------------------------------------------

// WAP to print the following pattern


1
12
123
1234
12345
123456
1234567
12345678

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");
}
}
//----------------------------------------------------------

// WAP to print following pattern

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");
}
}

//---------------------------------------------------------------------------

// WAP to display the following pattern

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");
}
}
//------------------------------------------------------------------------------

// WAP to display the following pattern.

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;
}
/*

// WAP to display the multi. table of 2-10

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;
}

//------------------------------------------------------------------

// WAP to convert the decimal number into binary.

128 64 32 16 8 4 2 1

38 --> 0010 0110

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;

printf("Enter a decimal number\n");


scanf("%d", &num);

printf("\nBinary equivalent of %d is ", num);


while(num)
{
rem = num % 2;
num = num / 2;
bin = (bin*10) + rem;
}
printf("%d\n", bin);
return 0;
}
//------------------------------------------------------------------------------
WAP to display Fibonacci series: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34

int main()
{
int a,b,tmp;
int i,cnt;

printf("\n Enter the No of terms you want: ");


scanf("%d",&cnt);

printf("\n The Fibonacci series is:");


a=0;
b=1;
i=0;
do
{
printf("%5d",a);
tmp=a;
a=b;
b=tmp+a;

}while(--cnt);
}

//------------------------------------------------------------------------

// WAP to convert the number from binary to decimal

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:
----------------;
----------------;
}

//------------------------------------------------------------------------------------------

// WAP to perform the arithmetic operations using switch case.


[ Using the int case constant]

int main()
{
int opt;
float a,b,ans=0.0;

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);

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;
default:
printf("\n Incorrect Option");
}
printf("\n Ans is: %0.2f",ans);
return 0;
}
//------------------------------------------------------------------------------------------

// WAP to perform the arithmetic operations using switch case.


[ Using the character case constant] - Using single character as a case constant

int main()
{
char opt;
float a,b,ans=0.0;

printf("\n ---------- Menu ------------\n");


printf("\n a.add \n b.sub \n c.Multi \n d.div");
printf("\n Select you option: ");
scanf("%c",&opt);

printf("\n Enter any two nos: ");


scanf("%f %f",&a,&b);

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;
}
//---------------------------------------------------------------------

// Using multiple characters as a case constant

int main()
{
char opt;
float a,b,ans=0.0;

printf("\n ---------- Menu ------------\n");


printf("\n a.add \n b.sub \n c.Multi \n d.div");
printf("\n Select you option: ");
scanf("%c",&opt);

printf("\n Enter any two nos: ");


scanf("%f %f",&a,&b);

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;
}
//---------------------------------------------------------------------

// Using switch with-in if()

int main()
{
int opt;
float a,b,ans=0.0;

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....!!!");
}
return 0;
}

//---------------------------------------------------------------------

// Using switch with-in loop (fix iterations)

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;
}

//---------------------------------------------------------------------

// Using switch with-in loop (variable iterations)

int main()
{
int cnt,opt,i;
float a,b,ans=0.0;

printf("\n How many times you want the execution: ");


scanf("%d",&cnt);

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;
}
//---------------------------------------------------------------------

// Using switch with-in loop (Infinite iterations) Break to terminate

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;
}
//---------------------------------------------------------------------

// Using switch with-in loop (Infinite iterations) exit() function to terminate

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 Un-Conditional Control Statments


=======================================

- 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]

// WAP to init and display the array.

int main()
{
int x[5]={110,390,4,23,78};
int i;

printf("\n Values are: ");


for(i=0;i<5;i++)
{
printf(" %d",x[i]);
}
printf("\n End of program");
return 0;
}

//-----------------------------------------------------

// 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;

printf("\n Values are: ");


for(i=0;i<5;i++)
{
printf("%5d",x[i]);
}
printf("\n End of program");
return 0;
}

//-----------------------------------------------------------------------------

// Enter the array of 10 nos thw keyboard and display it.

x
[] [] [] [] [] [] [] [] [] []

int main()
{
int x[10];
int i;

printf("\n Enter any 10 Nos: ");


for(i=0;i<10;i++)
{
scanf("%d",&x[i]);
}

printf("\n Values are: ");


for(i=0;i<10;i++)
{
printf("%5d",x[i]);
}
printf("\n End of program");
return 0;
}

//----------------------------------------------------------------------------------------------

// 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;

printf("\n Enter any 10 no: ");


for(i=0;i<10;i++)
{
scanf("%d",&ar[i]);
}

printf("\n Prime List: ");


for(i=0;i<10;i++)
{
d=2;
flg=0;
while(d<=(ar[i]/2))
{
if(ar[i]%d==0)
{
flg=1;
break;
}
d++;
}
if(flg==0)
printf("%5d",ar[i]);
}
return 0;
}

//----------------------------------------------------------------------------------------

// Enter array any size and reverse each element of array and store it in same
location

original array - 123 56 977 291 526


after revere - 321 65 779 192 625
int main()
{
int x[10];
int i,no,rev;

printf("\n Enter any 10 nos: ");


for(i=0;i<10;i++)
{
scanf("%d",&x[i]);
}
printf("\n Original array elements are: ");
for(i=0;i<10;i++)
{
printf("%5d",x[i]);
}
// process
for(i=0;i<10;i++)
{
rev=0;
while(x[i])
{
rev=(rev*10)+(x[i]%10);
x[i]=x[i]/10;
}
x[i]=rev;
}
printf("\n After Reverse elements are: ");
for(i=0;i<10;i++)
{
printf("%5d",x[i]);
}
}

//----------------------------------------------------------------------------------------------------

// WAP to count rep. of a no in an array.

array --> 12 546 12 67 89 12 45 90 34


no -----> 12
output: 12 present 3 times in an array.

int main()
{
int x[10];
int i,no,cnt;

printf("\n Enter any 10 nos: ");


for(i=0;i<10;i++)
{
scanf("%d",&x[i]);
}

printf("\n Enter the no to be searched: ");


scanf("%d",&no);

cnt=0;
i=0;
while(i<10)
{
if(x[i]==no)
{
cnt++;
}
i++;
}
printf("\n %d is present %d times",no,cnt);
return 0;
}

//------------------------------------------------------------------------------------

// WAP to display the rep. of each element from array.

// 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;

printf("\n Enter the elements: ");


for(i=0;i<10;i++)
{
scanf("%d",&x[i]);
}

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;
}

//------------------------------------------------------------------------------------------

// WAP to find the min and max from array

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];
}
}

printf("\n Max: %d \t Min: %d",max,min);


return 0;
}

You might also like