0% found this document useful (0 votes)
3 views21 pages

Harshitha - Colaboratory

Uploaded by

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

Harshitha - Colaboratory

Uploaded by

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

11/28/23, 10:01 PM Harshitha - Colaboratory

Check whether the number is a perfect number or not

#include<stdio.h>

int main()
{
int i, num, sum = 0;

printf("enter a number");
scanf("%d", &num);

for(i=1; i<num; i++)


{
if(num%i == 0)
{
sum += i;
}
}

if(sum == num)
{
printf("perfect number\n");
}
else
{
printf("not a perfect number\n");
}

return 0;
}

C program to print fibonacci series upto n terms

https://colab.research.google.com/drive/1nlx9Wn19vjbGmzGJQZzyhZrPvB_ri1rp#scrollTo=JVen_neFwlKb&printMode=true 1/21
11/28/23, 10:01 PM Harshitha - Colaboratory

#include <stdio.h>
int main() {

int i, n;

int t1 = 0, t2 = 1;
int nextTerm = t1 + t2;

scanf("%d", &n);
printf("%d, %d, ", t1, t2);

for (i = 3; i <= n; ++i) {


printf("%d, ", nextTerm);
t1 = t2;
t2 = nextTerm;
nextTerm = t1 + t2;
}

return 0;
}

Write a C program to find Greatest Common Divisor (GCD) of given two numbers

#include<stdio.h>

int main()
{
int n1, n2, gcd = 1;
scanf("%d%d",&n1,&n2);
for(int i = 1; i <= n1 || i <= n2; i++) {
if(n1 % i == 0 && n2 % i == 0)
gcd = i;
}

printf("%d",gcd);

return 0;
}

Check whether number is a palindrome or not

https://colab.research.google.com/drive/1nlx9Wn19vjbGmzGJQZzyhZrPvB_ri1rp#scrollTo=JVen_neFwlKb&printMode=true 2/21
11/28/23, 10:01 PM Harshitha - Colaboratory

#include <stdio.h>

int main() {
int n, n1, rev = 0, rem;

printf("enter a number ");


scanf("%d", &n);
n1 = n;

while (n > 0){


rem = n % 10;
rev = rev * 10 + rem;
n = n / 10;
}

if (n1 == rev){
printf("palindrome");
}
else{
printf("not palindrome");
}
return 0;
}

C program to check entered character is a vowel or a consonant . (Declare a character datatype


c and check that value whether it is the vowel or not)

