Sie sind auf Seite 1von 17

Swap of two number

#include<stdio.h>
main()
{
int n1,n2,temp;
n1=10;
n2=15;
printf("Before swap value of n1 and n2=%d,%d",n1,n2);
//swap two value
temp=n1;
n1=n2;
n2=temp;
printf("\n After swap value of n1 and n2=%d,%d",n1,n2);
}

Find sum and average of array elements


#include<stdio.h>
main()
{
int array[5];
int index;
int sum=0;
float avg=0;
for(index=0;index<5;index++)
{
printf("index[%d]=",index);
scanf("%d",&array[index]);
}
printf("Array elememts are:\n");
for(index=0;index<5;index++)
{
printf("%d\t",array[index]);
sum=sum+array[index];
}
avg=(float)sum/5;
printf("sum is=%d,Average value=%.2f",sum,avg);
}

Search elements from array


#include <stdio.h>

void main()
{
int array[20];
int i, low, mid, high, key, size;
printf("Enter the size of an array\n");
scanf("%d", &size);
printf("Enter the array elements\n");
for (i = 0; i < size; i++)
{
scanf("%d", &array[i]);
}

printf("Enter the key\n");


scanf("%d", &key);
low = 0;
high = (size - 1);
while (low <= high)
{
mid = (low + high) / 2;
if (key == array[mid])
{
printf("SUCCESSFUL SEARCH\n");
}
if (key < array[mid])
{
high = mid - 1;
}

}
}
//Decimal to binary conversion using function

#include<math.h>
#include<conio.h>
long tobinary(int n)
{
int i=0,r;
long b=0;

while(n)
{
r=n%2;
n=n/2;
b=(long)((pow(10,i)*r))+b;
i++;
}
return b;
}
int main()
{
int n;
printf("Enter a number : ");
scanf("%d",&n);
printf("Binary of %d is %ld",n,tobinary(n));
getch();
}

String operation
#include<stdio.h>
#include<string.h>
int main()
{
char str[100],temp[100];
printf("Enter a string : ");
gets(str);
printf("Length of %s is %d\n",str,strlen(str));
strcpy(temp,str);
printf("Reverse of %s is %s\n",str,strrev(temp));
strcpy(temp,str);
printf("Upper case of %s is %s\n",str,strupr(temp));
printf("Lower case of %s is %s\n",str,strlwr(temp));
getch();
}
Prime number
#include<stdio.h>

int main()
{
int n,d,flag;
printf("Enter a number : ");
scanf("%d",&n);

d=2;flag=0;
while(d<n)
{
if(n%d==0)
{
flag=1;
break;
}
d++;
}

if(flag)
printf("%d is not a prime number",n);
else
printf("%d is a prime number",n);
getch();
}

Print ascii codes of all character


#include<stdio.h>
#include<conio.h>
int main()
{
int i;
for(i=0;i<=255;i++)
printf("%d = %c\t",i,i);
getch();
}

/*
WAP to get a number and
check the number to be palindrome.
*/
#include<stdio.h>

int main()
{
int n,m,rev=0,d;
printf("Enter a number : ");
scanf("%d",&n);
m=n;
while(n)
{
d=n%10;
rev=rev*10+d;
n=n/10;
}
if(m==rev)
printf("%d is a palindrome",m);
else
printf("%d is not a palindrome",m);

getch();

}
/*
Write a program to check as number to be
Armstrong number
*/
int main()
{
int n,m,d,sum=0;
printf("Enter a number : ");
scanf("%d",&n);
m=n;
while(n)
{
d=n%10;
sum=sum+d*d*d;
n=n/10;
}
if(m==sum)
printf("%d is an Armstrong Number",m);
else
printf("%d is not an Armstrong Number",m);

getch();
}

/*
Write a program to get a number and
print sum of its digits using while loop
*/
#include<stdio.h>
#include<conio.h>
int main()
{
int num,d,sum=0;
printf("Enter a number : ");
scanf("%d",&num);

while(num)
{
d=num%10;
sum=sum+d;
num=num/10;
}
printf("Sum of digits is : %d",sum);
getch();
}

Solution 1: Factorial of a number

int factorial(int n)
{
if(n==2)
return 2;
else
return n*factorial(n-1);
}
int main()
{
int n;
printf("Enter a number : ");
scanf("%d",&n);
printf("Factorial of %d is %d",n,factorial(n));
getch();
}

Solution 2: Fibbonacci Series

int fib(int n)
{
if(n==1)
return 0;
else if(n==2)
return 1;
else
return fib(n-1)+fib(n-2);
}
int main()
{
int num,i;
printf("Numbers in series : ");
scanf("%d",&num);
for(i=1;i<=num;i++)
printf("%d ",fib(i));
getch();
}

Solution 3: Pascal Triangle

int pt(int r, int c)


{
if(c==1)
return 1;
else if(r==c)
return 1;
else
return pt(r-1,c)+pt(r-1,c-1);
}
int main()
{
int num,r,c;
printf("Enter rows in triangle : ");
scanf("%d",&num);
for(r=1;r<=num;r++)
{
for(c=1;c<=r;c++)
printf("%d ",pt(r,c));
printf("\n");
}
getch();
}

Solution 4: GCD of three numbers

int gcd(int a,int b)


{
if(a==b)
return a;
else if(a>b)
return gcd(a-b,b);
else if(b>a)
return gcd(a,b-a);
}
int main()
{
int a,b,c;
printf("Enter three numbers : ");
scanf("%d%d%d",&a,&b,&c);
printf("GCD is %d",gcd(a,gcd(b,c)));
getch();
}

Q 8.20: Can we call main() function recursively?

yes, we can call main() function within the main() function.

For example, print factorial of a 6 using recusive use of main()


function.
int main()
{
static int i=1,f=1;
f= f * i;
if(i<6)
{
i++;
main();
}
else
{
printf("Factorial of 6 is %d",f);
getch();
}
}
Write a program to input 10 numbers and print then in
ascending order.

#define N 10
int main()
{
int ar[N],i,j,temp;
printf("Enter %d numbers : ",N);
for(i=0;i<N;i++)
scanf("%d",&ar[i]);

for(i=0;i<N;i++)
for(j=i+1;j<N;j++)
{
if(ar[i]>ar[j])
{
temp=ar[i];
ar[i]=ar[j];
ar[j]=temp;
}
}
printf("Sorted numbers are : ");
for(i=0;i<N;i++)
printf("%d ",ar[i]);
getch();
}

Write a program input a string and reverse print it?

Hint: First look the NULL character \0 then start printing from the
backside to print reverse of a string. Use putchar() function to write
a character o screen rather than printf("%c")

int main()
{
char str[100]; int i=0;
printf("Enter a string : "); gets(str);
while(str[i]!='\0')
{
i++;
}
printf("Reverse of %s is : ",str);
for(i=i-1;i>=0;i--)
putchar(str[i]);
getch();
}
Java script program-Calculator
<html>
<head>
<title>Web App for Simple Interest Calculation</title>
<script>
function calculate()
{
p = document.getElementById("p").value;
n = document.getElementById("n").value;
r = document.getElementById("r").value;
result = document.getElementById("result");
result.innerHTML = "The interest is " + (p*n*r/100);
}
</script>
</head>
<body>
<h1>Simple Interest Calculation</h1>
Amount: <input id="p"><br/>
Rate: <input id="r"><br/>
No. of Years: <input id="n"><br/>
<button onclick="calculate()">Calculate</button>
<p id="result"></p>
</body>
</html>
Load image
<!DOCTYPE html>
<html>
<body>
<img src="w3html.gif" onload="loadImage()" width="100"
height="132">

<script>
function loadImage() {
alert("Image is loaded");
}
</script>

</body>
</html>
Viva Voice
1. local variable
2. global variable
3. subscript variable/array variable
4. reference type variable
5. keyword
6. constants
7. infinite loops
8. Array advantage
9. Pointer
10. Function
11. Html basic tags
12. Java script features
13. Tcp/ip model
14. Router and bridge
15. Network cable
16. Lan and wan

Das könnte Ihnen auch gefallen