0% found this document useful (0 votes)
29 views

Assignments

The document contains C code snippets for several programs: 1) A program to add the first and last digits of a four digit number. 2) A program to read a weekday number and print the name using a switch statement. 3) A program to read 10 numbers, calculate their sum and average. 4) Additional programs to print ASCII values, calculate powers, check for vowels/consonants, calculate sums of series, find sums of squares, and find the sum of prime numbers between 1 and n.

Uploaded by

PRATIKSHA BHOYAR
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views

Assignments

The document contains C code snippets for several programs: 1) A program to add the first and last digits of a four digit number. 2) A program to read a weekday number and print the name using a switch statement. 3) A program to read 10 numbers, calculate their sum and average. 4) Additional programs to print ASCII values, calculate powers, check for vowels/consonants, calculate sums of series, find sums of squares, and find the sum of prime numbers between 1 and n.

Uploaded by

PRATIKSHA BHOYAR
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

1.

/* write a program to add first and last digit of a four digit number */

#include<stdio.h>
#include<conio.h>
int main()
{
int x,sum;
clrscr();
printf("Enter four digit number:\n");
scanf("%d",&x);
printf("Four digit number is %d\n",x);
sum=x%10+x/1000;
printf("sum=%d\n",sum);
getch();
return 0;
}
16./* write a program to read week day No. and print week day name
using switch */
#include<stdio.h>
#include<conio.h>
int main()
{
int day;
clrscr();
printf("Enter day(1 to 7):");
scanf("%d",&day);
switch(day)

{
case 1 : printf("monday\n");
break;
case 2 : printf("tuesday\n");
break;
case 3 : printf("wensday\n");
break;
case 4 : printf("Thursday\n");
break;
case 5 : printf("friday\n");
break;
case 6 : printf("Saturday\n");
break;
case 7 : printf("sunday\n");
break;
default : printf("not a valid day\n");
}
getch();
}
4./* Write a program to read 10 digit number from keyboard and find
their sum and average */
#include<stdio.h>
#include<conio.h>
int main()
{
int i,n,sum=0,average;
clrscr();
printf("Enter 10 digit number\n");
for(i=1;i<=10;i++)
{
scanf("%d",&n);
sum=sum+n;
average=sum/10;
}
printf("%dis the sum of 10 number\n",sum);
printf("%d is the average of 10 number\n",average);
getch();
}
6./* Write a program to print ASCII values and their equivlant
character */

#include<stdio.h>
#include<conio.h>
int main()
{
char p;
clrscr();
printf("Enter a character:\n");
scanf("%c",&p);
printf("ASCII value of %c=%d",p,p);
getch();
return 0;
}
9./* Write a program to calculate power of a number using while loop
*/

#include<stdio.h>
#include<conio.h>
int main()
{
int a,b;
long result=1;
clrscr();
printf("Enter a base value:\n");
scanf("%d",&a);
printf("Enter power value:\n");
scanf("%d",&b);
while(b!=0)
{
result=result*a;
--b;
}
printf("result=%d",result);
getch();
}
17./* Write a program to check whether a character is vowel or consonant using
switch*/

#include<stdio.h>
#include<conio.h>
int main()
{
char c;
clrscr();
printf("Enter character:\n");
scanf("%c",&c);
switch(c)
{
case 'a':
case 'i':
case 'e':
case 'o':
case 'u':
printf("Entered character is vower");
break;
default:
printf("Entered character is consonant");
break;
}
getch();
return 0;
}
11./* Write a program to find sum of the square of all natural number
from 1 to n */

#include<stdio.h>
#include<conio.h>
int main()
{
int n=0,a;
clrscr();
printf("Enter the number:\n");
scanf("%d",&n);
a=(n*(n+1)*(2*n+1))/6;
printf("The sum of the square between 1 to %d is:=%d",n,a);
getch();
return 0;
}

/*
10./* Write a program to find sum of the series 1+11+111+1111 */

#include<stdio.h>
#include<conio.h>
int main()
{
int n,i,sum=0, t=0;
clrscr();
printf("Enter Number\n");
scanf("%d\n",&n);
for(i=1;i<=n;i++)
{
t=t*10+1;
sum=sum+t;
}
printf("%d\n",sum);
getch();
return 0;
}

/* write a program
to find sum of all
prime no. between 1
to n */
#include<stdio.h>
#include<conio.h>
int main()
{
int i,j,n,isPrime,sum=0;
clrscr();
printf("Find sum of all prime between 1 to n");
scanf("%d",&n);
for(i=2;i<=n;i++)
{
isPrime=1;
for(j=2;j<=i/2;j++)
{
if(i%j==0)
{
isPrime=0;
break;
}
}
if(isPrime==1)
{
sum=sum+i;
}
}
printf("Sum of all prime numbers between 1 to n %d=%d",n,sum);
getch();
return 0;
}

You might also like