Documentos de Académico
Documentos de Profesional
Documentos de Cultura
flag=1;
break;
}
}
if (flag==0)
printf("%d is a prime number.",n);
else
printf("%d is not a prime number.",n);
return 0;
}
9/* C program to check whether a year is leap year or not using if else statement.*/
#include <stdio.h>
int main(){
int year;
printf("Enter a year: ");
scanf("%d",&year);
if(year%4 == 0)
{
if( year%100 == 0) /* Checking for a century year */
{
if ( year%400 == 0)
printf("%d is a leap year.", year);
else
printf("%d is not a leap year.", year);
}
else
printf("%d is a leap year.", year );
}
else
printf("%d is not a leap year.", year);
return 0;
printf("Enter an integer\n");
scanf("%d",&n);
while(n != 0)
{
remainder = n % 10;
sum = sum + remainder;
n = n / 10;
}
printf("Sum of digits of entered number = %d\n",sum);
return 0;
}
15/* Source code to create a simple calculator for addition, subtraction, multiplication
and division using switch...case statement in C programming. */
# include <stdio.h>
int main()
{
char o;
float num1,num2;
printf("Enter operator either + or - or * or divide : ");
scanf("%c",&o);
printf("Enter two operands: ");
scanf("%f%f",&num1,&num2);
switch(o) {
case '+':
printf("%.1f + %.1f = %.1f",num1, num2, num1+num2);
break;
case '-':
printf("%.1f - %.1f = %.1f",num1, num2, num1-num2);
break;
case '*':
printf("%.1f * %.1f = %.1f",num1, num2, num1*num2);
break;
case '/':
printf("%.1f / %.1f = %.1f",num1, num2, num1/num2);
break;
default:
/* If operator is other than +, -, * or /, error message is shown */
printf("Error! operator is not correct");
break;
}
return 0;
}
17-
#include <stdio.h>
#include <math.h>
void main()
{
long int x,n,xpown;
long int power(int x, int n);
printf("Enter the values of X and N\n");
scanf("%ld %ld",&x,&n);
xpown = power (x,n);
printf(" %d to the power %d = %ld\n",x,n,xpown);
}
/*Recursive function to computer the X to power N*/
long int power(int x, int n)
{
if (n==1)
return(x);
else if ( n%2 == 0)
return (pow(power(x,n/2),2)); /*if n is even*/
else
return (x*power(x, n-1));
/* if n is odd*/
}
18/* Program to find roots of a quadratic equation when coefficients are entered by user.
*/
/* Library function sqrt() computes the square root. */
#include <stdio.h>
#include <math.h> /* This is needed to use sqrt() function.*/
int main()
{
float a, b, c, determinant, r1,r2, real, imag;
printf("Enter coefficients a, b and c: ");
scanf("%f%f%f",&a,&b,&c);
determinant=b*b-4*a*c;
if (determinant>0)
{
r1= (-b+sqrt(determinant))/(2*a);
r2= (-b-sqrt(determinant))/(2*a);
printf("Roots are: %.2f and %.2f",r1 , r2);
}
else if (determinant==0)
{
r1 = r2 = -b/(2*a);
printf("Roots are: %.2f and %.2f", r1, r2);
}
else
{
real= -b/(2*a);
imag = sqrt(-determinant)/(2*a);
printf("Roots are: %.2f+%.2fi and %.2f-%.2fi", real, imag, real, imag);
}
return 0;
}
19#include <stdio.h>
#include <stdlib.h>
struct complex
{
int real, img;
};
int main()
{
int choice, temp1, temp2, temp3;
struct complex a, b, c;
while(1)
{
printf("Press 1 to add two complex numbers.\n");
printf("Press 2 to subtract two complex numbers.\n");
printf("Press 3 to multiply two complex numbers.\n");
printf("Press 4 to divide two complex numbers.\n");
printf("Press 5 to exit.\n");
printf("Enter your choice\n");
scanf("%d",&choice);
if( choice == 5)
exit(0);
if(choice >= 1 && choice <= 4)
{
printf("Enter a and b where a + ib is the first complex number.");
printf("\na = ");
scanf("%d", &a.real);
printf("b = ");
scanf("%d", &a.img);
printf("Enter c and d where c + id is the second complex number.");
printf("\nc = ");
scanf("%d", &b.real);
printf("d = ");
scanf("%d", &b.img);
}
if ( choice == 1 )
{
c.real = a.real + b.real;
if ( temp2/temp3 >= 0)
printf("Division of two
%di",temp1,temp3,temp2/temp3);
else
printf("Division of two
%di",temp1,temp3,temp2/temp3);
}
else
{
if ( temp2/temp3 >= 0)
printf("Division of two
%di",temp1,temp3,temp2,temp3);
else
printf("Division of two
%di",temp1,temp3,temp2,temp3);
}
}
}
else
printf("Invalid choice.");
}
21#include<stdio.h>
int main(){
int a[50],size,i,big,small;
printf("\nEnter the size of the array: ");
scanf("%d",&size);
printf("\nEnter %d elements in to the array: ", size);
for(i=0;i<size;i++)
scanf("%d",&a[i]);
big=a[0];
for(i=1;i<size;i++){
if(big<a[i])
big=a[i];
}
printf("Largest element: %d",big);
small=a[0];
for(i=1;i<size;i++){
if(small>a[i])
small=a[i];
}
printf("Smallest element: %d",small);
return 0;
}
Sample Output:
Enter the size of the array: 4
Enter 4 elements in to the array: 2 7 8 1
Largest element: 8
Smallest element: 1
22#include<stdio.h>
#include<string.h>
int main() {
char str[100], temp;
int i, j = 0;
printf("\nEnter the string :");
gets(str);
i = 0;
j = strlen(str) - 1;
while (i < j) {
temp = str[i];
str[i] = str[j];
str[j] = temp;
i++;
j--;
}
23#include<conio.h>
#include<stdio.h>
void main()
{
char str[20];
int i;
clrscr();
printf("Enter any String ");
scanf("%s",str);
for(i=0;str[i]!='\0';i++)
{
if(str[i]>64&&str[i]<91)
// Uppercase characters
str[i]=str[i]+32;
// adding 32 to change to lowercase
else
if(str[i]>95&&str[i]<122)
// Lowercase characters
str[i]=str[i]-32;
// substracting 32 to change to Uppercase
}
printf("Here is the changed case: %s",str);
getch();
}
24#include <stdio.h>
int main()
{
int n, array[1000], c, d, t;
printf("Enter number of elements\n");
scanf("%d", &n);
printf("Enter %d integers\n", n);
for (c = 0; c < n; c++) {
scanf("%d", &array[c]);
}
for (c = 1 ; c <= n - 1; c++) {
d = c;
while ( d > 0 && array[d] < array[d-1]) {
t
= array[d];
array[d]
= array[d-1];
array[d-1] = t;
}
d--;
}
printf("Sorted list in ascending order:\n");
for (c = 0; c <= n - 1; c++) {
printf("%d\n", array[c]);
}
}
return 0;
25#include<stdio.h>
/* Function to calculate x raised to the power y */
int power(int x, unsigned int y)
{
if( y == 0)
return 1;
else if (y%2 == 0)
return power(x, y/2)*power(x, y/2);
else
return x*power(x, y/2)*power(x, y/2);
}
/* Program to test function power */
int main()
{
int x = 2;
unsigned int y = 3;
printf("%d", power(x, y));
getchar();
return 0;
}
26#include<stdio.h>
#include<string.h>
int main() {
char str1[100];
char str2[100];
char str3[100];
int len;
printf("\nEnter the String 1 : ");
gets(str1);
printf("\nEnter the String 2 : ");
gets(str2);
strcpy(str3, str1);
strcat(str3, str2);
printf("\nConcated String : %s", str3);
return (0);
}
20#include<iostream>
using namespace std;
void expand(int);
int main()
{
int num;
cout<<"Enter a number : ";
cin>>num;
expand(num);
}
void expand(int value)
{
const char * const ones[20] = {"zero", "one", "two", "three","four","five","six","seven",
"eight","nine", "eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen",
"eighteen","nineteen"};
const char * const tens[10] = {"", "ten", "twenty", "thirty","forty","fifty","sixty","seventy",
"eighty","ninety"};
if(value<0)
{
cout<<"minus ";
expand(-value);
}
else if(value>=1000)
{
expand(value/1000);
cout<<" thousand";
if(value % 1000)
{