Home > Uncategorized > CP Imported Program

CP Imported Program

Program to swap two numbers using functions.

#include<stdio.h>
#include<conio.h>

void main()
{
void swap(int,int);
int a,b,r;
clrscr();
printf(“enter value for a&b: “);
scanf(“%d%d”,&a,&b);
swap(a,b);
getch();
}
void swap(int a,int b)
{
int temp;
temp=a;
a=b;
b=temp;
printf(“after swapping the value for a & b is : %d %d”,a,b);
}

Program to show call by value

#include<stdio.h>
#include<conio.h>

void main()
{
int a,b,swap();
clrscr();
a=5;
b=10;
printf(“value of a=%d & value of b=%d before swap “,a,b);
swap(a,b);
printf(“\nvalue of a =%d & b=%d after swap”,a,b);
getch();
}
int swap(int x,int y)
{
int temp;
temp=x;
x=y;
y=temp;
}

Program to show call by reference.

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,*aa,*bb,swap();
clrscr();
a=5;
b=10;
aa=&a;
bb=&b;
printf(“value of a= %d & value of b=%d before swap”,a,b);
swap(aa,bb);
printf(“\nvalue of a=%d & b=%d after swap”,a,b);
getch();
}
int swap(int *x,int *y)
{
int temp;
temp=*x;
*x=*y;
*y=temp;
}

Program to find largest of two numbers using functions

#include<stdio.h>
#include<conio.h>

void main()
{
void max();
clrscr();
max();
getch();
}
void max()
{
int a[5],max,n,i;
printf(“How many no’s you want to enter: “);
scanf(“%d”,&n);
printf(“Enter element for the array: “);
for(i=0;i<n;i++)
scanf(“%d”,&a[i]);
max=a[0];
for(i=1;i<5;i++)
{
if(max<a[i])
max=a[i];
}
printf(“maximum no= %d”,max);
}

Program to find factorial of a number using recursion

#include<stdio.h>
#include<conio.h>

void main()
{
int n;
clrscr();
printf(“enter number: “);
scanf(“%d”,&n);
if(n<0)
printf(“invalid number”);
else
printf(“%d!=%d”,n,fact(n));
getch();

}
int fact(int x)
{
if(x==0)
return 1;
else
return(x*fact(x-1));
}

Program to find whether a string is palindrome or not

#include<stdio.h>
#include<conio.h>

void main()
{
char s1[20],s2[20];
clrscr();
printf(“enter a string: “);
scanf(“%s”,s1);
strcpy(s2,s1);
strrev(s2);
if(strcmp(s1,s2)==0)
printf(“string is a palindrome”);
else
printf(“not a palindrome string”);
getch();
}

Program to calculate sum of 5 subjects & find percentage.

#include<stdio.h>
#include<conio.h>

void main()
{
int s1,s2,s3,s4,s5,sum,total=500;
float per;
clrscr();
printf(“enter marks of 5 subjects: “);
scanf(“%d%d%d%d%d”,&s1,&s2,&s3,&s4,&s5);
sum=s1+s2+s3+s4+s5;
printf(“sum=%d”,sum);
per=(sum*100)/total;
printf(“percentage=%f”,per);
getch();
}

Program to reverse a given number

#include<stdio.h>
#include<conio.h>

void main()
{
int n,a,r=0;
clrscr();
printf(“enter any no to get its reverse: “);
scanf(“%d”,&n);
while(n>=1)
{
a=n%10;
r=r*10+a;
n=n/10;
}
printf(“reverse=%d”,r);
getch();
}

Write a C program to find the maximum value of given three numbers.

#include<stdio.h>
#include<conio.h>

void main()
{
int a,b,c;
clrscr();
printf(“enter value of a, b & c: “);
scanf(“%d%d%d”,&a,&b,&c);
if((a>b)&&(a>c))
printf(“a is greatest”);
if((b>c)&&(b>a))
printf(“b is greatest”);
if((c>a)&&(c>b))
printf(“c is greatest”);
getch();
}

Program to find whether given no is even or odd.

#include<stdio.h>
#include<conio.h>

void main()
{
int n;
clrscr();
printf(“enter any no: “);
scanf(“%d”,&n);
if(n%2==0)
printf(“no is even”);
else
printf(“no is odd”);
getch();
}

Program to display arithmetic operator using switch case.

#include<stdio.h>
#include<conio.h>

void main()
{
int a,b,n,s,m,su,d;
clrscr();
printf(“enter two no’s : “);
scanf(“%d%d”,&a,&b);
printf(“enter 1 for sum\n2 for multiply\n3for subtraction\n4 for division: “);
scanf(“%d”,&n);
switch(n)
{
case 1:
s=a+b;
printf(“sum=%d”,s);
break;
case 2:
m=a*b;
printf(“multiply=%d”,m);
break;
case 3:
su=a-b;
printf(“subtraction=%d”,su);
break;
case 4:
d=a/b;
printf(“divission=%d”,d);
break;
default:
printf(“wrong input”);
break;
}
getch();
}

Program to print Fibonacci series up to 100.

#include<stdio.h>
#include<conio.h>

void main()
{
int a=1,b=1,c=0,i;
clrscr();
printf(“%d\t%d\t”,a,b);
for(i=0;i<=10;i++)
{
c=a+b;
if(c<100)
{
printf(“%d\t”,c);
}
a=b;
b=c;
}
getch();
}

Program to find factorial of a number.

#include<stdio.h>
#include<conio.h>

void main()
{
int n,i,fact=1;
clrscr();
printf(“Enter any no: “);
scanf(“%d”,&n);
for(i=n;i>=1;i–)
{
fact=fact*i;
}
printf(“Factorial=%d”,fact);
getch();
}

Program to find whether given no is a prime no or not

#include<stdio.h>
#include<conio.h>

void main()
{
int i,n,r=0;
clrscr();
printf(“Enter any no: “);
scanf(“%d”,&n);
for(i=2;i<=n-1;i++)
{
if(n%i==0)
r=1;
break;
}
if(r==0)
printf(“prime no”);
else
printf(“Not prime”);
getch();
}

Program to display sum of series 1+1/2+1/3+……….+1/n.

#include<stdio.h>
#include<conio.h>

void main()
{
int n,i,sum=0;
clrscr();
printf(“Enter any no: “);
scanf(“%d”,&n);
printf(“1″);
for(i=2;i<=n-1;i++)
printf(” 1/%d +”,i);
for(i=1;i<=n;i++)
sum=sum+i;
printf(” 1/%d”,n);
printf(“\nSum=1/%d”,sum+1/n);
getch();
}

Convert string to upper case and lower case

#include <ctype.h>
#include <stdio.h>

int main(void)
{

char str[80];
int i;

printf("Enter a string: ");
gets(str);

for( i = 0; str[ i ]; i++)
str[ i ] = toupper( str[ i ] );

printf("%s\n", str); /* uppercase string */

for(i = 0; str[ i ]; i++)
str[i] = tolower(str[ i ]);

printf("%s\n", str); /* lowercase string */

return 0;
}

Categories: Uncategorized
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: