0% found this document useful (0 votes)
10 views6 pages

PGMM

The document contains various C programming examples, including finding the sum of non-negative numbers, checking for palindromes, determining leap years, calculating the roots of quadratic equations, summing digits, and generating Fibonacci sequences. It also includes programs for checking prime numbers and converting temperatures between Fahrenheit and Celsius. Each example is accompanied by code snippets and sample outputs.

Uploaded by

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

PGMM

The document contains various C programming examples, including finding the sum of non-negative numbers, checking for palindromes, determining leap years, calculating the roots of quadratic equations, summing digits, and generating Fibonacci sequences. It also includes programs for checking prime numbers and converting temperatures between Fahrenheit and Celsius. Each example is accompanied by code snippets and sample outputs.

Uploaded by

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

1)Illustrate aboutthe various data types in ‘C’ and write a C program to find the sum of

10 non-negative numbers entered by the user.


1a.#include<stdio.h>
int main()
{
int n,i,num,sum=0;
printf("Enter number of numbers to sum:");
scanf("%d",&n);
i=0;
printf("Enter %d number to sum:",n);
while(i<n)
{
scanf("%d",&num);
if(num>=0)
{
sum=sum+num;
i++;
}

}
printf("The sum is : %d",sum);
return 0;
}

1b.int main(){
int total;
int i;
int positiveSum = 0;
int negativeSum = 0;
printf("How many numbers you want to add : ");
scanf("%d",&total);
int numbers[total];
for(i=0; i<total; i++){
printf("Enter number %d : ",(i+1));
scanf("%d",&numbers[i]);
}
for(i=0 ; i<total ; i++){
if(numbers[i] < 0){
negativeSum += numbers[i];
}else{
positiveSum += numbers[i];
}
}
printf("You have entered : \n");
for(i=0 ; i<total; i++){
printf("%d ",numbers[i]);
}
printf("\nPositive numbers sum : %d",positiveSum);
printf("\nNegative numbers sum : %d\n",negativeSum);
}
2. Write a C program to check the integer is Palindrome or not.
#include <stdio.h>
int main()
{
int n, reversedInteger = 0, remainder, originalInteger;
printf("Enter an integer: ");
scanf("%d", &n);
originalInteger = n;
// reversed integer is stored in variable
while( n!=0 )
{
remainder = n%10;
reversedInteger = reversedInteger*10 + remainder;
n /= 10;
}
// palindrome if orignalInteger and reversedInteger are equal
if (originalInteger == reversedInteger)
printf("%d is a palindrome.", originalInteger);
else
printf("%d is not a palindrome.", originalInteger);

return 0;
}
Output:52325
Enter an integer: 1001
1001 is a palindrome.

3. Write a C program for the following :


(i). To check whether a given year is leap or not.(5)
(ii).To find the roots of a quadratic equation.(8

(i). To check whether a given year is leap or not.(5)

#include <stdio.h>

int main()
{
int year;

/* Input year from user */


printf("Enter year : ");
scanf("%d", &year);

/*
* If year is exactly divisible by 4 and year is not divisible by 100
* or year is exactly divisible by 400 then
* the year is leap year.
* Else year is normal year
*/
if(((year % 4 == 0) && (year % 100 !=0)) || (year % 400==0))
{
printf("LEAP YEAR");
}
else
{
printf("COMMON YEAR");
}

return 0;
}
Enter year : 2004
LEAP YEAR

(ii).To find the roots of a quadratic equation.

Write a C program for the following :


(i). To find the sum of the digits of a number. (123 => 1+2+3=6.(7)
(ii). To find the sum of all odd / even numbers between 1 and 100.(6)

(i). To find the sum of the digits of a number.


#include<stdio.h>
int main()
{
int n,sum=0,m;
printf("Enter a number:");
scanf("%d",&n);
while(n>0)
{
m=n%10;
sum=sum+m;
n=n/10;
}
printf("Sum is=%d",sum);
return 0;
}

(ii). To find the sum of all odd / even numbers between 1 and 100
#include <stdio.h>
void main()
{

int i, num, odd_sum = 0, even_sum = 0;


printf("Enter the value of num\n");
scanf("%d", &num);
for (i = 1; i <= num; i++)
{

if (i % 2 == 0)
even_sum = even_sum + i;
else
odd_sum = odd_sum + i;
}
printf("Sum of all odd numbers = %d\n", odd_sum);
printf("Sum of all even numbers = %d\n", even_sum);
}
Output:
Case 1:
Enter the value of num
10
Sum of all odd numbers = 25
Sum of all even numbers = 30

Case 2:
Enter the value of num
100
Sum of all odd numbers = 2500
Sum of all even numbers = 2550

4.Write a C program for the following :


(i)To generate the first n numbers in a Fibonacci series.(7)

PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
int f0,f1,f,n,i;
clrscr();
printf("ENTER THE VALUE FOR n \n");
scanf("%d",&n);
f0=0;
f1=1;
printf("FIBONACCI SEQUENCE FOR THE FIRST %d TERMS:\n",n);
i=0;
while(i<n)
{
printf("%d\t",f0);
f=f0+f1;
f0=f1;
f1=f;
i=i+1;
}
}
INPUT:
ENTER THE VALUE FOR n
10
OUTPUT:
FIBONACCI SEQUENCE FOR THE FIRST 10 TERMS:
0 1 1 2 3 5 8 13 21 34

(ii). To find the factorial of a given number.(6)


int main()
{
int n, i;
unsigned long long factorial = 1;
printf("Enter an integer: ");
scanf("%d",&n);
// show error if the user enters a negative integer
if (n < 0)
printf("Error! Factorial of a negative number doesn't exist.");
else
{
for(i=1; i<=n; ++i)
{
factorial *= i; // factorial = factorial*i;
}
printf("Factorial of %d = %llu", n, factorial);
}
return 0;
}

Enter an integer: 10

Factorial of 10 = 3628800

Develop a C program for the following :


(i) To check whether a number is prime

(ii)To convert the temperature given in Fahrenheit to Celsius and vice versa
/* C Program to convert temperature from Fahrenheit to Celsius and vice versa.*/
#include <stdio.h>
int main()
{

float fh,cl;
int choice;

printf("\n1: Convert temperature from Fahrenheit to Celsius.");


printf("\n2: Convert temperature from Celsius to Fahrenheit.");
printf("\nEnter your choice (1, 2): ");
scanf("%d",&choice);

if(choice ==1){
printf("\nEnter temperature in Fahrenheit: ");
scanf("%f",&fh);
cl= (fh - 32) / 1.8;
printf("Temperature in Celsius: %.2f",cl);
}
else if(choice==2){
printf("\nEnter temperature in Celsius: ");
scanf("%f",&cl);
fh= (cl*1.8)+32;
printf("Temperature in Fahrenheit: %.2f",fh);
}
else{
printf("\nInvalid Choice !!!");
}
return 0;
}

Output

First Run:
1: Convert temperature from Fahrenheit to Celsius.
2: Convert temperature from Celsius to Fahrenheit.
Enter your choice (1, 2): 1

Enter temperature in Fahrenheit: 98.6


Temperature in Celsius: 37.00

Second Run:
1: Convert temperature from Fahrenheit to Celsius.
2: Convert temperature from Celsius to Fahrenheit.
Enter your choice (1, 2): 2

Enter temperature in Celsius: 37.0


Temperature in Fahrenheit: 98.60

Third Run:
1: Convert temperature from Fahrenheit to Celsius.
2: Convert temperature from Celsius to Fahrenheit.
Enter your choice (1, 2): 3

Invalid Choice !!!

You might also like