Skip to document

Cprogram - Sample solved C programs-largest number,adam number etc..

Sample solved C programs-largest number,adam number etc..
Course

C programming (CP6G9A)

81 Documents
Students shared 81 documents in this course
Academic year: 2014/2015
Uploaded by:
0followers
30Uploads
8upvotes

Comments

Please sign in or register to post comments.

Preview text

Q1. C program to Find the largest number among three numbers

#include <stdio> int main() { double n1, n2, n3; printf("Enter three different numbers: "); scanf("%lf %lf %lf", &n1, &n2, &n3); // if n1 is greater than both n2 and n3, n1 is the largest if (n1 >= n2 && n1 >= n3) printf("% is the largest number.", n1); // if n2 is greater than both n1 and n3, n2 is the largest if (n2 >= n1 && n2 >= n3) printf("% is the largest number.", n2); // if n3 is greater than both n1 and n2, n3 is the largest if (n3 >= n1 && n3 >= n2) printf("% is the largest number.", n3); return 0; }

Output Enter three different numbers: 5 3 10 10 is the largest number

Q2 program to enter week number and print day name of week using switch

#include <stdio> int main() { unsigned int week; //Ask user to input week number printf("Enter week number (1-7): "); scanf("%u", &week); switch (week) { case 1: printf("Monday"); break; case 2: printf("Tuesday"); break; case 3: printf("Wednesday"); break; case 4: printf("Thursday"); break; case 5: printf("Friday"); break; case 6: printf("Saturday"); break; case 7: printf("Sunday"); break; default: printf("\n Please enter Valid Number between 1 to 7"); } return 0; }

Output

Enter week number (1-7): 5 Friday

temp=temp/10; } return t; } Output

Enter any number = 11 11 is an ADAM NUMNER

Q4. Write C program to check whether a number is palindrome or not. /* Palindrome number is such number which when reversed is equal to the original number.*/ #include <stdio> int main() { int n, num, rev = 0;

/* Input a number from user / printf("Enter any number to check palindrome: "); scanf("%d", &n); / Copy original value to 'num' to 'n'/ num = n; / Find reverse of n and store in rev / while(n != 0) { rev = (rev * 10) + (n % 10); n /= 10; } / Check if reverse is equal to 'num' or not */ if(rev == num) { printf("%d is palindrome.", num); } else { printf("%d is not palindrome.", num); } return 0; }

Output Enter any number to check palindrome: 121 121 is palindrome

{

printf(" %d ",prime[i]);

}

return 0;

}

Output Enter Limit : 6 Enter 6 numbers in an array : 12 23 45 85 449 27 Prime numbers in an above array : 23 449

Q6. Write C program to check whether number is prime or not. #include <stdio> int main() { int n, i, flag = 0; printf("Enter a positive integer: "); scanf("%d", &n); // 0 and 1 are not prime numbers // change flag to 1 for non-prime number if (n == 0 || n == 1) flag = 1; for (i = 2; i <= n / 2; ++i) { // if n is divisible by i, then n is not prime // change flag to 1 for non-prime number if (n % i == 0) { flag = 1; break; } } // flag is 0 for prime numbers if (flag == 0) printf("%d is a prime number.", n); else printf("%d is not a prime number.", n); return 0; }

Was this document helpful?

Cprogram - Sample solved C programs-largest number,adam number etc..

Course: C programming (CP6G9A)

81 Documents
Students shared 81 documents in this course
Was this document helpful?
Q1. C program to Find the largest number among three numbers
#include <stdio.h>
int main()
{
double n1, n2, n3;
printf("Enter three different numbers: ");
scanf("%lf %lf %lf", &n1, &n2, &n3);
// if n1 is greater than both n2 and n3, n1 is the largest
if (n1 >= n2 && n1 >= n3)
printf("%.2f is the largest number.", n1);
// if n2 is greater than both n1 and n3, n2 is the largest
if (n2 >= n1 && n2 >= n3)
printf("%.2f is the largest number.", n2);
// if n3 is greater than both n1 and n2, n3 is the largest
if (n3 >= n1 && n3 >= n2)
printf("%.2f is the largest number.", n3);
return 0;
}
Output
Enter three different numbers: 5 3 10
10 is the largest number