#include <stdio.h>
int main() {
char c;
int lowercase_vowel, uppercase_vowel;
scanf("%c", &c);

lowercase_vowel = (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u');

uppercase_vowel = (c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U');

if (lowercase_vowel || uppercase_vowel)
printf("vowel");
else
printf("consonant");
return 0;
}

C program to find all factors of a number

https://colab.research.google.com/drive/1nlx9Wn19vjbGmzGJQZzyhZrPvB_ri1rp#scrollTo=JVen_neFwlKb&printMode=true 3/21
11/28/23, 10:01 PM Harshitha - Colaboratory

#include <stdio.h>
int main() {
int num, i;
scanf("%d", &num);
for (i = 1; i <= num; ++i) {
if (num % i == 0) {
printf("%d, ", i);
}
}
return 0;
}

Write a c program to find factorial of a number

#include<stdio.h>

int main(){
int a,b=1;

scanf("%d",&a);
for(int i = 1; i<=a;i++)
{
b=b*i;
}
printf("%d",b);
return 0;
}

Write a C program to find whether a given number is a prime or not.

https://colab.research.google.com/drive/1nlx9Wn19vjbGmzGJQZzyhZrPvB_ri1rp#scrollTo=JVen_neFwlKb&printMode=true 4/21
11/28/23, 10:01 PM Harshitha - Colaboratory

#include <stdio.h>

int main() {

int n, i, flag = 0;
scanf("%d", &n);

if (n == 0 || n == 1)
flag = 1;

for (i = 2; i <= n / 2; ++i) {

if (n % i == 0) {
flag = 1;
break;
}
}

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

return 0;
}

Find the sum of digits in a three-digit number.

https://colab.research.google.com/drive/1nlx9Wn19vjbGmzGJQZzyhZrPvB_ri1rp#scrollTo=JVen_neFwlKb&printMode=true 5/21
11/28/23, 10:01 PM Harshitha - Colaboratory

#include <stdio.h>
int main()
{
int n, t, sum = 0, remainder;

printf("To find the sum of digits in a three-digit number.\n");


printf("Enter the three-digit number: ");
scanf("%d", &n);

t = n;
if (n>=100 && n<1000){
while (t != 0)
{
remainder = t % 10;
sum = sum + remainder;
t = t / 10;
}
printf("The sum of the digits of %d is %d.\n", n, sum);
}
else{
printf("The number %d is not a three-digit number.", t);
}

return 0;
}

Write a C program to print your roll no, date of birth, and mobile number.

#include <stdio.h>

int main(){
int roll, date, month, year;
long int mobile;
scanf("%d%d-%d-%d%ld", &roll, &date, &month, &year, &mobile);
printf("Roll no: %d\n",roll);
printf("Date of Birth: %d-%d-%d\n",date,month,year);
printf("Mobile no: %ld\n", mobile);
return 0;
}

Write a C Program to get Half Pyramid of Numbers

https://colab.research.google.com/drive/1nlx9Wn19vjbGmzGJQZzyhZrPvB_ri1rp#scrollTo=JVen_neFwlKb&printMode=true 6/21
11/28/23, 10:01 PM Harshitha - Colaboratory

#include <stdio.h>

int main (){


int n,i,j;
printf("Enter the number of rows: ");
scanf("%d",&n);
for(i=1;i<=n;i++){
for(j=1;j<=i;j++){
printf("%d ",j);
}
printf("\n");
}

Write a C program to find whether a given number is a prime or not.

#include <stdio.h>

int main()
{
int a,b,flag;
scanf("%d%d",&a,&b);
while(a<b){
flag=0;
if (a<=1){
++a;
continue;
}
for (int i=2;i<=a/2;++i){
if (a%i==0){
flag =1 ;
break;
}
}
if(flag==0)
printf("%d ", a);
a++;
}
return 0;
}

Write a C Program to get Half Pyramid of Alphabets

https://colab.research.google.com/drive/1nlx9Wn19vjbGmzGJQZzyhZrPvB_ri1rp#scrollTo=JVen_neFwlKb&printMode=true 7/21
11/28/23, 10:01 PM Harshitha - Colaboratory

#include <stdio.h>
int main() {
int i, j;
char input, alphabet = 'A';
printf("Enter an uppercase character you want to print in the last row: ");
scanf("%c", &input);
for (i = 1; i <= (input - 'A' + 1); ++i) {
for (j = 1; j <= i; ++j) {
printf("%c ", alphabet);
}
++alphabet;
printf("\n");
}
return 0;
}

Write a C program to calculate the distance between the two points (x1,y1) and (x2, y2).

#include <stdio.h>
#include <math.h>

int main() {
int x1, y1, x2, y2;
float gd;
scanf("%d", &x1);
scanf("%d", &y1);
scanf("%d", &x2);
scanf("%d", &y2);
gd= sqrt(pow((x2-x1),2)+(pow((y2-y1),2)));
printf("The distance between two points (%d,%d) and (%d,%d) is %.2f", x1,y1,x2,y2,gd)
printf("\n");
return 0 ;
}

Write a C Program to convert decimal number to binary

https://colab.research.google.com/drive/1nlx9Wn19vjbGmzGJQZzyhZrPvB_ri1rp#scrollTo=JVen_neFwlKb&printMode=true 8/21
11/28/23, 10:01 PM Harshitha - Colaboratory

#include <stdio.h>
#include <math.h>

long long convert(int);


int main() {
int n;
long long bin;
printf("Enter a decimal number: ");
scanf("%d", &n);
bin = convert(n);
printf("%d in decimal = %lld in binary", n, bin);
return 0;
}
long long convert(int n) {
long long bin = 0;
int rem, i = 1;
while (n != 0) {
rem = n % 2;
n /= 2;
bin += rem * i;
i *= 10;
}

return bin;
}

Write a C program to print a given number for a given number of times using for loop.

#include <stdio.h>

int main (){


int a,n,i;
scanf("%d%d",&a,&n);
if (n>=3){
for(i=1;i<=n;i++){
printf("%d\n",a);
}
}
}

Write a C program to display the ASCII value of a given character

#include <stdio.h>

int main (){


char a;
scanf("%c", &a);
printf("%d", a);
}

https://colab.research.google.com/drive/1nlx9Wn19vjbGmzGJQZzyhZrPvB_ri1rp#scrollTo=JVen_neFwlKb&printMode=true 9/21
11/28/23, 10:01 PM Harshitha - Colaboratory

Write a program in C to display the cube of the number up to given an integer.

#include <stdio.h>

int main (){


int a,i;
scanf("%d",&a);
for(i=1;i<=a;i++){
printf("%d\n",i*i*i);
}
}

C program to convert upper-case character to a lower-case character

#include<stdio.h>
int main(){
char c;
char lower,upper;
scanf("%c",&c);
if(c>=65&&c<97)
{
lower=c+32;
printf("%c", lower);
}
else
{
upper=c-32;
printf("%c",upper);
}
return 0;
}

Write a C program to input value of 'N' and find sum of all odd numbers 1 to 'N'

https://colab.research.google.com/drive/1nlx9Wn19vjbGmzGJQZzyhZrPvB_ri1rp#scrollTo=JVen_neFwlKb&printMode=true 10/21
11/28/23, 10:01 PM Harshitha - Colaboratory

#include <stdio.h>

int main()
{
int i, n, sum=0;

scanf("%d", &n);

for(i=1; i<=n; i+=2)


{
sum += i;
}

printf("%d", sum);

return 0;
}

Write a C Program to Print even and odd numbers in a given range using functions

#include<stdio.h>
int main()
{
int i,n,l,even=0,odd=0;
printf("enter lower limit and upper limit values to display even and odd numbers");
scanf("%d%d",&n, &l);
printf("\neven numbers are");
for(i=n;i<=l;i++)
{
if(i%2==0)
{
printf("\n%d",i);
even++;
}
}
printf("\n\nodd numbers are");
for(i=n;i<=l;i++)
{
if(i%2==1)
{
printf("\n%d",i);
odd++;
}
}
return 0;
}

Write a C program that accepts three items’ weights (floating points' values ) and a number of
purchases (floating points' values) and calculate the average value of the items.

https://colab.research.google.com/drive/1nlx9Wn19vjbGmzGJQZzyhZrPvB_ri1rp#scrollTo=JVen_neFwlKb&printMode=true 11/21
11/28/23, 10:01 PM Harshitha - Colaboratory

#include <stdio.h>

int main() {
float num1, num2,num3,num4, average;
scanf("%f%f%f%f", &num1,&num2,&num3,&num4);

average = ((num1)*(num2) + (num3)*(num4)) / (num2 + num4);

printf("Average Value = %0.2f", average);

return 0;
}

Write a C program to check whether the triangle is equilateral, isosceles or scalene

#include <stdio.h>

int main (){


int a,b,c;
printf("Enter sides of triangle:");
scanf("%d%d%d",&a,&b,&c);
if(a == b && b == c)
printf("The Given Triangle is equilateral");
else if(a == b && b!= c)
printf("The given Triangle is isosceles");
else
printf("The given Triangle is scalene");
}

Write a C program to print strong numbers in a given interval using functions

https://colab.research.google.com/drive/1nlx9Wn19vjbGmzGJQZzyhZrPvB_ri1rp#scrollTo=JVen_neFwlKb&printMode=true 12/21
11/28/23, 10:01 PM Harshitha - Colaboratory

#include<stdio.h>

int factorial(int f);

int main()
{
int fact=1,sum=0;
int n1,n2,r;
printf("Enter the lower limit to find strong number:\n");
scanf("%d",&n1);
printf("Enter the upper limit to find strong number:\n");
scanf("%d",&n2);
printf("All strong numbers between 1 to 100 are:\n");
for(int i=n1;i<=n2;i++)
{
int k=i;
while(k!=0)
{
r=k%10;
fact=factorial(r);
k=k/10;
sum=sum+fact;
}
if(sum==i){
printf("%d, ",i);
}
sum=0;
}
return 0;
}
int factorial(int f)
{
int mul=1;
for(int i=1; i<=f;i++)
{
mul=mul*i;
}
return mul;
}

Write a C Program to get Half Pyramid of *

https://colab.research.google.com/drive/1nlx9Wn19vjbGmzGJQZzyhZrPvB_ri1rp#scrollTo=JVen_neFwlKb&printMode=true 13/21
11/28/23, 10:01 PM Harshitha - Colaboratory

#include <stdio.h>

int main (){


int a;
printf("Enter the number of rows: ");
scanf("%d",&a);
for(int i=1;i<=a;i++){
for(int j=1;j<=i;j++){
printf("* ");
}
printf("\n");
}
}

Write a program to print the multiplication table of a given number.

#include<stdio.h>

int main(){
int a;
scanf("%d",&a);
for (int i=1;i<=10;i++)
printf("%d\t*\t%d\t=\t%d\n", a,i,a*i);
}

Write a C program to print sum of the digits of a given number.

#include <stdio.h>
int main()
{
int n, t, sum = 0, remainder;

scanf("%d", &n);

t = n;

while (t != 0)
{
remainder = t % 10;
sum = sum + remainder;
t = t / 10;
}

printf("%d\n",sum);

return 0;
}

Write a C Program to print sum of n numbers( declare n variable of integer datatype)

https://colab.research.google.com/drive/1nlx9Wn19vjbGmzGJQZzyhZrPvB_ri1rp#scrollTo=JVen_neFwlKb&printMode=true 14/21
11/28/23, 10:01 PM Harshitha - Colaboratory

#include<stdio.h>

int main (){


int num, i, sum = 0;
scanf("%d", &num);
i = 0;
do
{
sum = sum + i;
i++;
} while (i <= num);

printf("%d", sum);
}

Write a C program that accepts an employee's ID, totally worked hours of a month, and the
amount he received per hour. Print the employee's ID and salary for a particular month.

#include<stdio.h>

int main(){
int emp;
float hours,aph;
scanf("%d%f%f",&emp,&hours,&aph);
printf("Emp no : %d\n",emp);
printf("Salary for the month : %.2f",hours*aph);
return 0;
}

Check whether the triangle is equilateral, isosceles or scalene.

#include <stdio.h>
int main()
{
int side1, side2, side3;
printf("Enter the lengths of the three sides of the triangle:\n");
scanf("%d", &side1);
scanf("%d", &side2);
scanf("%d", &side3);
if (side1 == side2 == side3) printf("The triangle is equilateral");
else if (side1 == side2 || side1 == side3 || side2 == side3) printf("The triangle is
else printf("Te triangle is scalene");
}

https://colab.research.google.com/drive/1nlx9Wn19vjbGmzGJQZzyhZrPvB_ri1rp#scrollTo=JVen_neFwlKb&printMode=true 15/21
11/28/23, 10:01 PM Harshitha - Colaboratory

Check whether entered year is leap or not

#include <stdio.h>
int main()
{
int x;
printf("Enter year: ");
scanf("%d", &x);

if ((x % 4 == 0 && x % 100 != 0) || (x % 400 == 0))


{
printf("%d is a leap year.\n", x);
}
else
{
printf("%d is not a leap year.\n", x);
}
}

Find minimum among three numbers

#include <stdio.h>
int main()
{
int n1, n2, n3, min;

printf("Enter three numbers: ");


scanf("%d %d %d", &n1, &n2, &n3);
min = n1;
if (n2 < min) min = n2;
if (n3 < min) min = n3;
printf("Minimum number among %d, %d, and %d is: %d\n", n1, n2, n3, min);
}

Check whether a number is divisible by 5 and 11 or not

https://colab.research.google.com/drive/1nlx9Wn19vjbGmzGJQZzyhZrPvB_ri1rp#scrollTo=JVen_neFwlKb&printMode=true 16/21
11/28/23, 10:01 PM Harshitha - Colaboratory

#include <stdio.h>
int main()
{
int number;
printf("Enter an integer: ");
scanf("%d", &number);
if (number % 5 == 0 && number % 11 == 0)
{
printf("%d is divisible by both 5 and 11\n", number);
}
else
{
printf("%d is not divisible by both 5 and 11\n", number);
}
}

Check whether a number is positive, negative or zero using switch case

#include <stdio.h>
int main()
{
int num;
printf("Enter a number: ");
scanf("%d", &num);
switch (num > 0)
{
case 1:
printf("Positive number\n");
break;
case 0:
switch (num < 0)
{
case 1:
printf("Negative number\n");
break;
case 0:
printf("Zero\n");
break;
}
break;
}
}

Design a calculator that performs arithmetic operations on two numbers using switch case

https://colab.research.google.com/drive/1nlx9Wn19vjbGmzGJQZzyhZrPvB_ri1rp#scrollTo=JVen_neFwlKb&printMode=true 17/21
11/28/23, 10:01 PM Harshitha - Colaboratory

#include <stdio.h>

int main() {
char operation;
double num1, num2, result;

while (1)
{
printf("Enter an arithmetic operation (+, -, *, /) or 'q' to quit: ");
scanf(" %c", &operation);

if (operation == 'q' || operation == 'Q')


{
break;
}

printf("Enter two numbers: ");


scanf("%lf %lf", &num1, &num2);

switch (operation)
{
case '+':
result = num1 + num2;
break;
case '-':
result = num1 - num2;
break;
case '*':
result = num1 * num2;
break;
case '/':
if (num2 != 0)
{
result = num1 / num2;
}
else
{
printf("Division by zero is not allowed.\n");
continue; // Skip to the next iteration
}
break;
default:
printf("Invalid operation. Try again.\n");
continue; // Skip to the next iteration
}

printf("Result: %.2lf\n", result);


}

return 0;
}

Find roots of a quadratic equation


https://colab.research.google.com/drive/1nlx9Wn19vjbGmzGJQZzyhZrPvB_ri1rp#scrollTo=JVen_neFwlKb&printMode=true 18/21
11/28/23, 10:01 PM Harshitha - Colaboratory

#include <stdio.h>
#include <math.h>
int main()
{
int a, b, c, disc, *r1, *r2;
printf("Enter coefficients a, b and c for ax^2 + bx + c = 0 ");
scanf("%d %d %d", &a, &b, &c);
discriminant = b * b - 4 * a * c;

if ( disc < 0) printf("Complex roots");


{
r1 = &((-b + sqrt(disc))/(2 * a));
r2 = &((-b - sqrt(disc))/(2 * a));

if (disc == 0) {
printf("both roots are equal, ie: %d\n", *r1);
} else {
printf("Real roots: %d and %d\n", *r1, *r2);
}
}
}

Find factorial of a number

%%file factorial.cpp
#include <stdio.h>
int main()
{
int n;
unsigned long long factorial = 1;

printf("Enter a positive integer: ");


scanf("%d", &n);

if (n < 0) printf("Factorial is not defined for negative numbers.\n");


else
{
for (int i = 1; i <= n; i++)
{
factorial *= i;
}
printf("Factorial of %d = %llu\n", n, factorial);
}

return 0;
}

Check whether a number is a palindrome or not


https://colab.research.google.com/drive/1nlx9Wn19vjbGmzGJQZzyhZrPvB_ri1rp#scrollTo=JVen_neFwlKb&printMode=true 19/21
11/28/23, 10:01 PM Harshitha - Colaboratory

#include <stdio.h>
int main()
{
int num, reversed = 0, original;
printf("Enter an integer: ");
scanf("%d", &num);
original = num;
while (num > 0)
{
reversed = reversed * 10 + num % 10;
num /= 10;
}

if (original == reversed)
{
printf("%d is a palindrome\n", original);
} else {
printf("%d is not a palindrome\n", original);
}

return 0;
}

Check whether number is perfect or not

#include <stdio.h>
int main()
{
int rows = 4;

for (int i = 1; i <= rows; i++)


{
for (int j = 1; j <= i; j++)
{
printf("*");
}
printf("\n");
}
}

https://colab.research.google.com/drive/1nlx9Wn19vjbGmzGJQZzyhZrPvB_ri1rp#scrollTo=JVen_neFwlKb&printMode=true 20/21
11/28/23, 10:01 PM Harshitha - Colaboratory

%%file abcde.cpp
#include <stdio.h>
int main()
{
int rows=5;
char alphabet = 'A';

for (int i = 1; i <= rows; i++)


{
for (int j = 1; j <= i; j++)
{
printf("%c", alphabet);
}
alphabet++;
printf("\n");
%%file numbers.cpp
}
#include <stdio.h>
int main()
{ return 0;
} int rows=4;
int num = 1;
for (int i = 1; i <= rows; i++)
{
for (int j = 1; j <= i; j++)
{
printf("%d ", num);
num++;
}
printf("\n");
}

return 0;
}

https://colab.research.google.com/drive/1nlx9Wn19vjbGmzGJQZzyhZrPvB_ri1rp#scrollTo=JVen_neFwlKb&printMode=true 21/21

You might also like